Class: Brick::JoinHash

Inherits:
Hash show all
Defined in:
lib/brick/join_array.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#orig_parentObject (readonly)

Returns the value of attribute orig_parent.



177
178
179
# File 'lib/brick/join_array.rb', line 177

def orig_parent
  @orig_parent
end

#parentObject (readonly)

Returns the value of attribute parent.



177
178
179
# File 'lib/brick/join_array.rb', line 177

def parent
  @parent
end

#parent_keyObject (readonly)

Returns the value of attribute parent_key.



177
178
179
# File 'lib/brick/join_array.rb', line 177

def parent_key
  @parent_key
end

Instance Method Details

#[](*args) ⇒ Object



180
181
182
183
184
185
186
187
188
189
# File 'lib/brick/join_array.rb', line 180

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



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
226
227
228
229
230
231
# File 'lib/brick/join_array.rb', line 191

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_storeObject



178
# File 'lib/brick/join_array.rb', line 178

alias _brick_store []=