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.
80 81 82 83 |
# File 'lib/tina4/frond.rb', line 80 def initialize(parent) @parent = parent @local = {} end |
Instance Method Details
#[](key) ⇒ Object
85 86 87 |
# File 'lib/tina4/frond.rb', line 85 def [](key) @local.key?(key) ? @local[key] : @parent[key] end |
#[]=(key, value) ⇒ Object
89 90 91 |
# File 'lib/tina4/frond.rb', line 89 def []=(key, value) @local[key] = value end |
#dup ⇒ Object
124 125 126 127 128 |
# File 'lib/tina4/frond.rb', line 124 def dup copy = LoopContext.new(@parent) @local.each { |k, v| copy[k] = v } copy end |
#each(&block) ⇒ Object
136 137 138 |
# File 'lib/tina4/frond.rb', line 136 def each(&block) to_h.each(&block) end |
#fetch(key, *args, &block) ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/tina4/frond.rb', line 99 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
144 145 146 |
# File 'lib/tina4/frond.rb', line 144 def is_a?(klass) klass == Hash || super end |
#key?(key) ⇒ Boolean Also known as: include?, has_key?
93 94 95 |
# File 'lib/tina4/frond.rb', line 93 def key?(key) @local.key?(key) || @parent.key?(key) end |
#keys ⇒ Object
148 149 150 |
# File 'lib/tina4/frond.rb', line 148 def keys (@parent.is_a?(LoopContext) ? @parent.keys : @parent.keys) | @local.keys end |
#merge(other) ⇒ Object
113 114 115 116 117 |
# File 'lib/tina4/frond.rb', line 113 def merge(other) dup_hash = to_h dup_hash.merge!(other) dup_hash end |
#merge!(other) ⇒ Object
119 120 121 122 |
# File 'lib/tina4/frond.rb', line 119 def merge!(other) other.each { |k, v| @local[k] = v } self end |
#respond_to_missing?(name, include_private = false) ⇒ Boolean
140 141 142 |
# File 'lib/tina4/frond.rb', line 140 def respond_to_missing?(name, include_private = false) @parent.respond_to?(name, include_private) || super end |
#to_h ⇒ Object
130 131 132 133 134 |
# File 'lib/tina4/frond.rb', line 130 def to_h h = @parent.is_a?(LoopContext) ? @parent.to_h : @parent.dup @local.each { |k, v| h[k] = v } h end |