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.
211 212 213 214 |
# File 'lib/tina4/frond.rb', line 211 def initialize(parent) @parent = parent @local = {} end |
Instance Method Details
#[](key) ⇒ Object
216 217 218 |
# File 'lib/tina4/frond.rb', line 216 def [](key) @local.key?(key) ? @local[key] : @parent[key] end |
#[]=(key, value) ⇒ Object
220 221 222 |
# File 'lib/tina4/frond.rb', line 220 def []=(key, value) @local[key] = value end |
#dup ⇒ Object
255 256 257 258 259 |
# File 'lib/tina4/frond.rb', line 255 def dup copy = LoopContext.new(@parent) @local.each { |k, v| copy[k] = v } copy end |
#each(&block) ⇒ Object
267 268 269 |
# File 'lib/tina4/frond.rb', line 267 def each(&block) to_h.each(&block) end |
#fetch(key, *args, &block) ⇒ Object
230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/tina4/frond.rb', line 230 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
275 276 277 |
# File 'lib/tina4/frond.rb', line 275 def is_a?(klass) klass == Hash || super end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?
224 225 226 |
# File 'lib/tina4/frond.rb', line 224 def key?(key) @local.key?(key) || @parent.key?(key) end |
#keys ⇒ Object
279 280 281 |
# File 'lib/tina4/frond.rb', line 279 def keys (@parent.is_a?(LoopContext) ? @parent.keys : @parent.keys) | @local.keys end |
#merge(other) ⇒ Object
244 245 246 247 248 |
# File 'lib/tina4/frond.rb', line 244 def merge(other) dup_hash = to_h dup_hash.merge!(other) dup_hash end |
#merge!(other) ⇒ Object
250 251 252 253 |
# File 'lib/tina4/frond.rb', line 250 def merge!(other) other.each { |k, v| @local[k] = v } self end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
271 272 273 |
# File 'lib/tina4/frond.rb', line 271 def respond_to_missing?(name, include_private = false) @parent.respond_to?(name, include_private) || super end |
#to_h ⇒ Object
261 262 263 264 265 |
# File 'lib/tina4/frond.rb', line 261 def to_h h = @parent.is_a?(LoopContext) ? @parent.to_h : @parent.dup @local.each { |k, v| h[k] = v } h end |