Class: VcShortcut::ChainManager
- Inherits:
-
Object
- Object
- VcShortcut::ChainManager
- Defined in:
- lib/vc_shortcut/chain_manager.rb
Constant Summary collapse
- TREE =
{}
Instance Method Summary collapse
-
#initialize(shortcut, process, find, view_context) ⇒ ChainManager
constructor
A new instance of ChainManager.
- #method_missing(method, *args, **kwargs, &block) ⇒ Object
Constructor Details
#initialize(shortcut, process, find, view_context) ⇒ ChainManager
Returns a new instance of ChainManager.
5 6 7 8 9 10 |
# File 'lib/vc_shortcut/chain_manager.rb', line 5 def initialize(shortcut, process, find, view_context) @tree = TREE[shortcut] ||= {} @process = process @find = find @context = ChainContext.new(view_context) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, **kwargs, &block) ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/vc_shortcut/chain_manager.rb', line 12 def method_missing(method, *args, **kwargs, &block) @context.chain << method # If we haven't seen this path before, let's attempt constantization if @tree[method].nil? result = @find.call(@context) if result == :has_more # We matched with a module @tree[method] = {} elsif result # We matched with a leaf @tree[method] = result else raise NameError, "Cannot find a component or module matching. Chain: #{@context.chain.join('.')}" end end leaf_or_subtree = @tree[method] # We are at a module. Continue chaining if leaf_or_subtree.is_a?(Hash) @tree = leaf_or_subtree return self end # Otherwise, we are at the leaf node (i.e a component) @context.component = leaf_or_subtree @context.call_args = args @context.call_kwargs = kwargs @context.call_block = block @process.call(@context) end |