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

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

Constant Summary collapse

DELEGATE =
[:hash, :eql?].freeze

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of LazyCollectorStep.



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

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



38
39
40
41
42
43
44
# File 'lib/middleman-core/core_extensions/collections/lazy_step.rb', line 38

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



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

def leaves
  @parent.leaves
end

#value(ctx = nil) ⇒ Object



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

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

  original_block = @block

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

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