Module: HashId

Constant Summary collapse

SKIPPED_ATTRIBS =
["@opts", "@default_namespace", "@instance"]
SKIPPED_2 =
["@id", "@id_before_type_cast"]

Instance Method Summary collapse

Instance Method Details

#calculate_hash(object) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/elibri_onix/hash_id.rb', line 10

def calculate_hash(object)
  result = []
  if object.is_a? Array
    object.each { |x| result << calculate_hash(x) }
  else
    vars = []
    if object.class.to_s.include? "Elibri::ONIX"
      vars += object.class::ATTRIBUTES
      vars += object.class::RELATIONS
    end
    vars = object.instance_variables if vars.blank?
    vars.each do |attrib|
      next if SKIPPED_ATTRIBS.include? attrib
      next if SKIPPED_2.include? attrib
      attrib = attrib.to_s.gsub("@", "").to_sym if attrib.is_a?(String)
      if object.send(attrib).is_a? Array
        result << calculate_hash(object.send(attrib))
      elsif object.send(attrib).is_a?(String) || object.send(attrib).is_a?(Numeric) || object.send(attrib).is_a?(Fixnum) || object.send(attrib).is_a?(Symbol)
        result << object.send(attrib)
      else
        result << calculate_hash(object.send(attrib))
      end
    end
  end
  return Digest::SHA1.hexdigest(result.join("-"))
end

#eidObject



6
7
8
# File 'lib/elibri_onix/hash_id.rb', line 6

def eid
  calculate_hash(self)
end