class FormObj::Struct::Array

Public Instance Methods

build(hash = nil, raise_if_not_found: true) click to toggle source
# 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) click to toggle source
# File lib/form_obj/struct/array.rb, line 11
def create(*args)
  build(*args)
end
to_hash() click to toggle source
# File lib/form_obj/struct/array.rb, line 15
def to_hash
  self.map(&:to_hash)
end
update_attributes(items, raise_if_not_found:) click to toggle source
# File lib/form_obj/struct/array.rb, line 19
def update_attributes(items, raise_if_not_found:)
  items_for_CUD = define_items_for_CUD(items)

  destroy_items(items_for_CUD[:destroy])
  update_items(items_for_CUD[:update], raise_if_not_found: raise_if_not_found)
  build_items(items_for_CUD[:create], raise_if_not_found: raise_if_not_found)

  resort_items_after_CUD!(items)
end

Private Instance Methods

build_item(hash, raise_if_not_found:) click to toggle source
# File lib/form_obj/struct/array.rb, line 85
def build_item(hash, raise_if_not_found:)
  item_class.new(hash, raise_if_not_found: raise_if_not_found)
end
build_items(items, params) click to toggle source

items - array of hashes of new attribute values params - additional params for constructor

# File lib/form_obj/struct/array.rb, line 81
def build_items(items, params)
  items.each { |item| self << build_item(item, params) }
end
define_items_for_CUD(items) click to toggle source

Should return hash with 3 keys: :create, :update, :destroy In default implementation: :create - array of hashes of new attribute values :update - hash where key is the item to update and value is a hash of new attribute values :destroy - array of items to be destroyed

# File lib/form_obj/struct/array.rb, line 44
def define_items_for_CUD(items)
  to_be_created = []
  to_be_updated = {}

  items.each do |item_hash|
    item_hash = HashWithIndifferentAccess.new(item_hash)
    item = find_by_primary_key(primary_key(item_hash))
    if item
      to_be_updated[item] = item_hash
    else
      to_be_created << item_hash
    end
  end
  to_be_destroyed = self - to_be_updated.keys

  { create: to_be_created, update: to_be_updated, destroy: to_be_destroyed }
end
destroy_items(items) click to toggle source

items - array of items to be destroyed

# File lib/form_obj/struct/array.rb, line 69
def destroy_items(items)
  items.each { |item| delete(item) }
end
find_by_primary_key(id) click to toggle source
# File lib/form_obj/struct/array.rb, line 35
def find_by_primary_key(id)
  find { |item| item.primary_key == id }
end
primary_key(hash) click to toggle source
# File lib/form_obj/struct/array.rb, line 31
def primary_key(hash)
  hash[item_class.primary_key]
end
resort_items_after_CUD!(items) click to toggle source

Resort items so they will be in the same order as in the update_attributes parameter items - hash received by update_attributes

# File lib/form_obj/struct/array.rb, line 64
def resort_items_after_CUD!(items)
  sort! { |a, b| (items.index { |val| primary_key(val) == a.primary_key } || -1) <=> (items.index { |val| primary_key(val) == b.primary_key } || -1) }
end
update_items(items, params) click to toggle source

items - hash where key is the item to update and value is a hash of new attribute values params - additional params for :update_attributes method

# File lib/form_obj/struct/array.rb, line 75
def update_items(items, params)
  items.each_pair { |item, attr_values| item.update_attributes(attr_values, params) }
end