Class: CompositingHash

Inherits:
HookedHash
  • Object
show all
Defined in:
lib/compositing-hash/CompositingHash.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_composite_hash = nil, configuration_instance = nil) ⇒ CompositingHash

initialize #



8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/compositing-hash/CompositingHash.rb', line 8

def initialize( parent_composite_hash = nil, configuration_instance = nil )
  
  super( configuration_instance )
      
  @replaced_parents = { }
  @parent_key_lookup = { }

  # we may later have our own child composites that register with us
  @sub_composite_hashes = [ ]

  initialize_for_parent( parent_composite_hash )
  
end

Instance Attribute Details

#parent_composite_objectObject Also known as: parent_composite_hash

parent_composite_object #

parent_composite_hash    #


27
28
29
# File 'lib/compositing-hash/CompositingHash.rb', line 27

def parent_composite_object
  @parent_composite_object
end

Instance Method Details

#==(object) ⇒ Object

#



126
127
128
129
130
131
132
133
134
# File 'lib/compositing-hash/CompositingHash.rb', line 126

def ==( object )
  
  @parent_key_lookup.each do |this_key, true_value|
    self[ this_key ]
  end
  
  super
  
end

#[](key) ⇒ Object

#



182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/compositing-hash/CompositingHash.rb', line 182

def []( key )
  
  return_value = nil

  if @parent_key_lookup.has_key?( key )
    return_value = set_parent_element_in_self( key, @parent_composite_object[ key ] )
    @parent_key_lookup.delete( key )
  else
    return_value = super
  end

  return return_value
  
end

#[]=(key, object) ⇒ Object Also known as: store



205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
# File 'lib/compositing-hash/CompositingHash.rb', line 205

def []=( key, object )

  @replaced_parents[ key ] = true
  
  @parent_key_lookup.delete( key )
  
  super
  
  @sub_composite_hashes.each do |this_sub_hash|
    this_sub_hash.instance_eval do
      update_as_sub_hash_for_parent_store( key )
    end
  end
      
  return object

end

#child_post_delete_hook(key, object) ⇒ Object

child_post_delete_hook #



114
115
116
117
118
# File 'lib/compositing-hash/CompositingHash.rb', line 114

def child_post_delete_hook( key, object )
  
  return object
  
end

#child_post_set_hook(key, object) ⇒ Object

child_post_set_hook #



93
94
95
96
97
# File 'lib/compositing-hash/CompositingHash.rb', line 93

def child_post_set_hook( key, object )
  
  return object
  
end

#child_pre_delete_hook(key) ⇒ Object

child_pre_delete_hook #



103
104
105
106
107
108
# File 'lib/compositing-hash/CompositingHash.rb', line 103

def child_pre_delete_hook( key )
  
  # false means delete does not take place
  return true
  
end

#child_pre_set_hook(key, object) ⇒ Object

child_pre_set_hook #



83
84
85
86
87
# File 'lib/compositing-hash/CompositingHash.rb', line 83

def child_pre_set_hook( key, object )

  return object
  
end

#delete(key) ⇒ Object



232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/compositing-hash/CompositingHash.rb', line 232

def delete( key )

  @replaced_parents.delete( key )

  @parent_key_lookup.delete( key )

  object = super

  @sub_composite_hashes.each do |this_sub_hash|
    this_sub_hash.instance_eval do
      update_as_sub_hash_for_parent_delete( key, object )
    end
  end

  return object

end

#each(*args, &block) ⇒ Object

each #



140
141
142
143
144
145
146
147
148
# File 'lib/compositing-hash/CompositingHash.rb', line 140

def each( *args, & block )
  
  @parent_key_lookup.each do |this_key, true_value|
    self[ this_key ]
  end

  super
  
end

#freeze!Object

freezes configuration and prevents ancestors from changing this configuration in the future



255
256
257
258
259
260
261
262
263
264
# File 'lib/compositing-hash/CompositingHash.rb', line 255

def freeze!
  
  # unregister with parent composite so we don't get future updates from it
  if @parent_composite_object
    @parent_composite_object.unregister_sub_composite_hash( self )
  end
  
  return self
  
end

#initialize_for_parent(parent_composite_hash) ⇒ Object

initialize_for_parent #



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/compositing-hash/CompositingHash.rb', line 37

def initialize_for_parent( parent_composite_hash )

  if @parent_composite_object = parent_composite_hash

    @parent_composite_object.register_sub_composite_hash( self )

    # @parent_key_lookup tracks keys that we have not yet received from parent
    @parent_composite_object.each do |this_key, this_object|
      @parent_key_lookup[ this_key ] = true
      non_cascading_store( this_key, nil )
    end
    
  end

end

#inspectObject

inspect #



168
169
170
171
172
173
174
175
176
# File 'lib/compositing-hash/CompositingHash.rb', line 168

def inspect
 
  @parent_key_lookup.each do |this_key, true_value|
    self[ this_key ]
  end
 
  super
  
end

#register_sub_composite_hash(sub_composite_hash) ⇒ Object

register_sub_composite_hash #



57
58
59
60
61
62
63
# File 'lib/compositing-hash/CompositingHash.rb', line 57

def register_sub_composite_hash( sub_composite_hash )

  @sub_composite_hashes.push( sub_composite_hash )

  return self

end

#to_sObject

to_s #



154
155
156
157
158
159
160
161
162
# File 'lib/compositing-hash/CompositingHash.rb', line 154

def to_s
 
  @parent_key_lookup.each do |this_key, true_value|
    self[ this_key ]
  end
 
  super
  
end

#unregister_sub_composite_hash(sub_composite_hash) ⇒ Object

unregister_sub_composite_hash #



69
70
71
72
73
74
75
# File 'lib/compositing-hash/CompositingHash.rb', line 69

def unregister_sub_composite_hash( sub_composite_hash )

  @sub_composite_hashes.delete( sub_composite_hash )

  return self

end