Sha256: d54c27934609320a2a4e6a710a9da4fb24591c76da97d7b4d2395d32928a5a65

Contents?: true

Size: 829 Bytes

Versions: 8

Compression:

Stored size: 829 Bytes

Contents

#
# Wrapper around Array so that the casted_by attribute is set in all
# elements of the array.
#

module CouchRest::Model
  class CastedArray < Array
    attr_accessor :casted_by
    attr_accessor :property

    def initialize(array, property)
      self.property = property
      super(array)
    end
    
    def << obj
      super(instantiate_and_cast(obj))
    end
    
    def push(obj)
      super(instantiate_and_cast(obj))
    end
    
    def []= index, obj
      super(index, instantiate_and_cast(obj))
    end

    protected

    def instantiate_and_cast(obj)
      if self.casted_by && self.property && obj.class != self.property.type_class
        self.property.cast_value(self.casted_by, obj)
      else
        obj.casted_by = self.casted_by if obj.respond_to?(:casted_by)
        obj
      end
    end
  end
end

Version data entries

8 entries across 8 versions & 3 rubygems

Version Path
couchrest_model-1.1.0.beta2 lib/couchrest/model/casted_array.rb
couchrest_model-1.1.0.beta lib/couchrest/model/casted_array.rb
couchrest_model-1.0.0 lib/couchrest/model/casted_array.rb
couchrest_model-radiant-1.0.0 lib/couchrest/model/casted_array.rb
couchrest_model-1.0.0.beta8 lib/couchrest/model/casted_array.rb
couchrest_model_thought-1.0.0.beta9 lib/couchrest/model/casted_array.rb
couchrest_model_thought-1.0.0.beta8 lib/couchrest/model/casted_array.rb
couchrest_model-1.0.0.beta7 lib/couchrest/model/casted_array.rb