module Netfira::WebConnect::Model::Record::Writable

Public Class Methods

attributes_to_send() click to toggle source

Attributes that can be sent

# File lib/netfira/web_connect/model/record/writable.rb, line 14
def self.attributes_to_send
  @attributes_to_send ||= attribute_names +
      (has_languages? ? self::Translation.translated_attribute_names : []) -
      Serializer::EXCLUDE_FROM_SERIALIZE
end

Public Instance Methods

guid() click to toggle source
# File lib/netfira/web_connect/model/record/writable.rb, line 52
def guid
  @guid ||= Guid.from(self[:guid])
end
guid=(_) click to toggle source
# File lib/netfira/web_connect/model/record/writable.rb, line 56
def guid=(_)
  raise 'GUIDs are read-only'
end
to_deliverable() click to toggle source
# File lib/netfira/web_connect/model/record/writable.rb, line 22
def to_deliverable
  attributes_as_camel_cased_hash.tap do |result|

    # Include GUIDs for sendables, e.g. "orderId" => "hd8jdi38jds02893hdo8wq3"
    result["#{self.class.single_name_camel}Id"] = guid.to_s if sendable?

    # Included timestamps, e.g. "createdAt" => "2014-09-27T11:10:00+10:00"
    %w(created_at updated_at).each do |field|
      result[field.camelize :lower] = __send__(field).strftime '%FT%T%:z'
    end

    # Include collections, e.g. "lines" => [{...}, ...]
    result.merge! self.class.schema.has_many.keys.map { |k| [k, __send__(k).map(&:to_deliverable)] }.to_h
  end
end
to_deprecated_downstream() click to toggle source

Used by older downstream methods, e.g. newOrders, newInboundDocuments etc

# File lib/netfira/web_connect/model/record/writable.rb, line 39
def to_deprecated_downstream
  fields = attributes_as_camel_cased_hash(true)
  if sendable?
    sendable_fields = {
        "#{self.class.single_name_camel}GUID" => guid.to_s,
        "#{self.class.single_name_camel}Id" => id.to_s,
        finalised: '1'
    }
    fields.merge! sendable_fields
  end
  {created: created_at.to_i, fields: fields}.merge deprecated_downstream_children
end

Private Instance Methods

attributes_as_camel_cased_hash(stringify = false) click to toggle source
# File lib/netfira/web_connect/model/record/writable.rb, line 62
def attributes_as_camel_cased_hash(stringify = false)
  self.class.attributes_to_send.map do |key|
    value = __send__(key)
    value = value.to_f if BigDecimal === value
    value = value.to_s if stringify
    [key.camelize(:lower), value]
  end.to_h
end
create_guid() click to toggle source
# File lib/netfira/web_connect/model/record/writable.rb, line 77
def create_guid
  self[:guid] ||= (@guid = Guid.create).b
end
deprecated_downstream_children() click to toggle source
# File lib/netfira/web_connect/model/record/writable.rb, line 71
def deprecated_downstream_children
  self.class.schema.has_many.keys.map do |key|
    [key, __send__(key).map(&:to_deprecated_downstream)]
  end.to_h
end