lib/morph.rb in morph-0.1.2 vs lib/morph.rb in morph-0.1.3
- old
+ new
@@ -1,7 +1,7 @@
module Morph
- VERSION = "0.1.2"
+ VERSION = "0.1.3"
def self.included(base)
base.extend ClassMethods
base.send(:include, InstanceMethods)
end
@@ -56,12 +56,36 @@
end
end
module InstanceMethods
- def morph label, value
- attribute = self.class.convert_to_morph_method_name label
- send("#{attribute}=".to_sym, value)
+ #
+ # Set attribute value(s). Adds accessor methods to class if
+ # they are not already present.
+ #
+ # Can be called with a +string+ and a value, a +symbol+ and a value,
+ # or with a +hash+ of attribute to value pairs. For example.
+ #
+ # require 'rubygems'; require 'morph'
+ #
+ # class Order; include Morph; end
+ #
+ # order = Order.new
+ # order.morph :drink => 'tea', :sugars => 2, 'milk' => 'yes please'
+ # order.morph 'Payment type:', 'will wash dishes'
+ # order.morph :lemon, false
+ #
+ # p order # -> #<Order:0x33c50c @lemon=false, @milk="yes please",
+ # @payment_type="will wash dishes", @sugars=2, @drink="tea">
+ #
+ def morph attributes, value=nil
+ if attributes.is_a? Hash
+ attributes.each { |a, v| morph(a, v) }
+ else
+ label = attributes
+ attribute = label.is_a?(String) ? self.class.convert_to_morph_method_name(label) : label
+ send("#{attribute}=".to_sym, value)
+ end
end
def method_missing symbol, *args
is_writer = symbol.to_s =~ /=\Z/