Class: Arq::ActionModuleHash

Inherits:
Hash
  • Object
show all
Defined in:
lib/arq/action_module_hash.rb

Overview

Allows for dot-accessing of modules and running of actions. Child modules are automatically wrapped and actions are called.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mod, action_inst) ⇒ ActionModuleHash

Returns a new instance of ActionModuleHash.



19
20
21
22
23
24
# File 'lib/arq/action_module_hash.rb', line 19

def initialize(mod, action_inst)
  super()

  @module = mod
  @action_inst = action_inst
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/arq/action_module_hash.rb', line 26

def method_missing(method, *args, &block)
  # Format method as module path.
  formatted = method.to_s.camelize

  # Attempt to find object.
  obj = if Object.const_defined?(formatted)
          Object.const_get(formatted)
        else
          return super
        end

  self.class.from(obj)
end

Class Method Details

.from(obj, action_inst) ⇒ Object

Calls action or wraps in hash class if module.



8
9
10
11
12
13
14
15
16
17
# File 'lib/arq/action_module_hash.rb', line 8

def self.from(obj, action_inst)
  case obj
  when Arq::Action
    action_inst.call_other(obj)
  when Module
    new(obj, action_inst)
  else
    raise "Object must be an Action or Module"
  end
end

Instance Method Details

#respond_to_missing?(method, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/arq/action_module_hash.rb', line 40

def respond_to_missing?(method, include_private = false)
  @module.const_defined?(method.to_s.camelize.to_sym) || super
end