Class: Tina4::Frond::LoopContext
- Inherits:
-
Object
- Object
- Tina4::Frond::LoopContext
- Defined in:
- lib/tina4/frond.rb
Overview
-- Lazy context overlay for for-loops (avoids full Hash#dup) --
Instance Method Summary collapse
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
- #dup ⇒ Object
- #each(&block) ⇒ Object
- #fetch(key, *args, &block) ⇒ Object
-
#initialize(parent) ⇒ LoopContext
constructor
A new instance of LoopContext.
- #is_a?(klass) ⇒ Boolean
- #key?(key) ⇒ Boolean (also: #include?, #has_key?)
- #keys ⇒ Object
- #merge(other) ⇒ Object
- #merge!(other) ⇒ Object
- #respond_to_missing?(name, include_private = false) ⇒ Boolean
- #to_h ⇒ Object
Constructor Details
#initialize(parent) ⇒ LoopContext
Returns a new instance of LoopContext.
139 140 141 142 |
# File 'lib/tina4/frond.rb', line 139 def initialize(parent) @parent = parent @local = {} end |
Instance Method Details
#[](key) ⇒ Object
144 145 146 |
# File 'lib/tina4/frond.rb', line 144 def [](key) @local.key?(key) ? @local[key] : @parent[key] end |
#[]=(key, value) ⇒ Object
148 149 150 |
# File 'lib/tina4/frond.rb', line 148 def []=(key, value) @local[key] = value end |
#dup ⇒ Object
183 184 185 186 187 |
# File 'lib/tina4/frond.rb', line 183 def dup copy = LoopContext.new(@parent) @local.each { |k, v| copy[k] = v } copy end |
#each(&block) ⇒ Object
195 196 197 |
# File 'lib/tina4/frond.rb', line 195 def each(&block) to_h.each(&block) end |
#fetch(key, *args, &block) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/tina4/frond.rb', line 158 def fetch(key, *args, &block) if @local.key?(key) @local[key] elsif @parent.key?(key) @parent[key] elsif block yield key elsif !args.empty? args[0] else raise KeyError, "key not found: #{key.inspect}" end end |
#is_a?(klass) ⇒ Boolean
203 204 205 |
# File 'lib/tina4/frond.rb', line 203 def is_a?(klass) klass == Hash || super end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?
152 153 154 |
# File 'lib/tina4/frond.rb', line 152 def key?(key) @local.key?(key) || @parent.key?(key) end |
#keys ⇒ Object
207 208 209 |
# File 'lib/tina4/frond.rb', line 207 def keys (@parent.is_a?(LoopContext) ? @parent.keys : @parent.keys) | @local.keys end |
#merge(other) ⇒ Object
172 173 174 175 176 |
# File 'lib/tina4/frond.rb', line 172 def merge(other) dup_hash = to_h dup_hash.merge!(other) dup_hash end |
#merge!(other) ⇒ Object
178 179 180 181 |
# File 'lib/tina4/frond.rb', line 178 def merge!(other) other.each { |k, v| @local[k] = v } self end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
199 200 201 |
# File 'lib/tina4/frond.rb', line 199 def respond_to_missing?(name, include_private = false) @parent.respond_to?(name, include_private) || super end |
#to_h ⇒ Object
189 190 191 192 193 |
# File 'lib/tina4/frond.rb', line 189 def to_h h = @parent.is_a?(LoopContext) ? @parent.to_h : @parent.dup @local.each { |k, v| h[k] = v } h end |