Module: Akatus::Transferrable

Included in:
Address, Installment, InstallmentOptions, Item, Payer, Payment, PaymentOption, PaymentType, Phone, Receiver, SplitFee
Defined in:
lib/akatus/transferrable.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



5
6
7
# File 'lib/akatus/transferrable.rb', line 5

def self.included(klass)
  klass.send(:extend, ClassMethods)
end

Instance Method Details

#initialize(attrs = {}) ⇒ Object



9
10
11
# File 'lib/akatus/transferrable.rb', line 9

def initialize(attrs = {})
  attrs.each { |attr, val| send("#{attr}=", val) }
end

#to_payload(include_root = true) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/akatus/transferrable.rb', line 13

def to_payload(include_root = true)
  class_key = self.class.name.demodulize.underscore

  payload = Hash[(self.class.attributes || []).map do |attr|

    attr_value = send(attr)

    next if attr_value.nil?

    if attr_value.respond_to?(:to_payload)
      attr_value = attr_value.to_payload(false)
    end

    if NUMERIC_FIELDS.include?(attr)
      attr_value = Akatus.format_number(attr_value)
    end

    if (INTEGER_FIELDS + STRING_FIELDS).include?(attr)
      attr_value = attr_value.to_s
    end

    [ I18n.t(attr, :locale => "pt-BR", :scope => [:payload, :attributes, class_key]), attr_value ]
  end]

  if include_root
    { I18n.t(class_key, :locale => "pt-BR", :scope => [:payload]) => payload }
  else
    payload
  end

end