Class: Babl::Rendering::Context

Inherits:
Object
  • Object
show all
Defined in:
lib/babl/rendering/context.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(freelist = []) ⇒ Context

Returns a new instance of Context.



10
11
12
# File 'lib/babl/rendering/context.rb', line 10

def initialize(freelist = [])
    @freelist = freelist
end

Instance Attribute Details

#freelistObject

Returns the value of attribute freelist.



8
9
10
# File 'lib/babl/rendering/context.rb', line 8

def freelist
  @freelist
end

#keyObject

Returns the value of attribute key.



8
9
10
# File 'lib/babl/rendering/context.rb', line 8

def key
  @key
end

#objectObject

Returns the value of attribute object.



8
9
10
# File 'lib/babl/rendering/context.rb', line 8

def object
  @object
end

#parentObject

Returns the value of attribute parent.



8
9
10
# File 'lib/babl/rendering/context.rb', line 8

def parent
  @parent
end

#pinsObject

Returns the value of attribute pins.



8
9
10
# File 'lib/babl/rendering/context.rb', line 8

def pins
  @pins
end

Instance Method Details

#create_pin(ref) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/babl/rendering/context.rb', line 60

def create_pin(ref)
    new_frame = @freelist.pop || Context.new(freelist)

    new_frame.parent = parent
    new_frame.object = object
    new_frame.key = key
    new_frame.pins = (pins || Utils::Hash::EMPTY).merge(ref => new_frame)

    yield new_frame
ensure
    @freelist << new_frame
end

#formatted_stack(*additional_stack_items) ⇒ Object



78
79
80
81
# File 'lib/babl/rendering/context.rb', line 78

def formatted_stack(*additional_stack_items)
    stack_trace = ([:__root__] + stack + additional_stack_items).join('.')
    "BABL @ #{stack_trace}"
end

#goto_pin(ref) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/babl/rendering/context.rb', line 43

def goto_pin(ref)
    current_pins = pins || Utils::Hash::EMPTY
    pin_frame = current_pins[ref]
    raise Errors::RenderingError, 'Pin reference cannot be used here' unless pin_frame

    new_frame = @freelist.pop || Context.new(freelist)

    new_frame.object = pin_frame.object
    new_frame.parent = pin_frame.parent
    new_frame.key = pin_frame.key
    new_frame.pins = (pin_frame.pins || Utils::Hash::EMPTY).merge(current_pins)

    yield new_frame
ensure
    @freelist << new_frame
end

#move_backwardObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/babl/rendering/context.rb', line 27

def move_backward
    new_frame = @freelist.pop || Context.new(freelist)

    parent_frame = parent
    raise Errors::RenderingError, 'There is no parent element' unless parent_frame

    new_frame.object = parent_frame.object
    new_frame.parent = parent_frame.parent
    new_frame.key = parent_frame.key
    new_frame.pins = pins

    yield new_frame
ensure
    @freelist << new_frame
end

#move_forward(new_object, key) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/babl/rendering/context.rb', line 14

def move_forward(new_object, key)
    new_frame = @freelist.pop || Context.new(freelist)

    new_frame.object = new_object
    new_frame.key = key
    new_frame.parent = self
    new_frame.pins = pins

    yield new_frame
ensure
    @freelist << new_frame
end

#stackObject



73
74
75
76
# File 'lib/babl/rendering/context.rb', line 73

def stack
    parent_frame = parent
    (parent_frame ? parent_frame.stack : Utils::Array::EMPTY) + [key].compact
end