Module: Upmin::AutomaticDelegation

Extended by:
ActiveSupport::Concern
Included in:
Action, Model
Defined in:
lib/upmin/automatic_delegation.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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

Delegates missing instance methods to the source model.



6
7
8
9
10
11
12
13
# File 'lib/upmin/automatic_delegation.rb', line 6

def method_missing(method, *args, &block)
  if delegatable?(method)
    self.class.delegate(method, to: :model)
    send(method, *args, &block)
  else
    return super
  end
end

Instance Method Details

#delegatable?(method) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/upmin/automatic_delegation.rb', line 15

def delegatable?(method)
  return model.respond_to?(method)
end

#delegated?(method) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/upmin/automatic_delegation.rb', line 19

def delegated?(method)
  return self.class.delegated?(method)
end

#method(method_name) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/upmin/automatic_delegation.rb', line 27

def method(method_name)
  if delegated?(method_name)
    return model.method(method_name)
  else
    return super(method_name)
  end
rescue NameError => e
  if delegatable?(method_name)
    self.class.delegate(method_name, to: :model)
    return method(method_name)
  else
    super(method_name)
  end
end

#respond_to?(method) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/upmin/automatic_delegation.rb', line 23

def respond_to?(method)
  super || delegatable?(method)
end