class BrregGrunndata::Utils::BaseTypeMerger

Helper class to merge two objects instance of Types::Base.

Public Class Methods

new(a, b) click to toggle source
# File lib/brreg_grunndata/utils.rb, line 49
def initialize(a, b)
  if a.class != b.class
    raise ArgumentError, "#{b.class.name} is not of type #{a.class.name}"
  end

  @a = a
  @b = b
end

Public Instance Methods

merge() click to toggle source

Merges the two given classes a and b.

@return A new instance of type @a

# File lib/brreg_grunndata/utils.rb, line 61
def merge
  a_hash = without_empty_values @a.to_h
  b_hash = without_empty_values @b.to_h

  @a.class.new a_hash.merge b_hash
end

Private Instance Methods

without_empty_values(hash) click to toggle source
# File lib/brreg_grunndata/utils.rb, line 70
def without_empty_values(hash)
  hash.delete_if { |_k, v| v.nil? }
end