Skip to content

Commit

Permalink
Add an attributes= method that accepts a hash of attributes. The valu…
Browse files Browse the repository at this point in the history
…e side of the hash may be a proc. If so, it's evaluated at the time of the attributes= call. Using this so I can have a hash of defaults to reuse.
  • Loading branch information
Brian Cooke committed Sep 27, 2008
1 parent 2ad4027 commit 5f85a6f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/populator/record.rb
Expand Up @@ -43,6 +43,13 @@ def attribute_values
end
end

def attributes=(values_hash)
values_hash.each_pair do |key, value|
value = value.call if value.is_a?(Proc)
self.send((key.to_s + "=").to_sym, value)
end
end

private

def method_missing(sym, *args, &block)
Expand Down
13 changes: 13 additions & 0 deletions spec/populator/record_spec.rb
Expand Up @@ -58,4 +58,17 @@
Product.stubs(:column_names).returns(['id', 'foo'])
Populator::Record.new(Product, 1).foo.should == 'Product'
end

it "should allow set via attributes hash" do
record = Populator::Record.new(Product, 1)
record.attributes = {:stock => 2..5}
record.stock.should >= 2
record.stock.should <= 5
end

it "should take a proc object via attributes hash" do
record = Populator::Record.new(Product, 1)
record.attributes = {:stock => lambda {15}}
record.stock.should == 15
end
end

0 comments on commit 5f85a6f

Please sign in to comment.