Class: ProtoJ::Associations::HasManyAssociation
- Inherits:
-
Object
- Object
- ProtoJ::Associations::HasManyAssociation
- Defined in:
- lib/proto_j/associations/has_many_association.rb
Instance Attribute Summary collapse
-
#target ⇒ Object
readonly
Returns the value of attribute target.
Instance Method Summary collapse
- #clear ⇒ Object
- #concat(*records) ⇒ Object
-
#initialize(klass) ⇒ HasManyAssociation
constructor
A new instance of HasManyAssociation.
- #new ⇒ Object
- #reader ⇒ Object
- #to_hash ⇒ Object
- #update(json = []) ⇒ Object
Constructor Details
#initialize(klass) ⇒ HasManyAssociation
Returns a new instance of HasManyAssociation.
6 7 8 9 10 |
# File 'lib/proto_j/associations/has_many_association.rb', line 6 def initialize(klass) @klass = klass @target = [] @proxy = CollectionProxy.new(self) end |
Instance Attribute Details
#target ⇒ Object (readonly)
Returns the value of attribute target.
4 5 6 |
# File 'lib/proto_j/associations/has_many_association.rb', line 4 def target @target end |
Instance Method Details
#clear ⇒ Object
38 39 40 41 |
# File 'lib/proto_j/associations/has_many_association.rb', line 38 def clear @target = [] @proxy end |
#concat(*records) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 |
# 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 ⇒ Object
28 29 30 31 32 |
# File 'lib/proto_j/associations/has_many_association.rb', line 28 def new child = @klass.new @target << child child end |
#reader ⇒ Object
12 13 14 |
# File 'lib/proto_j/associations/has_many_association.rb', line 12 def reader @proxy end |
#to_hash ⇒ Object
34 35 36 |
# File 'lib/proto_j/associations/has_many_association.rb', line 34 def to_hash @target.collect { |t| t.to_hash } end |
#update(json = []) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# 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 |