Class: Brick::JoinHash
Instance Attribute Summary collapse
-
#orig_parent ⇒ Object
readonly
Returns the value of attribute orig_parent.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#parent_key ⇒ Object
readonly
Returns the value of attribute parent_key.
Instance Method Summary collapse
Instance Attribute Details
#orig_parent ⇒ Object (readonly)
Returns the value of attribute orig_parent.
171 172 173 |
# File 'lib/brick/join_array.rb', line 171 def orig_parent @orig_parent end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
171 172 173 |
# File 'lib/brick/join_array.rb', line 171 def parent @parent end |
#parent_key ⇒ Object (readonly)
Returns the value of attribute parent_key.
171 172 173 |
# File 'lib/brick/join_array.rb', line 171 def parent_key @parent_key end |
Instance Method Details
#[](*args) ⇒ Object
174 175 176 177 178 179 180 181 182 183 |
# File 'lib/brick/join_array.rb', line 174 def [](*args) if (current = super) current elsif (key = args[0]).is_a?(Symbol) ::Brick::JoinHash.new.tap do |child| child.instance_variable_set(:@parent, self) child.instance_variable_set(:@parent_key, key) end end end |
#[]=(*args) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/brick/join_array.rb', line 185 def []=(*args) ::Brick::JoinArray.attach_back_to_root(self) if !(key = args[0]).is_a?(Symbol) || (!(value = args[1]).is_a?(Symbol) && !value.nil?) super # Revert to normal hash behaviour when we're not passed symbols else case (current = fetch(key, nil)) when value if value.nil? # Setting a single value where nothing yet exists case orig_parent when ::Brick::JoinHash if self.empty? # Convert this empty hash into a JoinArray orig_parent._brick_store(parent_key, ::Brick::JoinArray.new.replace([key])) else # Call back into []= to use our own logic, this time setting this value from the context of the parent orig_parent[parent_key] = key end when ::Brick::JoinArray orig_parent[parent_key][key] = nil else # No knowledge of any parent, so all we can do is add this single value right here as { key => nil } super end key else # Setting a key / value pair where nothing yet exists puts "X2" super(key, ::Brick::JoinArray.new.replace([value])) value end when Symbol # Upgrade an existing symbol to be a part of our special JoinArray puts "X3" super(key, ::Brick::JoinArray.new.replace([current, value])) when ::Brick::JoinArray # Concatenate new stuff onto any existing JoinArray current.set_matching(value, nil) if value when ::Brick::JoinHash # Graduate an existing hash into being in an array if things are dissimilar super(key, ::Brick::JoinArray.new.replace([current, value])) value else # Perhaps this is part of some hybrid thing super(key, ::Brick::JoinArray.new.replace([value])) value end end end |
#_brick_store ⇒ Object
172 |
# File 'lib/brick/join_array.rb', line 172 alias _brick_store []= |