Class: Middleman::CoreExtensions::Collections::LazyCollectorStep

Inherits:
BasicObject
Defined in:
middleman-core/lib/middleman-core/core_extensions/collections/lazy_step.rb

Constant Summary collapse

DELEGATE =
%i[hash eql?].freeze

Instance Method Summary collapse

Constructor Details

#initialize(name, args, block, parent = nil) ⇒ LazyCollectorStep

Returns a new instance of LazyCollectorStep.


9
10
11
12
13
14
15
16
17
18
# File 'middleman-core/lib/middleman-core/core_extensions/collections/lazy_step.rb', line 9

def initialize(name, args, block, parent = nil)
  @name = name
  @args = args
  @block = block

  @parent = parent
  @result = nil

  leaves << self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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


42
43
44
45
46
47
48
# File 'middleman-core/lib/middleman-core/core_extensions/collections/lazy_step.rb', line 42

def method_missing(name, *args, &block)
  return ::Kernel.send(name, *args, &block) if DELEGATE.include? name

  leaves.delete self

  LazyCollectorStep.new(name, args, block, self)
end

Instance Method Details

#leavesObject


20
21
22
# File 'middleman-core/lib/middleman-core/core_extensions/collections/lazy_step.rb', line 20

def leaves
  @parent.leaves
end

#value(ctx = nil) ⇒ Object


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'middleman-core/lib/middleman-core/core_extensions/collections/lazy_step.rb', line 24

def value(ctx = nil)
  data = @parent.value(ctx)

  original_block = @block

  if original_block
    b = if ctx
          ::Proc.new do |*args|
            ctx.instance_exec(*args, &original_block)
          end
        else
          original_block
        end
  end

  data.send(@name, *@args.deep_dup, &b)
end