create our own gem in ruby on rails

$ gem install bundler
 
 
$ bundle gem dogeify
 
This will create following directories 
 
$ tree dogeify
dogeify
├── .gitignore
├── Gemfile
├── LICENSE.txt
├── README.md
├── Rakefile
├── dogeify.gemspec
└── lib
    ├── dogeify
    │   └── version.rb
    └── dogeify.rb  
 
 
Let's first look at the gemspec file (dogeify.gemspec in this case). 
 
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'dogeify/version'

Gem::Specification.new do |spec|
  spec.name          = "dogeify"
  spec.version       = Dogeify::VERSION
  spec.authors       = ["Matt Huggins"]
  spec.email         = ["matt.huggins@gmail.com"]
  spec.description   = %q{Convert everyday boring English into doge speak!}
  spec.summary       = %q{English to doge translations}
  spec.homepage      = ""
  spec.license       = "MIT"

  spec.files         = `git ls-files`.split($/)
  spec.executables   = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
  spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
  spec.require_paths = ["lib"]

  spec.add_dependency 'engtagger'

  spec.add_development_dependency 'bundler', '~> 1.3'
  spec.add_development_dependency 'rake'
  spec.add_development_dependency 'rspec'
end
 
 
The first grouping of lines (assigning name, version, authors,
 etc.) are relatively straightforward string (and array of string) 
assignments, so I won't dive into those beyond pointing out that they 
exist.
 
 
Now that we're familiar with the gemspec, let's take a look at lib/dogeify.rb and lib/dogeify/version.rb.  These two files are initially very simple.  Let's start with version.rb  
 
 module Dogeify
  VERSION = "0.0.1"
end
 
Let's now take a look at dogeify.rb.
 
require "dogeify/version"

module Dogeify
  # Your code goes here...
end
 
Puts your code here 
 
require 'dogeify/version.rb'

module Dogeify
  def self.process(str)
    # TODO: process `str`
    str
  end
end 
 
After you have created a gemspec, you can build a gem from it. Then you can install the generated gem locally to test it out.
  $ gem build dogeify.gemspec
   Successfully built RubyGem
   Name: dogeify
   Version: 0.0.0
   File: dogeify-0.0.0.gem

$ gem install dogeify-0.0.0.gem
  Successfully installed dogeify-0.0.0
  1 gem installed

Of course, the smoke test isn’t over yet: the final step is to require the gem and use it:
$ irb
>> require 'dogeify'
=> true
>> Dogeify.process(Hello world)
Hello world! 
 
1- executes git tag -am "tag [tag_name]" [tag_name], with tag_name being the version number as specified in your .gemspec preceded by v (e.g. v0.0.1)

2- executes git remote add remote_name url
2- executes git push remote_name master  


Releasing your gem

$ bundle exec rake release



 

Comments

Popular Posts

How to pass hash in Postman

nginx: unrecognized service

Bootstrap Select Picker append add new item if search not exist

Reading Excel Sheets using "Roo" gem in ruby on rails

Add CORS to Nginx on AWS Elastic Beanstalk

Enable gzip compression on Elastic Beanstalk with nginx

Can't connect to local MySQL server through socket '/var/run/mysqld/mysqld.sock'

Get video duration by URL in Ruby on Rails

site-enables nginx setting in ruby in rails