Module: NubeFact::Utils

Included in:
Document, Document::Item
Defined in:
lib/util/utils.rb

Instance Method Summary collapse

Instance Method Details

#as_json(options = {}) ⇒ Object

Fix to bug due to ActiveSupport::Serialization JSON.

it calls as_json in order to get the hash representation of the object
ActiveSupport uses all object attributes, including item.invoice who force
the json convertion into an infinite loop:

. (invoice -> items -> item -> invoice -> items -> item …)



21
22
23
# File 'lib/util/utils.rb', line 21

def as_json(options={})
  Hash[to_h.map { |k, v| [k.to_s, options ? v.as_json(options.dup) : v.as_json] }]
end

#to_hObject

This iterates over all fields and if is an array (items and guias) converts it to hash using to_h



4
5
6
7
8
9
10
# File 'lib/util/utils.rb', line 4

def to_h
  Hash[self.class::FIELDS.map do |field| 
    value = send(field)
    value = value.map &:to_h if value.is_a? Array
    [field, value]
  end]
end

#to_json(options = {}) ⇒ Object



12
13
14
# File 'lib/util/utils.rb', line 12

def to_json(options = {})
  self.to_h.to_json options
end