This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def env | |
@_env ||= begin | |
require 'active_support/string_inquirer' | |
ActiveSupport::StringInquirer.new(RAILS_ENV) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Momoro < ActiveRecord::Base | |
def food | |
ActiveSupport::StringInquirer.new("#{read_attribute(:food)}") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# sweet! | |
>> m.food = "hi" | |
=> "hi" | |
>> m.food.hi? | |
=> true | |
>> m.food.apple? | |
=> false | |
>> m.food = "apple" | |
=> "apple" | |
>> m.food.apple? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Momoro < ActiveRecord::Base | |
def food | |
ActiveSupport::StringInquirer.new(read_attribute(:food)) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
=> #<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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActiveSupport | |
# Wrapping a string in this class gives you a prettier way to test | |
# for equality. The value returned by <tt>Rails.env</tt> is wrapped | |
# in a StringInquirer object so instead of calling this: | |
# | |
# Rails.env == "production" | |
# | |
# you can call this: | |
# | |
# Rails.env.production? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Momoro < ActiveRecord::Base | |
def food | |
ActiveSupport::StringInquirer.new("basil") | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>> !!active | |
=> true | |
>> active = false | |
=> false | |
>> !!active | |
=> false | |
>> active = nil |