lib/gorillib/receiver.rb in gorillib-0.1.3 vs lib/gorillib/receiver.rb in gorillib-0.1.4
- old
+ new
@@ -131,10 +131,11 @@
elsif hsh.has_key?(attr.to_s) then val = hsh[attr.to_s]
else next ; end
_receive_attr attr, val
end
impose_defaults!(hsh)
+ replace_options!(hsh)
run_after_receivers(hsh)
self
end
# true if the attr is a receiver variable and it has been set
@@ -156,9 +157,28 @@
self.class.receiver_defaults.each do |attr, val|
next if attr_set?(attr)
self.instance_variable_set "@#{attr}", val
end
end
+
+ # class Foo
+ # include Receiver
+ # include Receiver::ActsAsHash
+ # rcvr_accessor :attribute, String, :default => 'okay' :replace => { 'bad' => 'good' }
+ # end
+ #
+ # f = Foo.receive({:attribute => 'bad'})
+ # => #<Foo:0x10156c820 @attribute="good">
+
+ def replace_options!(hsh)
+ self.receiver_attrs.each do |attr, info|
+ val = self.instance_variable_get("@#{attr}")
+ if info[:replace] and info[:replace].has_key? val
+ self.instance_variable_set "@#{attr}", info[:replace][val]
+ end
+ end
+ end
+
def run_after_receivers(hsh)
self.class.after_receivers.each do |after_receiver|
self.instance_exec(hsh, &after_receiver)
end