Skip to content

Instantly share code, notes, and snippets.

class Momoro < ActiveRecord::Base
def food
ActiveSupport::StringInquirer.new("#{read_attribute(:food)}")
end
end
def env
@_env ||= begin
require 'active_support/string_inquirer'
ActiveSupport::StringInquirer.new(RAILS_ENV)
end
end
# It doesn't work this way!
>> m = Momoro.new
=> #<Momoro id: nil, name: nil, food: nil, created_at: nil, updated_at: nil>
>> m.food = "basil"
>> m.food.basil?
=> true
>> m.food = "pillows"
>> m.food.pillows?
class Momoro < ActiveRecord::Base
def food
ActiveSupport::StringInquirer.new("basil")
end
end
=> #<Momoro id: 5, name: nil, food: nil, created_at: "2008-12-09 07:38:34", updated_at: "2008-12-09 07:38:34">
>> m.food
=> "basil"
>> m.read_attribute(:food)
=> nil
>> m = Momoro.new
=> #<Momoro id: nil, name: nil, food: nil, created_at: nil, updated_at: nil>
>> m.food = "basil"
=> "basil"
>> m.food.basil?
method name = basil?
method_name.to_s[-1,1] = ?
self = basil
method_name.to_s[0..-2] = basil
=> true
class Momoro < ActiveRecord::Base
def food
ActiveSupport::StringInquirer.new(read_attribute(:food))
end
end
if [child, parent, grandpa, president, food, ink].all{|e| e.valid?}
... #do stuff
end
>> %w{foo, bar, baz}.all?{|e| e.is_a?(String)}
=> true
>> ["foo", "bar", 1].all?{|e| e.is_a?(String)}
=> false
@mischa
mischa / railroad.rb
Created December 16, 2008 02:52
Run Railroad as a rake task
#(Thanks to Thomas Ritz, http://www.galaxy-ritz.de ,for the code.)
namespace :doc do
namespace :diagram do
task :models do
sh "railroad -i -l -a -m -M | dot -Tsvg | sed 's/font-size:14.00/font-size:11.00/g' > doc/models.svg"
end
task :controllers do
sh "railroad -i -l -C | neato -Tsvg | sed 's/font-size:14.00/font-size:11.00/g' > doc/controllers.svg"
module FlashHelper
def flashes
{:partial => '/forms/flashes', :object => flash}
end
def flash_names
[:notice, :error, :message, :success]
end