Module: NubeFact::Utils
- Included in:
- Document, Document::Item
- Defined in:
- lib/util/utils.rb
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Object
Fix to bug due to ActiveSupport::Serialization JSON.
-
#to_h ⇒ Object
This iterates over all fields and if is an array (items and guias) converts it to hash using to_h.
- #to_json(options = {}) ⇒ Object
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(={}) Hash[to_h.map { |k, v| [k.to_s, ? v.as_json(.dup) : v.as_json] }] end |
#to_h ⇒ Object
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( = {}) self.to_h.to_json end |