Class: Twig::Runtime::Context
- Inherits:
-
Hash
- Object
- Hash
- Twig::Runtime::Context
- Defined in:
- lib/twig/runtime/context.rb
Instance Attribute Summary collapse
-
#call_context ⇒ Object
readonly
Returns the value of attribute call_context.
Class Method Summary collapse
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #buffer_and_return ⇒ Object
- #clear ⇒ Object
- #dup ⇒ Object
- #each ⇒ Object
- #get(name) ⇒ Object
- #has?(name) ⇒ Boolean
-
#initialize(initial_context = {}, output_buffer: nil, call_context: nil) ⇒ Context
constructor
A new instance of Context.
- #keep!(keys) ⇒ Object
- #merge(other, overwrite: true) ⇒ Object
- #merge!(other, overwrite: true) ⇒ Object
- #only(other) ⇒ Object
- #original_buffer ⇒ Object
- #output_buffer ⇒ Object
- #pop_output_buffer ⇒ Object
- #pop_stack(new_only = false) ⇒ Object
- #push_output_buffer ⇒ Object
- #push_stack ⇒ Object
- #remove!(*keys) ⇒ Object
Constructor Details
#initialize(initial_context = {}, output_buffer: nil, call_context: nil) ⇒ Context
Returns a new instance of Context.
8 9 10 11 12 13 14 15 16 17 |
# File 'lib/twig/runtime/context.rb', line 8 def initialize(initial_context = {}, output_buffer: nil, call_context: nil) super() output_buffer ||= OutputBuffer.new @output_buffer_stack = [output_buffer] @call_context = call_context @popping = false merge!(initial_context) end |
Instance Attribute Details
#call_context ⇒ Object (readonly)
Returns the value of attribute call_context.
6 7 8 |
# File 'lib/twig/runtime/context.rb', line 6 def call_context @call_context end |
Class Method Details
.from(context = {}, output_buffer: nil, call_context: nil) ⇒ Context
147 148 149 150 151 152 153 |
# File 'lib/twig/runtime/context.rb', line 147 def self.from(context = {}, output_buffer: nil, call_context: nil) if context.is_a?(Context) context else new(context, output_buffer:, call_context:) end end |
Instance Method Details
#[](key) ⇒ Object
124 125 126 |
# File 'lib/twig/runtime/context.rb', line 124 def [](key) super(key.to_sym) end |
#[]=(key, value) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/twig/runtime/context.rb', line 128 def []=(key, value) super if popping key = key.to_sym if (frame = stack.last) super and return if frame[:replace].key?(key) || frame[:remove].include?(key) if key?(key) frame[:replace][key] = self[key] else frame[:remove].push(key) end end super end |
#buffer_and_return ⇒ Object
106 107 108 109 110 |
# File 'lib/twig/runtime/context.rb', line 106 def buffer_and_return(&) push_output_buffer yield pop_output_buffer end |
#clear ⇒ Object
112 113 114 115 116 117 118 |
# File 'lib/twig/runtime/context.rb', line 112 def clear # Copy everything to the replace stack merge!(self) # Clear the hash super end |
#dup ⇒ Object
120 121 122 |
# File 'lib/twig/runtime/context.rb', line 120 def dup self.class.new(to_h, call_context:, output_buffer:) end |
#each ⇒ Object
155 156 157 |
# File 'lib/twig/runtime/context.rb', line 155 def each(...) to_h.each(...) end |
#get(name) ⇒ Object
63 64 65 66 67 68 69 70 71 |
# File 'lib/twig/runtime/context.rb', line 63 def get(name) if name[0] == '@' call_context.instance_variable_get(name) elsif key?(name) self[name] elsif call_context.respond_to?(name) call_context.send(name) end end |
#has?(name) ⇒ Boolean
55 56 57 58 59 60 61 |
# File 'lib/twig/runtime/context.rb', line 55 def has?(name) if name[0] == '@' call_context.instance_variable_defined?(name) else key?(name) || call_context.respond_to?(name) end end |
#keep!(keys) ⇒ Object
39 40 41 |
# File 'lib/twig/runtime/context.rb', line 39 def keep!(keys) (self.keys - keys).each { |k| delete(k) } end |
#merge(other, overwrite: true) ⇒ Object
47 48 49 |
# File 'lib/twig/runtime/context.rb', line 47 def merge(other, overwrite: true) self.class.new(self, call_context:, output_buffer:).merge!(other, overwrite:) end |
#merge!(other, overwrite: true) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/twig/runtime/context.rb', line 23 def merge!(other, overwrite: true) return if other == [] unless other.is_a?(Hash) raise "Must merge! another Hash, given #{other.class.name}" end other.each do |k, v| if overwrite || !key?(k) self[k] = v end end self end |
#only(other) ⇒ Object
51 52 53 |
# File 'lib/twig/runtime/context.rb', line 51 def only(other) self.class.new(other, call_context:, output_buffer:) end |
#original_buffer ⇒ Object
19 20 21 |
# File 'lib/twig/runtime/context.rb', line 19 def original_buffer output_buffer_stack.first end |
#output_buffer ⇒ Object
94 95 96 |
# File 'lib/twig/runtime/context.rb', line 94 def output_buffer output_buffer_stack.last end |
#pop_output_buffer ⇒ Object
102 103 104 |
# File 'lib/twig/runtime/context.rb', line 102 def pop_output_buffer output_buffer_stack.pop end |
#pop_stack(new_only = false) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/twig/runtime/context.rb', line 77 def pop_stack(new_only = false) return unless stack.last @popping = true frame = stack.pop frame[:remove].each do |k| delete(k) end unless new_only frame[:replace].each { |k, v| self[k] = v } end @popping = false end |
#push_output_buffer ⇒ Object
98 99 100 |
# File 'lib/twig/runtime/context.rb', line 98 def push_output_buffer output_buffer_stack.push(OutputBuffer.new) end |
#push_stack ⇒ Object
73 74 75 |
# File 'lib/twig/runtime/context.rb', line 73 def push_stack stack.push({ remove: [], replace: {} }) end |
#remove!(*keys) ⇒ Object
43 44 45 |
# File 'lib/twig/runtime/context.rb', line 43 def remove!(*keys) keys.each { |k| delete(k) } end |