Localizing numbers in Ruby without Rails - ruby

I've got a Ruby app that is not using Rails, but needs localization. I've been using the I18n gem, which works well for dates, times, and strings. It doesn't seem to do numbers, though, unless I'm missing something.
I found the NumberHelper class in ActionView from Rails, and this question says how you can include it in a non-Rails app. However, making my app dependent on the whole activemodel framework seems like overkill just to get a few numeric localization routines.
Is there a better way to approach this? Should I be using one of the other Ruby localization libraries instead?

I've used the gem R18n for that: https://github.com/ai/r18n
It can do the same stuff as I18n but is more flexible and can also localize numbers. It does not have dependencies of other gems.
Basic usage:
Add r18n-core to Gemfile
Initialize with `R18n.extension_places << 'path/to/yml/files'
And then either:
i18n = R18n::I18n.new('en')
i18n.t[:foo][:bar] # => "Translated text"
i18n.l(123.45) # => "123.45"
Or the more static version:
R18n.locale = 'en'
R18n.t[:foo][:bar]
R18n.l(123.45)
Helpers exists (in other gems) for rails and sinatra

Related

Using `wicked_pdf` in a non-rails project

All the documentation for wicked_pdf assume you are using rails.
But I want to use this feature in a non-rails environment (a Jekyll plugin, written in Ruby).
Trying to use pdf = WickedPdf.new.pdf_from_string(page.content) returns Error: uninitialized constant WickedPdf.
How do I initialize WickedPdf without the rails generate wicked_pdf provided in their documentation?
I had to add gem 'activesupport' to my gemfile, then use require 'wicked_pdf' in my source code.
wicked_pdf depends on active_support/core_ext - as you can see in lib/wicked_pdf.rb. However you can use it outside of controller "context" like this...
pdf = WickedPdf.new.pdf_from_string('<h1>My life is wicked.</h1>')

Has anyone gotten Padrino to work with Sprockets and Compass?

I'm on Padrino 0.10.7 and I haven't been able to get Sprockets to recognize Compass in the load path.
This is the only question on SO I've found on this topic.
Got this working with the following:
padrino-pipeline gem - going down the Sprockets route
Somewhere (I used my app.rb file) you will need the following:
Sass.load_paths << "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/compass/stylesheets"
Sass.load_paths << "#{Gem.loaded_specs['compass'].full_gem_path}/frameworks/blueprint/stylesheets"
Not sure if there is a simpler way. Feels like there should be. Also, any neat ways of getting image-url() sass method working?
EDIT: There is a way to get image-url working - if you thought the above was messy, add this to your app.rb too...
assets.context_class.class_eval do
def settings
YourAppName::App.settings
end
include Padrino::Helpers::AssetTagHelpers
end
(where YourAppName is the name of the module that describes your application, at the top of app.rb)

padrino can't find active_support methods

In my Padrino application i'm trying to get the time 10 years from now using
10.year.from_now
I'm getting:
NoMethodError: undefined method `year' for 10:Fixnum
Any idea why ?!
ActiveSupport is not fully loaded by default this because differently than rails Padrino aims to be small and fast.
If you use ActiveRecord a full set of ActiveSupport will be loaded otherwise you need to require (i.e. in boot.rb) your dependencies. In your case:
require 'active_support/core_ext'
require 'active_support/duration'
My suggestion is to load a full active_support set whenever is really needed.

Getting around ruby-units conflict with activesupport

I want to use the ruby-units with a rails 3 project, but it seems like it conflicts with activesupport.
It looks to me like both activesupport and ruby-units create a to() method for String. For some reason, ruby-units one wins, and so whenever to() is called inside rails it throws an error. (Unit not recognized)
I want to know the best way to deal with this. I don't care about having the to() method from ruby-units, I just don't want it to interfere with rails. I'd like to avoid forking if there's another approach.
To see my problem:
Add to your gemfile
gem 'ruby-units'
Open up rails console (I'm on rails 3 with ruby 1.9.2) and try apples.to(1)
Without ruby-units:
"ap"
With:
"'apples' Unit not recognized"
The answer to this is to require the ruby-units library before the rails library in the Gemfile:
gem 'ruby-units'
gem 'rails'
Obviously you then won't be able to use .to() on strings to access the ruby-units conversion.

Is there a Rack or Sinatra based environment configuration utility?

Is there anything in the Sinatra / Rack world similar to Rails configuration loading scheme that loads one of the config\enviroments\*.rb files depending on Rails.env
I know I could develop one pretty easily, i was just wondering if there was something already in place.
If you're following the Rails convention of putting a file for each environment in config/environments/environment_name.rb, you can put something like this in your Sinatra app, or for Rack in your config.ru file:
Dir.glob(File.dirname(__FILE__) + "/config/environments/#{settings.environment}.rb", &method(:require))
With some minor modifications you could make it load other file locations/combinations. Sinatra's configure blocks work just as well, too.
It turns out that there is something from Sinatra, that provides a similar, though limited, functionality.
See the code:
https://github.com/sinatra/sinatra/blob/master/lib/sinatra/base.rb#L1120
So that you can do this:
class MyApp < Sinatra::Base
configure :development, :test do
#only executes this code when environment is equal to one of the passed arguments
# I'm pretty sure Sinatra sets this based on ENV['RACK_ENV']
end
end
There is one called Sinatra::ConfigFile, which now lives in Sinatra::Contrib http://www.sinatrarb.com/contrib/config_file.html
There's lots of useful stuff in there.
I adapted mine from monkrb.com (it's also yaml in RoR anyways)
YAML.load_file(path_of "config/settings.yml")[RACK_ENV]
e.g.
http://github.com/codepants/yasumi/blob/master/config/settings.yml

Resources