Class: Core::Extension::Behavior

Inherits:
Object
  • Object
show all
Defined in:
lib/core/extension/behavior.rb

Overview

public

Defines behavior that can be applied to objects. Behavior will only be applied to each object once by default.

force - If true, behavior will be applied even if it has already been applied to the including object.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(force: false, &block) ⇒ Behavior

Returns a new instance of Behavior.



10
11
12
13
# File 'lib/core/extension/behavior.rb', line 10

def initialize(force: false, &block)
  @force = force
  @module = build_module(&block)
end

Class Method Details

.conditional_keyword_arguments(arguments, callable) ⇒ Object



66
67
68
69
70
71
72
# File 'lib/core/extension/behavior.rb', line 66

def conditional_keyword_arguments(arguments, callable)
  arguments.keep_if { |key, _value|
    callable.parameters.any? { |type, name|
      (type == :key || type == :keyreq || type == :keyrest) && name == key
    }
  }
end

Instance Method Details

#apply_extend(object) ⇒ Object

public

Applies this behavior to an object via extend.



17
18
19
20
21
# File 'lib/core/extension/behavior.rb', line 17

def apply_extend(object)
  if force? || unapplied?(object)
    object.extend(@module)
  end
end

#apply_include(object) ⇒ Object

public

Applies this behavior to an object via include.



25
26
27
28
29
# File 'lib/core/extension/behavior.rb', line 25

def apply_include(object)
  if force? || unapplied?(object)
    object.include(@module)
  end
end