Module: Yarr::Command::Concern::ASTDigger

Included in:
Evaluate, Fake, List, Ri, URLEvaluate
Defined in:
lib/yarr/command/concern/ast_digger.rb

Overview

Extend with ASTDigger to gain a digger class method. Diggers can be used to define deep accessors on AST

Instance Method Summary collapse

Instance Method Details

#digger(name, ast_name = name, &block) {|value| ... } ⇒ Object

Defines a new digger method

Parameters:

  • name (Symbol)

    name of the digger

  • ast_name (String) (defaults to: name)

    AST key

  • block (block)

    An optional transformation block applied to the digged value

Yield Parameters:

  • value (Object)

    the value digged


13
14
15
16
17
18
19
20
21
22
23
# File 'lib/yarr/command/concern/ast_digger.rb', line 13

def digger(name, ast_name = name, &block)
  define_method(name) do
    @digger ||= {}

    @digger[name] ||=
      begin
        block = -> value { value } unless block_given?
        instance_exec(ast.dig_deep(ast_name), &block)
      end
  end
end