class ProtoJ::Associations::HasManyAssociation

Attributes

target[R]

Public Class Methods

new(klass) click to toggle source
# File lib/proto_j/associations/has_many_association.rb, line 6
def initialize(klass)
  @klass  = klass
  @target = []
  @proxy  = CollectionProxy.new(self)
end

Public Instance Methods

clear() click to toggle source
# File lib/proto_j/associations/has_many_association.rb, line 38
def clear
  @target = []
  @proxy
end
concat(*records) click to toggle source
# File lib/proto_j/associations/has_many_association.rb, line 16
def concat(*records)
  records = records.flatten

  records.each do |record|
    unless record.is_a?(@klass)
      raise ::ProtoJ::Exception::TypeMismatch.new("require #{@klass}, but got #{record.class}")
    end
  end

  @target.concat(records)
end
new() click to toggle source
# File lib/proto_j/associations/has_many_association.rb, line 28
def new
  child = @klass.new
  @target << child
  child
end
reader() click to toggle source
# File lib/proto_j/associations/has_many_association.rb, line 12
def reader
  @proxy
end
to_hash() click to toggle source
# File lib/proto_j/associations/has_many_association.rb, line 34
def to_hash
  @target.collect { |t| t.to_hash }
end
update(json=[]) click to toggle source
# File lib/proto_j/associations/has_many_association.rb, line 43
def update(json=[])
  new_target = []

  if json.is_a?(Array)
    array = json
  else
    array = JSON.parse(json)
  end

  if array.is_a? Array
    array.each do |item|
      new_target << @klass.new(item)
    end

    @target = new_target
  end

  @target
end