Module: CompositingArray::Interface

Included in:
CompositingArray
Defined in:
lib/compositing-array/CompositingArray/Interface.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parent_composite_objectObject Also known as: parent_composite_array

parent_composite_object #

parent_composite_array   #


58
59
60
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 58

def parent_composite_object
  @parent_composite_object
end

Instance Method Details

#[]=(index, object) ⇒ Object

[]= #



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 135

def []=( index, object )

  super
  
  if index_inside_parent_objects?( index )
    @replaced_parents[ index ] = true
  end
  
  @sub_composite_arrays.each do |this_sub_array|
    this_sub_array.instance_eval do
      update_as_sub_array_for_parent_set( index, object )
    end
  end

  return object

end

#child_post_delete_hook(index, object) ⇒ Object

child_post_delete_hook #



123
124
125
126
127
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 123

def child_post_delete_hook( index, object )
  
  return object
  
end

#child_post_set_hook(index, object, is_insert = false) ⇒ Object

child_post_set_hook #



102
103
104
105
106
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 102

def child_post_set_hook( index, object, is_insert = false )
  
  return object
  
end

#child_pre_delete_hook(index) ⇒ Object

child_pre_delete_hook #



112
113
114
115
116
117
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 112

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

#child_pre_set_hook(index, object, is_insert = false) ⇒ Object

child_pre_set_hook #



92
93
94
95
96
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 92

def child_pre_set_hook( index, object, is_insert = false )

  return object
  
end

#delete_at(index) ⇒ Object

delete_at #



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 194

def delete_at( index )

  deleted_object = super( index )

  @replaced_parents.delete( index )

  if index_inside_parent_objects?( index )
    update_corresponding_index_for_local_change( index, -1 )      
  end

  @sub_composite_arrays.each do |this_sub_array|
    this_sub_array.instance_eval do
      update_as_sub_array_for_parent_delete( index )
    end
  end

  return deleted_object

end

#freeze!Object

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



219
220
221
222
223
224
225
226
227
228
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 219

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_array( self )
  end

  return self

end

#initialize(parent_composite_array = nil, configuration_instance = nil, *args) ⇒ Object

initialize #



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 10

def initialize( parent_composite_array = nil, configuration_instance = nil, *args )

  super( configuration_instance, *args )
  
  # arrays that inherit from us
  @sub_composite_arrays = [ ]

  # hash tracking index in self corresponding to index in parent
  # this is since objects can be inserted before/between parent objects
  @replaced_parents = { }

  # initialize corresponding indexes in self to indexes in parent
  @local_index_for_parent_index = { }

  # we keep track of how many objects are interpolated between parent objects
  # plus number of parent objects
  @parent_and_interpolated_object_count = 0    

  initialize_for_parent( parent_composite_array )

end

#initialize_for_parent(parent_composite_array) ⇒ Object

initialize_for_parent #



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

def initialize_for_parent( parent_composite_array )

  if @parent_composite_object = parent_composite_array

    # initialize contents of self from parent contents
    unless @parent_composite_object.empty?
      update_as_sub_array_for_parent_insert( 0, *@parent_composite_object )
    end
    
    @parent_composite_object.register_sub_composite_array( self )

  end
  
end

#insert(index, *objects) ⇒ Object

insert #



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 157

def insert( index, *objects )
  
  unless objects.empty?
    
    if inserted_objects = super
      
      # super may insert more (nil generally) or remove some (ie for duplicates)
      count_changed = objects.count - inserted_objects.count
      
      if count_changed < 0
        index += count_changed
      end
      
      objects = inserted_objects
  
      if index_inside_parent_objects?( index )
        update_corresponding_index_for_local_change( index, objects.count )
      end

      @sub_composite_arrays.each do |this_sub_array|
        this_sub_array.instance_eval do
          update_as_sub_array_for_parent_insert( index, *objects )
        end
      end

    end
    
  end
  
  return objects

end

#register_sub_composite_array(sub_composite_array) ⇒ Object

register_sub_composite_array #



66
67
68
69
70
71
72
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 66

def register_sub_composite_array( sub_composite_array )

  @sub_composite_arrays.push( sub_composite_array )

  return self

end

#unregister_sub_composite_array(sub_composite_array) ⇒ Object

unregister_sub_composite_array #



78
79
80
81
82
83
84
# File 'lib/compositing-array/CompositingArray/Interface.rb', line 78

def unregister_sub_composite_array( sub_composite_array )

  @sub_composite_arrays.delete( sub_composite_array )

  return self

end