Sha256: 38f68ddcd69a3106548621b4ced8a75b96642f1cec417fc3989e5a49cb5a012f

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

require 'forwardable'

module Hashme

  # The Hashme CastedArray is a special object wrapper that allows other Model's
  # or Objects to be stored in an array, but maintain casted ownership.
  #
  # Adding objects will automatically assign the Array's owner, as opposed
  # to the array itself.
  #
  class CastedArray
    extend Forwardable
    include Castable

    def_delegators :@_array,
      :to_a, :==, :eql?, :size,
      :first, :last, :at, :length,
      :each, :reject, :empty?, :map,
      :clear, :pop, :shift, :delete, :delete_at,
      :encode_json, :as_json, :to_json,
      :inspect, :any?

    def initialize(owner, property, values = [])
      @_array = []
      self.casted_by = owner
      self.casted_by_property = property
      if values.respond_to?(:each)
        values.each do |value|
          self.push(value)
        end
      end
    end

    def <<(obj)
      @_array << instantiate_and_build(obj)
    end

    def push(obj)
      @_array.push(instantiate_and_build(obj))
    end

    def unshift(obj)
      @_array.unshift(instantiate_and_build(obj))
    end
    
    def [] index
      @_array[index]
    end

    def []= index, obj
      @_array[index] = instantiate_and_build(obj)
    end
    
    protected

    def instantiate_and_build(obj)
      casted_by_property.build(casted_by, obj)
    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
hashme-0.1.2 lib/hashme/casted_array.rb
hashme-0.1.1 lib/hashme/casted_array.rb
hashme-0.1.0 lib/hashme/casted_array.rb