Sha256: 3dad71bc7e514d69683d1a7e008f06c4760dabfa411934653f368a80b0695304

Contents?: true

Size: 1.35 KB

Versions: 14

Compression:

Stored size: 1.35 KB

Contents

require 'volt/models/model_wrapper'
require 'volt/models/model_helpers'

class ArrayModel < ReactiveArray
  include ModelWrapper
  include ModelHelpers
  
  attr_reader :parent, :path, :persistor, :options

  def initialize(array=[], options={})
    @options = options
    @parent = options[:parent]
    @path = options[:path] || []
    @persistor = setup_persistor(options[:persistor])
    
    array = wrap_values(array)
    
    super(array)
    
    @persistor.loaded if @persistor
  end
  
  def attributes
    self
  end
  
  # Make sure it gets wrapped
  def <<(*args)
    args = wrap_values(args)
    
    super(*args)
    
    @persistor.added(args[0], @array.size-1) if @persistor
  end
  
  # Make sure it gets wrapped
  def inject(*args)
    args = wrap_values(args)
    super(*args)
  end

  # Make sure it gets wrapped
  def +(*args)
    args = wrap_values(args)
    super(*args)
  end

  def new_model(*args)
    Model.new(*args)
  end
  
  def new_array_model(*args)
    ArrayModel.new(*args)
  end
  
  # Convert the model to an array all of the way down
  def to_a
    array = []
    attributes.each do |value|      
      array << deep_unwrap(value)
    end
    
    return array
  end
  
  private
    # Takes the persistor if there is one and
    def setup_persistor(persistor)
      if persistor
        @persistor = persistor.new(self)
      end
    end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
volt-0.5.18 lib/volt/models/array_model.rb
volt-0.5.17 lib/volt/models/array_model.rb
volt-0.5.16 lib/volt/models/array_model.rb
volt-0.5.15 lib/volt/models/array_model.rb
volt-0.5.14 lib/volt/models/array_model.rb
volt-0.5.13 lib/volt/models/array_model.rb
volt-0.5.12 lib/volt/models/array_model.rb
volt-0.5.11 lib/volt/models/array_model.rb
volt-0.5.10 lib/volt/models/array_model.rb
volt-0.5.9 lib/volt/models/array_model.rb
volt-0.5.8 lib/volt/models/array_model.rb
volt-0.5.7 lib/volt/models/array_model.rb
volt-0.5.6 lib/volt/models/array_model.rb
volt-0.5.4 lib/volt/models/array_model.rb