Permalink
Browse files

use define_method in model instead of method_missing hacks

  • Loading branch information...
1 parent c030c5f commit 7071c29d026f7f9b5b8028676078cd20ef1d760e @siuying committed Oct 7, 2012
Showing with 9 additions and 21 deletions.
  1. +8 −20 lib/nano_store/model.rb
  2. +1 −1 lib/nano_store/version.rb
View
@@ -17,26 +17,6 @@ def delete
raise NanoStoreError, error_ptr[0].description if error_ptr[0]
self
end
-
- def method_missing(method, *args)
- matched = method.to_s.match(/^([^=]+)(=)?$/)
- name = matched[1]
- modifier = matched[2]
-
- if self.class.attributes.include?(name.to_sym)
- if modifier == "="
- if args[0].nil?
- self.info.delete(name.to_sym)
- else
- self.info[name.to_sym] = args[0]
- end
- else
- self.info[name.to_sym]
- end
- else
- super
- end
- end
end
module ModelClassMethods
@@ -59,6 +39,14 @@ def create(data={})
def attribute(name)
@attributes << name
+
+ define_method(name) do |*args, &block|
+ self.info[name]
+ end
+
+ define_method((name + "=").to_sym) do |*args, &block|
+ self.info[name] = args[0]
+ end
end
def attributes
@@ -1,3 +1,3 @@
module NanoStore
- VERSION = "0.3.14"
+ VERSION = "0.4.0"
end

0 comments on commit 7071c29

Please sign in to comment.