Module: DirtySeed::MethodMissingHelper

Included in:
Association, Attribute, Model
Defined in:
lib/dirty_seed/method_missing_helper.rb

Overview

Forwards missing method to another object if it matches

Instance Method Summary collapse

Instance Method Details

#define_addressee(addressee_key) ⇒ void

This method returns an undefined value.

Defines addressee method

Parameters:

  • addressee_key (Object)


19
20
21
22
23
# File 'lib/dirty_seed/method_missing_helper.rb', line 19

def define_addressee(addressee_key)
  define_method :addressee do
    public_send(addressee_key)
  end
end

#define_method_missingvoid

This method returns an undefined value.

Defines missing_method method so it returns the adressee or calls super

Examples:

calling #name on a Model instance will call name on its @model object


29
30
31
32
33
34
35
# File 'lib/dirty_seed/method_missing_helper.rb', line 29

def define_method_missing
  define_method :method_missing do |method_name, *args, &block|
    return super(method_name, *args, &block) unless addressee.respond_to?(method_name)

    addressee.public_send(method_name, *args, &block)
  end
end

#define_respond_to_missing?void

This method returns an undefined value.

Defines respond_to_missing? method to matches the method_missing behavior



39
40
41
42
43
44
45
46
47
# File 'lib/dirty_seed/method_missing_helper.rb', line 39

def define_respond_to_missing?
  define_method :respond_to_missing? do |method_name, _include_private = false|
    # :nocov:
    return super(method_name, _include_private = false) unless addressee.respond_to?(method_name)

    true
    # :nocov:
  end
end

#forward_missing_methods_to(addressee_key) ⇒ Object

Defines missing_method and respond_to_missing? methods

Parameters:

  • addressee_key (Object)

Returns:

  • (Object)

Raises:

  • (NoMethodError)


10
11
12
13
14
# File 'lib/dirty_seed/method_missing_helper.rb', line 10

def forward_missing_methods_to(addressee_key)
  define_addressee(addressee_key)
  define_method_missing
  define_respond_to_missing?
end