Module: HQMF::Conversion::Utilities

Instance Method Summary collapse

Instance Method Details

#build_hash(source, elements) ⇒ Object



5
6
7
8
9
10
11
12
# File 'lib/hqmf-model/utilities.rb', line 5

def build_hash(source, elements)
  hash = {}
  elements.each do |element|
    value = source.send(element)
    hash[element] = value unless value.nil?
  end
  hash
end

#check_equality(left, right) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/hqmf-model/utilities.rb', line 42

def check_equality(left,right)
  same = true
  left.instance_variables.each do |variable|
    same &&= left.instance_variable_get(variable) == right.instance_variable_get(variable)
  end
  same
end

#json_array(elements) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hqmf-model/utilities.rb', line 14

def json_array(elements) 
  return nil if elements.nil?
  array = []
  elements.each do |element| 
    if (element.is_a? OpenStruct)
      array << openstruct_to_json(element)
    else
      array << element.to_json 
    end
  end
  array.compact!
  (array.empty?) ? nil : array
end

#openstruct_to_json(element) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/hqmf-model/utilities.rb', line 28

def openstruct_to_json(element)
  json = {}
  element.marshal_dump.each do |key,value|
    if value.is_a? OpenStruct
      json[key] = openstruct_to_json(value) 
    elsif (value.class.to_s.split("::").first.start_with? 'HQMF')
      json[key] = value.to_json
    else
      json[key] = value
    end
  end
  json
end