Sha256: 4bb0ae7f31ce28cf28a64b00fd8b31f1de67900a80b61f4af0a7ccc38385a012

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 KB

Contents

module Etapper
  # This is a virtual base class for specialized representations of eTapestry API arrays 
  # such as PhoneHash, DefinedValueHash, etc.  It _should not_ be invoked on its own.  It
  # needs the following constants defined in subclasses to work:
  class EtapHash < Hash
    attr_reader :base, :detailed, :etapper_class
    alias_method :hash_assign, :[]=
    
    def initialize(array_from_api, etapper_class, key_symbol, value_symbol)
      @base, @etapper_class, @key_symbol, @value_symbol = array_from_api, etapper_class, key_symbol, value_symbol
      @detailed = Hash.new
      @base.each do |element|
        friendly = etapper_class.new(element)
        add_to_self(friendly)
        add_to_detailed(friendly)  # So we can easily find our objects later
      end
    end
    
    def []=(key, val)
      update_base(key, val)
      hash_assign(key, val)
    end
    
  protected
  
    def update_base(key, val)
      if @detailed.has_key?(key)
        friendly = @detailed[key]
        friendly.value = val
      else # New object, so create it and push to base
        friendly = @etapper_class.new(@key_symbol => key, @value_symbol => val)
        friendly.value = val
        @detailed[key] = friendly
        @base << friendly.base
      end
    end
  
    def add_to_self(obj)
      self.hash_assign(obj.key, obj.value)
    end
    
    def add_to_detailed(obj)
      @detailed[obj.key] = obj  # So we can easily find our objects later
    end
  end
  
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
etapper-0.0.5 lib/etapper/classes/etap_hash.rb
etapper-0.0.4 lib/etapper/classes/etap_hash.rb
etapper-0.0.3 lib/etapper/classes/etap_hash.rb
etapper-0.0.2 lib/etapper/classes/etap_hash.rb
etapper-0.0.1 lib/etapper/classes/etap_hash.rb
etapper-0.0.0 lib/etapper/classes/etap_hash.rb