How to create a Gem file

1) bundle gem gemwork1

(My IDE has option for “Gem Application” and given the name “gemwork1” as the application name)

2) in ‘lib/gemwork1.rb’ add the functionality code

example

add the following code to already existing module “Gemwork1”. here we adding form validation for email field

def validates_email_address_of attr_name

email_regex = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
self.validates_format_of attr_name,
:with => email_regex,
:message => “is not a valid email address”
end
To extend it with ActiveRecord::Base class add this too

require “gemwork1”
class ActiveRecord::Base
extend Gemwork1
end

where “gemwork1” is file name and “Gemwork1” is module name

3) build the gem:- gem build gemwork1.gemspec or gem build
4) to install it into your system:- rake install
5) to check the working

create a new rails project and add the following in your Gemfile:
gem ‘gemwork1’

Run bundle, generate a User scaffold with an email:string attribute and migrate the database. In your User model add a validation for a given email string

validates_email_address_of :email

Now start the server and try to create a new user with an email address like “foo”. Rails should output an validation error.

6) to publis the gem in RubyGems

want a account in https://rubygems.org/. during the push command it will ask email and password for sign in
gem push gemwork1-0.0.1.gem

references

1) http://blog.galk.me/post/7443618295/getting-started-with-gems-how-to-create-a-gem-for
2) http://railscasts.com/episodes/245-new-gem-with-bundler

 

www.klairvoyant.in

 

Advertisements
This entry was posted in rails, ROR, ruby on rails, web development and tagged , , , , , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s