spec/attributes_spec.rb in simple_model-1.1.1 vs spec/attributes_spec.rb in simple_model-1.2.0
- old
+ new
@@ -7,86 +7,161 @@
has_attributes :test1,:test2
end
@init = TestInit.new(:test1 => "1", :test2 => '2')
end
-
it "should set provided attributes on initialize" do
-
@init.test1.should eql("1")
@init.test2.should eql("2")
end
it "should include set attributes in attributes hash" do
- @init.attributes.class.should eql(Hash)
+ @init.attributes.should be_kind_of(ActiveSupport::HashWithIndifferentAccess)
@init.attributes[:test1].should eql("1")
@init.attributes[:test2].should eql("2")
end
-
-end
-describe SimpleModel::Attributes, 'define_reader_with_options' do
- before(:each) do
- class TestDefault
- include SimpleModel::Attributes
- attr_accessor :test
- define_reader_with_options :test, :default => "test"
+
+ context '#new_with_store'do
+ it "should use the provided object as the attribute store" do
+ my_store = {:test1 => 1,:test2 => 2}
+ new = TestInit.new_with_store(my_store)
+ new.test1 = 3
+ new.test1.should eql(3)
+ my_store[:test1].should eql(new.test1)
end
end
-
- it "should define setter method with default value" do
- default = TestDefault.new
- default.test.should eql("test")
- end
- it "should not intefer with setting" do
- default = TestDefault.new
- default.test = "New"
- default.test.should eql("New")
- end
- context 'default value is a symbol' do
- it "should call the method it describes" do
+ context "AVAILABLE_ATTRIBUTE_METHODS" do
+ SimpleModel::Attributes::ClassMethods::AVAILABLE_ATTRIBUTE_METHODS.each do |m,options|
+ it "should respond to #{m}" do
+ TestInit.respond_to?(m).should be_true
+ end
+ it "should respond to alias #{options[:alias]}" do
+ TestInit.respond_to?(options[:alias]).should be_true
+ end
+ end
+ end
+
+ context '#has_attribute' do
+ before(:all) do
class TestDefault
include SimpleModel::Attributes
- attr_accessor :test
- define_reader_with_options :test, :default => :default_value
+ has_attribute :foo, :default => "foo", :allow_blank => false
+ has_attribute :bar, :default => :default_value
+ has_attribute :fab, :default => :some_symbol
+ has_attribute :hop, :default => :default_hop, :allow_blank => false
+ has_attribute :tip, :default => "2", :initialize => false, :allow_blank => false
+ has_attribute :nap
+ has_attribute :my_array, :default => []
def default_value
+ "bar"
+ end
+
+ def default_hop
+ "hop" if nap
+ end
+ end
+ end
+
+ before(:each) do
+ @default = TestDefault.new
+ end
+
+ it "should define setter method" do
+ @default.respond_to?(:foo=).should be_true
+ end
+
+ it "should define reader/getter method" do
+ @default.respond_to?(:foo).should be_true
+ end
+
+ context ':initialize => false' do
+ it "should not initialize with the default value" do
+ @default.attributes[:tip].should be_nil
+ @default.tip.should eql("2")
+ end
+ context "allow_blank => false"do
+ it "should not initialize, but should set the value on get" do
+ @default.attributes[:tip].should be_nil
+ @default.tip.should eql("2")
+ end
+ end
+ end
+
+ it "should call the method it describe by the default value if it exists" do
+ @default.attributes[:bar].should eql("bar")
+ end
+
+ it "should set the defaul to the supplied symbol, if the method does not exist" do
+ @default.attributes[:fab].should eql(:some_symbol)
+ end
+
+ it "should allow default value to be an empty array" do
+ @default.my_array.should eql([])
+ end
+
+ it "should create a boolean? method for each attribute" do
+ @default.respond_to?(:foo?).should be_true
+ end
+
+ it "should return !blank?" do
+ @default.my_array.should eql([]) # blank array
+ @default.my_array?.should be_false
+ @default.my_array << 1
+ @default.my_array?.should be_true
+ end
+
+ it "should not allow blank if set" do
+ @default.foo.should eql("foo")
+ @default.foo = ""
+ @default.foo.should eql("foo")
+ @default.foo = "not blank"
+ @default.foo.should eql("not blank")
+ end
+
+ it "should try for the default if its blank on get" do
+ @default.hop.blank?.should be_true
+ @default.nap = "yep"
+ @default.hop.should eql("hop")
+ end
+
+
+ end
+
+ context "on get" do
+ it "should perform on_get when set" do
+ class OnGet
+ include SimpleModel::Attributes
+ has_attribute :foo, :on_get => lambda{|obj,attr| (attr.blank? ? obj.send(:foo_default) : attr)}
+
+ def foo_default
"test"
end
end
- default = TestDefault.new
- default.test.should eql("test")
+ new = OnGet.new
+ new.foo.should eql("test")
+ new.foo = "foo"
+ new.foo.should eql("foo")
end
end
-
-end
-describe SimpleModel::Attributes, 'has_booleans' do
- before(:all) do
- class TestBoolean
- include SimpleModel::Attributes
- has_booleans :test
+ context 'if supplied value can be cast' do
+ it "should throw an exception" do
+ class TestThrow
+ include SimpleModel::Attributes
+ has_booleans :boo
+ end
+
+ lambda{TestThrow.new(:boo => [])}.should raise_error(SimpleModel::ArgumentError)
end
+
end
-
- it "should add setter=, getter and getter? methods" do
- methods = TestBoolean.new.methods
- union = methods | [:test, :test=, :test?]
- union.should eql(methods)
+
+
+ after(:all) do
+ Object.send(:remove_const,:TestThrow)
+ Object.send(:remove_const,:OnGet)
+ Object.send(:remove_const,:TestDefault)
+ Object.send(:remove_const,:TestInit)
end
-
-end
-describe SimpleModel::Attributes, 'has_attributes' do
- before(:all) do
- class TestBoolean
- include SimpleModel::Attributes
- has_attributes :my_array, :default => []
- end
- end
-
- it "should allow default value to be an empty array" do
- test = TestBoolean.new
- test.my_array.should eql([])
- end
-
-end
-
+end
\ No newline at end of file