Ruby gem for resolving pluralization in English - ruby-on-rails

This is strange question in sense that I don't think there is answer for it but here it goes.
I am looking for gem which would allow me to get root word from pluralized word.
categories => category
people => person
apples => apple
Trick is I need it in ruby and not in ROR so solution should be independent from ActiveRecord which probably has this mechanism built in. Thanks. There is also paper on this subject that I found http://www.csse.monash.edu.au/~damian/papers/HTML/Plurals.html/ in case somebody is interested in building the gem :)

This behaviour is defined in ActiveSupport, which you can include on its own without having to require Rails completely.
>> require 'rubygems'
=> true
>> require 'active_support/core_ext/string/inflections'
=> true
>> "categories".singularize
=> "category"

You can require just the inflections from ActiveSupport, leaving out the rest of rails, like this:
require 'active_support/core_ext/string/inflections'
If that doesn't work for you, check out the inflections gem.

You could always install just the active_support gem, and require just the inflector, that way you don't load all of rails. Many parts of rails can be used independently.
$ irb
1.9.3-p194 :001 > require 'active_support/inflector'
=> true
1.9.3-p194 :002 > ActiveSupport::Inflector.singularize('inflections')
=> "inflection"

Related

Non specific MVC framework ruby gems and example code to Rails framework

I have been learning Ruby on Rails, but I still have issues when it comes to Ruby gems with examples that are irb based and not Rails or Sinatra framework based. I am trying to implement the Block.io Bitcoin API functionality. But the code I find is Ruby only, so I am not sure where to create a config file for the API Key and also whether I need to create a controller to make this work in the views for Rails.
The gem and examples are on: https://github.com/BlockIo/gem-block-io
I installed this gem via bundle install on Rails
gem install block_io -v=1.0.6
The Ruby example show the following:
>> require 'block_io'
>> BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2
In Rails which config file would I enter the above api_key and pin?
In the example they show the code to get your address as follows:
BlockIo.get_my_address
Do I need to create a function in a controller such as:
def address
#my_address = BlockIo.get_my_addresses
end
and in the view use:
<%= #my_address %>
I need some guidance with regards to the above, any comment or assistance will be greatly appreciated.
require 'block_io' can go into Gemfile like gem 'block_io'. Rails/bundler will require it automaticaly for you as long as the gem name is also the file name you want to require from this gem.
BlockIo.set_options :api_key=> 'API KEY', :pin => 'SECRET PIN', :version => 2 can be put into an initilizer like config/initializers/block_io.rb. This way set_options is called only once when Rails starts a server or console or runner.
Put it like this into the file config/initializers/block_io.rb
BlockIo.set_options :api_key=> ENV['BLOCK_IO_API_KEY'], :pin => ENV['BLOCK_IO_PIN'], :version => 2
With the environment variables in use you don't commit any secret into your repo.
Now you should be able to call BlockIo.get_my_address within any action.

Error when trying to use ruby gem- NameError (uninitialized constant RightScraper::Scraper)

I am trying to use a ruby gem called right_scraper. I have added the gem to my gem file and it installs fine. I used the example code from the gems github page (https://github.com/rightscale/right_scraper)
require 'rubygems'
require 'right_scraper'
scraper = RightScraper::Scraper.new(:basedir => '/tmp', :kind => :cookbook)
scraper.scrape(:type => :git, :url => 'git://github.com/rightscale/right_scraper.git')
running this code returns the following error:
NameError (uninitialized constant RightScraper::Scraper)
Does anyone know whats going wrong?
Looks like the README is out of date; that class does indeed not exist.
In cases like this, it is often a good idea to look at the specs to see what the correct usage should be. For example, based on this spec, I found the following works:
RightScraper::Main.new(:basedir => '/tmp', :kind => :cookbook)
It would also be a good idea to raise this as an issue with the author. You could even provide a pull request with some up-to-date documentation.

Ruby On Rails Rspec require conflicts

The following is my problem I have Rspec installed started a project in it every-time I want to test something have to require_relative it now this wasn't a problem until I wanted to finally test my controllers and cant seem to get the functions get , post constantly getting undefined function error.
If I put my controller like this
RSpec.describe ApplicationController, :type => :controller do
end
I am receiving uninitialized constant ApplicationController (NameError) if i require relative the application controller like this require_relative '../../app/controllers/application_controller' I get uninitialized constant ActionController (NameError) .
Can't seem to catch where the problem might be here is my Gemfile also
gem "rspec-rails", :group => [:test, :development]
gem "rspec", :group => [:test, :development]
group :test do
gem "capybara"
gem "factory_girl_rails"
end
I have been browsing the internet for 2 hours now viewd at least 10 other stackoverflow questions tried everything but nothing seems to be working ...
Everywhere I check they say that I should only need to add require 'spec_helper' and it should work but its not.
I can copy my rails_helper file also if needed but its the standard what came with it when I ran rails generate rspec:install .
Your help is much appreciated I really need to fix this because I am running out of time.
As of rspec-rails 3 the auto generated spec_helper.rb is split into two parts: spec_helper.rb and rails_helper.rb
The idea is that you can have specs that don't need rails loaded (and so are fast to load) as well as specs that do need rails loaded
For ones that do need rails loaded, like your controller spec you need to do
require 'rails_helper'
At the top, instead of requiring spec_helper.

Create a routes entry from your own gem

I'm writing a gem and I'm going to use it with Rails 4. Is it possible for me to add a route from my Gem rather than from config/routes.rb in my rails project? I want this to be inside a gem so I can include it in more than one Rails project without having to configure every Rails project, rather do it once in the gem. Is that possible and how?
i.e :
If my routes were :
get 'test' => 'users#test'
how would that translate into my gem. If my gem were used as an engine just like RB suggested in his answer :
module Blorgh
class Engine < ::Rails::Engine
get 'test' => 'users#test'
end
end
This doesn't work, what am I doing wrong?
Read the Engine Guide of Ruby on Rails. Basically you'll want to create the file in config/routes.rb (on your gem folder) and add the following:
YourGemName::Engine.routes.draw do
get 'test' => 'users#test'
end
Yes it is possible if you make your gem an engine.
Read the Getting Started with Engines guide.

config.gem requires gem?

I have a bunch of config.gem statements in my environment.rb file:
config.gem "fastercsv", :version => "~>1.5.0"
config.gem "parseexcel", :version => "~>0.5.2"
config.gem "geokit", :version => "~>1.4.1"
config.gem "facebooker", :version => "~>1.0.50"
...
If I do "rake gems:install" then I get this issue:
rake aborted!
no such file to load -- fastercsv
Well...i know there is no such file to load because I am trying to install it. I suspect this may result from the location of my require. I have a module in my lib directory:
module SmartContactsImporter
require 'fastercsv'
require 'parseexcel'
...
Maybe Rails doesn't like me requiring a gem there but it seems silly since there is nothing wrong with having your module depend on a gem. Any ideas on how to solve this issue?
UPDATE
Turns out that this issue also occured with mechanize, geokit, and the list is continuing. It's a bit strange that config.gem doesn't work pretty easily out of the box. FYI I'm not freezing my gems.
If you leave out the require in SmartContactsImporter this should work (config.gem "fastercsv" will do the require for you).
You can work around it when a require is needed in environment.rb with a:
begin
require "rack/cache"
rescue LoadError
STDERR.puts "not loaded rack/cache: #{$!}"
end
This is ugly but it does do the trick.
You shouldn't require inside your module, config.gem will require it for you.
There's also a related issue with config.gem where it will attempt to require a dependent gem that is not yet installed whilst installing the gems, but this doesn't appear to be the case yet.

Resources