Module: Demeter::InstanceMethods

Defined in:
lib/demeter.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *attrs, &block) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/demeter.rb', line 16

def method_missing(method_name, *attrs, &block)
  object_method_name = method_name.to_s
  object_name = self.class.demeter_names.find {|name| object_method_name =~ /^#{name}_/ }

  return super unless object_name

  object_method_name.gsub!(/^#{object_name}_/, "")

  instance_eval <<-TXT
    def #{method_name}                                          # def address_street
      #{object_name}.#{object_method_name} if #{object_name}    #   address.street
    end                                                         # end
  TXT

  send(method_name)
end

Instance Method Details

#respond_to?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
36
37
38
39
40
41
42
# File 'lib/demeter.rb', line 33

def respond_to?(method_name, include_private = false)
  object_method_name = method_name.to_s
  object_name = self.class.demeter_names.find {|name| object_method_name =~ /^#{name}_/ }

  if object_name && (object = send(object_name))
    object.respond_to?(object_method_name.gsub(/^#{Regexp.escape(object_name.to_s)}_/, ""))
  else
    super
  end
end