Module: HasMeta::DynamicMethods
- Defined in:
- lib/has_meta/dynamic_methods.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Instance Method Summary collapse
- #method_missing(method, *args, &block) ⇒ Object
-
#respond_to?(method, include_private = false) ⇒ Boolean
TODO: refactor this.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/has_meta/dynamic_methods.rb', line 17 def method_missing method, *args, &block attribute = self..select { |x| method.match /^#{x}(_id)?=?$/ }.pop if attribute object = self.class.find_object_from attribute if method =~ /=$/ # setter object ? (:"#{attribute}_id", args.first.try(:id) || args.first) : (attribute, args.first) else # getter if object method =~ /_id$/ ? (:"#{attribute}_id") : object.find_by_id((:"#{attribute}_id")) else (attribute) end end else super end end |
Class Method Details
.included(base) ⇒ Object
3 4 5 |
# File 'lib/has_meta/dynamic_methods.rb', line 3 def self.included base base.extend ClassMethods end |
Instance Method Details
#respond_to?(method, include_private = false) ⇒ Boolean
TODO: refactor this
8 9 10 11 12 13 14 15 |
# File 'lib/has_meta/dynamic_methods.rb', line 8 def respond_to? method, include_private=false attribute = self..select { |x| method.match /^#{x}(_id)?=?$/ }.pop if attribute self.class.find_object_from(attribute) ? true : !method.match(/^#{attribute}=?$/).nil? else super end end |