Module: ActiveModel::Embedding::Collecting
- Includes:
- ForbiddenAttributesProtection
- Included in:
- Collection
- Defined in:
- lib/active_model/embedding/collecting.rb
Instance Attribute Summary collapse
-
#document_class ⇒ Object
readonly
Returns the value of attribute document_class.
-
#documents ⇒ Object
(also: #to_a)
readonly
Returns the value of attribute documents.
Instance Method Summary collapse
- #==(other) ⇒ Object
- #as_json ⇒ Object
- #attributes=(documents_attributes) ⇒ Object
- #build(attributes = {}) ⇒ Object
- #each ⇒ Object
- #find_by_id(id) ⇒ Object
- #initialize(documents) ⇒ Object
- #push(*new_documents) ⇒ Object (also: #<<, #append)
- #save ⇒ Object
- #to_json ⇒ Object
Instance Attribute Details
#document_class ⇒ Object (readonly)
Returns the value of attribute document_class.
8 9 10 |
# File 'lib/active_model/embedding/collecting.rb', line 8 def document_class @document_class end |
#documents ⇒ Object (readonly) Also known as: to_a
Returns the value of attribute documents.
8 9 10 |
# File 'lib/active_model/embedding/collecting.rb', line 8 def documents @documents end |
Instance Method Details
#==(other) ⇒ Object
103 104 105 |
# File 'lib/active_model/embedding/collecting.rb', line 103 def ==(other) documents.map(&:attributes) == other.map(&:attributes) end |
#as_json ⇒ Object
95 96 97 |
# File 'lib/active_model/embedding/collecting.rb', line 95 def as_json documents.as_json end |
#attributes=(documents_attributes) ⇒ Object
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_model/embedding/collecting.rb', line 17 def attributes=(documents_attributes) documents_attributes = sanitize_for_mass_assignment(documents_attributes) case documents_attributes when Hash documents_attributes.each do |index, document_attributes| index = index.to_i id = fetch_id(document_attributes) || index document = find_by_id id if id unless document document = documents[index] || build end document.attributes = document_attributes end when Array documents_attributes.each do |document_attributes| id = fetch_id(document_attributes) document = find_by_id id if id unless document document = build end document.attributes = document_attributes end else raise_attributes_error end end |
#build(attributes = {}) ⇒ Object
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/active_model/embedding/collecting.rb', line 53 def build(attributes = {}) case attributes when Hash document = document_class.new(attributes) append document document when Array attributes.map do |document_attributes| build(document_attributes) end else raise_attributes_error end end |
#each ⇒ Object
89 90 91 92 93 |
# File 'lib/active_model/embedding/collecting.rb', line 89 def each return self.to_enum unless block_given? documents.each { |document| yield document } end |
#find_by_id(id) ⇒ Object
49 50 51 |
# File 'lib/active_model/embedding/collecting.rb', line 49 def find_by_id(id) documents.find { |document| document.id == id } end |
#initialize(documents) ⇒ Object
12 13 14 15 |
# File 'lib/active_model/embedding/collecting.rb', line 12 def initialize(documents) @documents = documents @document_class = documents.first.class end |
#push(*new_documents) ⇒ Object Also known as: <<, append
70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/active_model/embedding/collecting.rb', line 70 def push(*new_documents) new_documents = new_documents.flatten valid_documents = new_documents.all? { |document| document.is_a? document_class } unless valid_documents raise ArgumentError, "Expect arguments to be of class #{document_class}" end @documents.push(*new_documents) end |
#save ⇒ Object
85 86 87 |
# File 'lib/active_model/embedding/collecting.rb', line 85 def save documents.all?(&:save) end |
#to_json ⇒ Object
99 100 101 |
# File 'lib/active_model/embedding/collecting.rb', line 99 def to_json as_json.to_json end |