Class: FormObj::Struct::Array
- Inherits:
-
TypedArray
- Object
- TypedArray
- FormObj::Struct::Array
- Defined in:
- lib/form_obj/struct/array.rb
Direct Known Subclasses
Instance Method Summary collapse
- #build(hash = nil, raise_if_not_found: true) ⇒ Object
- #create(*args) ⇒ Object
- #to_hash ⇒ Object
- #update_attributes(vals, raise_if_not_found:) ⇒ Object
Instance Method Details
#build(hash = nil, raise_if_not_found: true) ⇒ Object
6 7 8 9 |
# File 'lib/form_obj/struct/array.rb', line 6 def build(hash = nil, raise_if_not_found: true) self << (item = build_item(hash, raise_if_not_found: raise_if_not_found)) item end |
#create(*args) ⇒ Object
11 12 13 |
# File 'lib/form_obj/struct/array.rb', line 11 def create(*args) build(*args) end |
#to_hash ⇒ Object
15 16 17 |
# File 'lib/form_obj/struct/array.rb', line 15 def to_hash self.map(&:to_hash) end |
#update_attributes(vals, raise_if_not_found:) ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/form_obj/struct/array.rb', line 19 def update_attributes(vals, raise_if_not_found:) items_to_add = [] items_to_update = {} vals.each do |val| val = HashWithIndifferentAccess.new(val) item = find_by_primary_key(primary_key(val)) if item items_to_update[item] = val else items_to_add << val end end items_to_destroy = items_for_destruction(items_to_add: items_to_add, items_to_update: items_to_update) destroy_items(items_to_destroy) update_items(items_to_update, raise_if_not_found: raise_if_not_found) build_items(items_to_add, raise_if_not_found: raise_if_not_found) sort! { |a, b| (vals.index { |val| primary_key(val) == a.primary_key } || -1) <=> (vals.index { |val| primary_key(val) == b.primary_key } || -1) } end |