Skip to content

Instantly share code, notes, and snippets.

@tjh
Created October 21, 2011 01:30
Show Gist options
  • Save tjh/1302883 to your computer and use it in GitHub Desktop.
Save tjh/1302883 to your computer and use it in GitHub Desktop.
acts_as_boolean

Acts As Boolean

A quick way to create an attribute on your ActiveRecord models that always responds with a boolean value, similar to the Boolean attributes from a table that have the ? method for syntactic sugar.

class SomeObject < ActiveRecord::Base
  acts_as_boolean :is_active
end

o = SomeObject.new
o.is_active?                  # => false
o.is_active = true
o.is_active?                  # => true
o.is_active = SomeObject.new
o.is_active?                  # => true
o.is_active = nil 
o.is_active?                  # => false

Credits

Gem::Specification.new do |s|
s.name = 'acts_as_boolean'
s.version = '0.1.0'
s.platform = Gem::Platform::RUBY
s.author = 'Tim Harvey'
s.email = 'tim@theharveys.org'
s.summary = 'Acts As Boolean'
s.description = 'Create question-mark attr_accessor model methods'
s.files = ['acts_as_boolean.rb']
s.test_file = 'acts_as_boolean_spec.rb'
s.require_path = '.'
s.add_development_dependency('rspec', ["~> 2.0"])
end
module ActsAsBoolean
def acts_as_boolean(*attributes)
[*attributes].each do |attribute|
# Define the = setter method
attr_writer attribute
# Define the ? accessor method
define_method("#{attribute}?") { !!instance_variable_get("@#{attribute}") }
end
end
end
ActiveRecord::Base.send :extend, ActsAsBoolean
require File.expand_path('acts_as_boolean')
class ObjectWithActsAsBoolean
extend ActsAsBoolean
end
describe ActsAsBoolean do
let(:instance) { ObjectWithActsAsBoolean.new }
describe '#acts_as_boolean' do
context 'with an argument of :my_attribute' do
before(:each) { ObjectWithActsAsBoolean.send :acts_as_boolean, :my_attribute }
it 'should define a #my_attribute? accessor method' do
instance.respond_to?(:my_attribute?).should be_true
end
it 'should define an #my_attribute= setter method' do
instance.respond_to?(:my_attribute=).should be_true
end
it 'should not define a #my_attribute getter method' do
instance.respond_to?(:my_attribute).should be_false
end
it 'should return false when #my_attribute? is called and my_attribute is nil' do
instance.my_attribute?.should == false
end
it 'should return true when #my_attribute? is called and my_attribute is true' do
instance.my_attribute = true
instance.my_attribute?.should == true
end
it 'should return false when #my_attribute? is called and my_attribute is false' do
instance.my_attribute = false
instance.my_attribute?.should == false
end
end
context 'with two arguments, :my_attribute and :your_attribute' do
before(:each) { ObjectWithActsAsBoolean.send :acts_as_boolean, :my_attribute, :your_attribute }
it 'should define a #my_attribute? accessor method' do
instance.respond_to?(:my_attribute?).should be_true
end
it 'should define a #your_attribute? accessor method' do
instance.respond_to?(:your_attribute?).should be_true
end
it 'should return true when #your_attribute? is called and your_attribute is true' do
instance.your_attribute = true
instance.your_attribute?.should == true
end
it 'should return true when #my_attribute? is called and my_attribute is true' do
instance.my_attribute = true
instance.my_attribute?.should == true
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment