lib/matic.rb in matic-0.1.1 vs lib/matic.rb in matic-0.2.0
- old
+ new
@@ -7,54 +7,44 @@
module ClassMethods
def collection_name
self.name.tableize
end
- def field(attr_name)
- generate_attribute_methods(attr_name)
+ def fields(*attrs)
+ if attrs.first.is_a? Hash
+ attrs.first.each { |k, v| define_accessor(k, v) }
+ define_attribute_methods(attrs.first.keys)
+ else
+ attrs.each { |k, v| define_accessor(k, v) }
+ define_attribute_methods(attrs)
+ end
+ end
+ private
+
+ def define_accessor(attr_name, attr_field=nil)
+ attr_field ||= attr_name
+
define_method(attr_name) do
- self[attr_name.to_s]
+ self[attr_field.to_s]
end
define_method("#{attr_name}=") do |val|
- unless val == self[attr_name.to_s]
+ unless val == self[attr_field.to_s]
eval("#{attr_name}_will_change!")
end
- self[attr_name.to_s] = val
+ self[attr_field.to_s] = val
end
end
-
- private
-
- def generate_attribute_methods(attr_name)
- attribute_method_matchers.each do |matcher|
- method_name = matcher.method_name(attr_name)
-
- generated_attribute_methods.module_eval <<-STR, __FILE__, __LINE__ + 1
- def #{method_name}(*args)
- send(:#{matcher.method_missing_target}, '#{attr_name}', *args)
- end
- STR
- end
- end
end
def insert(opts={})
clear_changes if super
end
- def insert!(opts={})
- insert(opts.merge(:safe => true))
- end
-
def update(opts={}, update_doc=@doc)
clear_changes if super
- end
-
- def update!(opts={}, update_doc=@doc)
- update(opts.merge(:safe => true), update_doc)
end
def save
is_new ? insert : update
end