Sha256: 59d00fe2ccd4ac765e78a0fd2415ea2733df17239fdf56135cd99296440ae44b
Contents?: true
Size: 1.5 KB
Versions: 1
Compression:
Stored size: 1.5 KB
Contents
module NubeFact::Utils # This iterates over all fields and if is an array (items and guias) converts # it to hash using to_h 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 def to_json(options = {}) self.to_h.to_json options end # 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 ...) def as_json(options={}) Hash[to_h.map { |k, v| [k.to_s, options ? v.as_json(options.dup) : v.as_json] }] end private def set_default_data self.class::DEFAULT_DATA.each do |field, value| value = value.call if value.kind_of? Proc instance_variable_set("@#{field}", value) end end def load_data_from_param(data_hash) set_default_data data_hash.each do|key, value| if self.class.const_defined?('AUTO_CALCULATED_FIELDS') \ && self.class::AUTO_CALCULATED_FIELDS.include?(key) warn("field #{key} will be calculated automatically, you don't need to pass it.") end begin send "#{key}=", value rescue NoMethodError => e raise NubeFact::InvalidField.new "Invalid Field: #{key}" end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
nubefact-0.0.3 | lib/util/utils.rb |