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

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

Constant Summary

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



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

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
37
38
# File 'lib/middleman-core/core_extensions/collections/lazy_step.rb', line 22

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