module DHS::Data::Extend

Public Instance Methods

extend!(addition, key = nil) click to toggle source

Extends already fetched data (self) with additionally fetched data (addition) using the given key

# File lib/dhs/concerns/data/extend.rb, line 12
def extend!(addition, key = nil)
  addition = cast_relation_class_for_extension(addition, key)
  if collection?
    extend_collection!(addition, key)
  elsif _raw.is_a?(Array) || self[key]._raw.is_a?(Array)
    extend_array!(addition, key)
  elsif item?
    extend_item!(addition, key)
  end
end

Private Instance Methods

cast_relation_class_for_extension(addition, key = nil) click to toggle source
# File lib/dhs/concerns/data/extend.rb, line 25
def cast_relation_class_for_extension(addition, key = nil)
  return addition if _record.nil? || key.nil? || _record._relations.nil? || _record._relations[key].nil?
  addition.becomes(_record._relations[key][:record_class_name].constantize, errors: addition.errors, warnings: addition.warnings)
end
extend_array!(addition, key = nil) click to toggle source
# File lib/dhs/concerns/data/extend.rb, line 48
def extend_array!(addition, key = nil)
  (key ? self[key] : self).zip(addition) do |item, additional_item|
    item._raw.merge!(additional_item._raw) if additional_item.present?
  end
end
extend_collection!(addition, key = nil) click to toggle source
# File lib/dhs/concerns/data/extend.rb, line 30
def extend_collection!(addition, key = nil)
  map do |item|
    item_raw = key ? item._raw[key] : item._raw
    item_raw.blank? ? [nil] : item_raw
  end
    .flatten
    .each_with_index do |item, index|
      item_addition = addition[index]
      next if item_addition.nil? || item.nil?
      if item_addition._raw.is_a?(Array)
        item[items_key] ||= []
        item[items_key].concat(item_addition._raw)
      else
        item.merge! item_addition._raw
      end
    end
end
extend_item!(addition, key = nil) click to toggle source
# File lib/dhs/concerns/data/extend.rb, line 54
def extend_item!(addition, key = nil)
  return if addition.nil?
  if addition.collection?
    extend_item_with_collection!(addition, key)
  else # simple case merges hash into hash
    (key ? _raw[key.to_sym] : _raw).merge!(addition._raw)
  end
end
extend_item_with_collection!(addition, key = nil) click to toggle source
# File lib/dhs/concerns/data/extend.rb, line 63
def extend_item_with_collection!(addition, key = nil)
  target = (key ? self[key] : self)
  if target._raw.is_a? Array
    self[key] = addition.map(&:_raw) if key
  else # hash with items
    extend_item_with_hash_containing_items!(target, addition)
  end
end
extend_item_with_hash_containing_items!(target, addition) click to toggle source
# File lib/dhs/concerns/data/extend.rb, line 72
def extend_item_with_hash_containing_items!(target, addition)
  DHS::Collection.nest(input: target._raw, value: [], record: self) # inits the nested collection
  if DHS::Collection.access(input: target._raw, record: self).empty?
    DHS::Collection.nest(
      input: target._raw,
      value: addition.reject { |item| item.nil? },
      record: self
    )
  else
    DHS::Collection.access(input: target._raw, record: self).each_with_index do |item, index|
      item.merge!(addition[index])
    end
  end
end