Module: AutoDemeter

Defined in:
lib/auto_demeter/methods.rb,
lib/auto_demeter/version.rb

Constant Summary collapse

VERSION =
'0.0.12'

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/auto_demeter/methods.rb', line 42

def method_missing(method_id, *args, &block)
  begin
    super
  rescue NoMethodError, NameError => e
    if match_data=method_id.to_s.match(reflected_children_regex)
      association_name=self.respond_to?(match_data[1]) ? match_data[1] : "#{self.class.name.underscore}_#{match_data[1]}"
      begin
        if association=send(association_name)
          association.send(match_data[2], *args, &block)
        else
          nil
        end
      rescue Exception
        raise e
      end
    else
      raise
    end
  end
end

Instance Method Details

#respond_through_association?(method_id) ⇒ Boolean

Returns:

  • (Boolean)


21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/auto_demeter/methods.rb', line 21

def respond_through_association?(method_id)
  if children_names && (match_data=method_id.to_s.match(reflected_children_regex)) && match_data[1].present?
    association_name=(self.methods.include?(match_data[1].intern) || self.methods.include?(match_data[1])) ? match_data[1] : "#{self.class.name.underscore}_#{match_data[1]}"
    begin
      if association=send(association_name)
        association.respond_to?(match_data[2])
      elsif association.nil?
        association_name.camelize.constantize.new.respond_to?(match_data[2])
      end
    rescue
      false
    end
  else
    false
  end
end

#respond_to?(method_id, public = false) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/auto_demeter/methods.rb', line 38

def respond_to?(method_id, public=false)
  super || (method_id != :base_name && respond_through_association?(method_id))
end