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.
229 230 231 232 |
# File 'lib/tina4/frond.rb', line 229 def initialize(parent) @parent = parent @local = {} end |
Instance Method Details
#[](key) ⇒ Object
234 235 236 |
# File 'lib/tina4/frond.rb', line 234 def [](key) @local.key?(key) ? @local[key] : @parent[key] end |
#[]=(key, value) ⇒ Object
238 239 240 |
# File 'lib/tina4/frond.rb', line 238 def []=(key, value) @local[key] = value end |
#dup ⇒ Object
273 274 275 276 277 |
# File 'lib/tina4/frond.rb', line 273 def dup copy = LoopContext.new(@parent) @local.each { |k, v| copy[k] = v } copy end |
#each(&block) ⇒ Object
285 286 287 |
# File 'lib/tina4/frond.rb', line 285 def each(&block) to_h.each(&block) end |
#fetch(key, *args, &block) ⇒ Object
248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/tina4/frond.rb', line 248 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
293 294 295 |
# File 'lib/tina4/frond.rb', line 293 def is_a?(klass) klass == Hash || super end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?
242 243 244 |
# File 'lib/tina4/frond.rb', line 242 def key?(key) @local.key?(key) || @parent.key?(key) end |
#keys ⇒ Object
297 298 299 |
# File 'lib/tina4/frond.rb', line 297 def keys (@parent.is_a?(LoopContext) ? @parent.keys : @parent.keys) | @local.keys end |
#merge(other) ⇒ Object
262 263 264 265 266 |
# File 'lib/tina4/frond.rb', line 262 def merge(other) dup_hash = to_h dup_hash.merge!(other) dup_hash end |
#merge!(other) ⇒ Object
268 269 270 271 |
# File 'lib/tina4/frond.rb', line 268 def merge!(other) other.each { |k, v| @local[k] = v } self end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
289 290 291 |
# File 'lib/tina4/frond.rb', line 289 def respond_to_missing?(name, include_private = false) @parent.respond_to?(name, include_private) || super end |
#to_h ⇒ Object
279 280 281 282 283 |
# File 'lib/tina4/frond.rb', line 279 def to_h h = @parent.is_a?(LoopContext) ? @parent.to_h : @parent.dup @local.each { |k, v| h[k] = v } h end |