Module: HasMeta::DynamicMethods

Defined in:
lib/has_meta/dynamic_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

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.meta_attributes.select { |x| method.match /^#{x}(_id)?=?$/ }.pop
  if attribute
    object = self.class.find_object_from attribute            
    if method =~ /=$/ # setter
      object ? meta_set!(:"#{attribute}_id", args.first.try(:id) || args.first) : meta_set!(attribute, args.first)
    else # getter
      if object
        method =~ /_id$/ ? meta_get(:"#{attribute}_id") : object.find_by_id(meta_get(:"#{attribute}_id"))
      else
        meta_get(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

Returns:

  • (Boolean)


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.meta_attributes.select { |x| method.match /^#{x}(_id)?=?$/ }.pop
  if attribute
    self.class.find_object_from(attribute) ? true : !method.match(/^#{attribute}=?$/).nil?
  else
    super
  end
end