• No se han encontrado resultados

1.2. CEMENTO PORTLAND

1.2.2. Cementos comunes: Clasificación y componentes

1.2.2.1. Clasificación de los cementos

The first step when designing a new library is to organize the structure of the files and directories. This includes how to package the library for deployment and installation on other computers. RubyGems is the obvious choice for creating and sharing libraries. This is true even for service client libraries that are private and will be shared only by internal services and developers. Gems can be deployed via a private file server, a local gem server, or inside the Bundler cache of an application.

ptg To create a gem, all that is required is a gemspec. It’s possible to create this file

manually by using the syntax on the Gem::Specification documentation page

(http://rubygems.rubyforge.org/rubygems-update/Gem/Specification.html). How- ever, it can get a bit time-consuming and can cause problems if you forget to update the gemspec with new files. An easier way to get started creating a RubyGems gem is to use a generator such as Jeweler.

Jeweler

Jeweler (http://github.com/technicalpickles/jeweler), created by Josh Nichols, is a gen- erator for starting new gems. It has a convenient command-line interface for starting new gems and comes with built-in rake tasks to handle things like generating the

gemspec, building the gem, and pushing to RubyGems (http://rubygems.org). For internal libraries, you’ll probably want to skip the last part, but the other conveniences of the library make it worth using even for private gems.

This chapter focuses on building out an example from the social feed reader appli- cation. First, you create a library for the entry service. Remember that this service has methods for getting the information on feed entries, given a list of IDs. A larger exam- ple might have the service also return entries given a source (such as a feed URL or an ID), but the single list of IDs is enough to highlight the important parts. The second library is for accessing the vote service created in Chapter 5, “Implementing Services.” The third library is for accessing the reading list for a user. The simple example in this chapter only returns a list of entry IDs, given the user email. A more complete exam- ple would include getting a list of subscriptions and adding and removing feeds from

Bundler

Bundler (http://gembundler.com) is a new system of man- aging an application’s packages. It is part of how Rails 3 manages dependencies. The .gem files, which are built

gems, can be placed in an application’s local Bundler cache so that they can be installed by Bundler without checking a remote gem server. In Rails, the cache is located in the vendor/ bundler/ruby/1.8/cache/ directory. In place of 1.8, you substi- tute the version of Ruby that your application is being devel- oped on.

ptg the subscriptions. It is important to make these three separate libraries, one for each

service, to ensure that when the service is updated, only its gem has to be updated. First, you install Jeweler and its dependencies. This requires that you have the git

and gemcutter gems installed in addition to any testing library that you’ll use. You

also need git installed. The Jeweler installation is simple: gem install jeweler

Now it’s time to create the gems. For organizational purposes, it’s easiest to cre- ate a single repository that all the gems can be put in. However, in a production environment, it’s a good idea to keep each client gem as a directory in the larger structure of the service code. That way, the client can be iterated and improved with the service code.

In the overall gem directory, it’s simple to create the skeleton structure of the gems with the following commands:

jeweler ––rspec pauldix-entries

jeweler ––rspec pauldix-readling-list jeweler ––rspec pauldix-ratings

These examples use RSpec, but Jeweler includes helpers to generate files for many other testing frameworks. You can use the command jeweler–help to see the avail-

able options. The gems have my name, pauldix, as a prefix to ensure that they won’t

collide with any public gems. For your gems, you should use the organization name or an abbreviation as a prefix to the gem name. The generator creates a basic direc- tory structure for you, as well as some starting files. Here is the initial list for the

pauldix-entries gem: pauldix-entries/ lib/ pauldix-entries.rb LICENCE Rakefile README.rdoc spec/ pauldix-entries-spec.rb

ptg

spec.opts spec_heleper.rb

There are only a couple more things to set up to get an initial version of the gem built. First, you go into Rakefile and make the following changes:

# at the top of the file with the other requires require 'lib/pauldix-entries/version.rb'

# right after line 14

gem.version = PauldixEntries::VERSION

gem.files = FileList['lib/**/*.rb', '[A-Z]*', 'spec/**/*'].to_a

The require is a file that sets a version constant that will be shown next. This is use-

ful for other libraries that want to ensure the version of the gem at runtime. The other lines are modifications to the gem specification. The first line sets the gem version from a constant in a module that will be set up next. The second line sets the genera- tor to create a gemspec that includes all .rb files from the lib directory and all its sub-

directories, pulls in the README and LICENCE files from the root directory, and pulls in

all the files from the spec directory and its subdirectories.

Now it’s time to create the lib/pauldix-entries/version.rb file. You create

the directory and make the version.rb file look like this: module PauldixEntries

VERSION = "0.0.0" end

Next, you load the version file in lib/pauldix-entries.rb. This file is respon-

sible for loading all the other files and required gems used in the library. To begin, it’s only the following single line:

require 'pauldix-entries/version.rb'

With all this setup out of the way, it’s time to generate a gemspec and build the gem. Jeweler includes a few rake tasks for getting this done. From the root of the gem

directory, you issue the following commands:

rake gemspec rake build

ptg The first command generates the gemspec. It’s worth taking a quick look at the gen-

erated file just to see what the rake task has built. The second command generates the

gemspec and creates the gem file pkg/pauldix-entries-0.0.0.gem. This file can be

used to install the gem. You need to take the same steps to get the vote service gem ready to go. Later sections fill out the files in these gems and make them functionally complete.