module Lolita::ObservedArray

This module overwrite Array methods that change Array to change element that is added to Array. Class that include this module must have collection_variable method that return collection variable that hold all records. Also build_element method must have with element and &block as arguments Build element method should contain actions that modify element or if you do not want to change given element, than return it.

Example

class MyCollection
 ...
 def build_element(element)
   element=element.to_s if element.is_a?(Symbol)
   element
 end
end

my_collection=MyCollection.new
my_collection.push(:element)
my_collection.last #=> element
my_collection.last.class #=> String

Example

class MyClass
  include Lolita::ObservedArray
  private
    def collection_variable
      @collection_variable
    end
    def build_element(element,&block)
      ...
    end
end

Public Instance Methods

<<(value) click to toggle source
# File lib/lolita/observed_array.rb, line 50
def <<(value)
  value=build_element(value)
  collection_variable<<value
end
[]=(value,index) click to toggle source
# File lib/lolita/observed_array.rb, line 55
def []=(value,index)
  value=build_element(value)
  collection_variable[index]=value
end
each() { |collection_element| ... } click to toggle source

To support enumerable functions as each, collect etc.

# File lib/lolita/observed_array.rb, line 61
def each
  collection_variable.each{|collection_element| yield collection_element}
end
insert(value) click to toggle source
# File lib/lolita/observed_array.rb, line 45
def insert(value)
  value=build_element(value)
  collection_variable.insert(value)
end
method_missing(method,*args,&block) click to toggle source
# File lib/lolita/observed_array.rb, line 35
def method_missing(method,*args,&block)
  generate_collection_elements! if self.respond_to?(:generate_collection_elements!)
  collection_variable.__send__(method,*args,&block)
end
push(value) click to toggle source
# File lib/lolita/observed_array.rb, line 40
def push(value)
  value=build_element(value)
  collection_variable.push(value)
end

Private Instance Methods

build_element(element,&block) click to toggle source
# File lib/lolita/observed_array.rb, line 71
def build_element(element,&block)
  raise "You should implement collection_variable method in your class.See ObsservedArray for implementation."
end
collection_variable() click to toggle source
# File lib/lolita/observed_array.rb, line 67
def collection_variable
  raise "You should implement collection_variable method in your class. See ObservedArray for implementation."
end