Class: AmazonFlexPay::Model

Inherits:
Object
  • Object
show all
Defined in:
lib/amazon_flex_pay/model.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash = {}) ⇒ Model

:nodoc:



95
96
97
# File 'lib/amazon_flex_pay/model.rb', line 95

def initialize(hash = {}) #:nodoc:
  assign(hash)
end

Class Method Details

.attribute(attr, options = {}) ⇒ Object

Creates an attribute by defining reader and writer methods. These attributes will also be returned by AmazonFlexPay::Model#to_hash for processing into query strings, etc.

A few different attribute types are supported.

Enumerated Attributes

If Amazon only supports certain values, this will enforce a whitelist. Name one of the existing enumerations from AmazonFlexPay::Enumerations like this:

attribute :status, :enumeration => :token_status

Complex Attributes

When the attribute itself has attributes, name one of the existing complex data types from AmazonFlexPay::DataTypes like this:

attribute :final_amount, :type => :amount

Arrays of Complex Attributes

When Amazon’s XML returns (or has the potential to return) multiples of a complex data type, declare it like this:

attribute :transaction, :collection => :transaction_detail

And then for convenience and readability, alias a plural method:

alias_method :transactions, :transaction


31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/amazon_flex_pay/model.rb', line 31

def attribute(attr, options = {})
  attribute_names << attr.to_s.camelcase
  name = attr.to_s.underscore

  # writer
  if options[:enumeration]
    enumerated_attribute(name, options[:enumeration])
  elsif options[:type]
    complex_attribute(name, options[:type])
  elsif options[:collection]
    collection_attribute(name, options[:collection])
  else
    attr_accessor name
  end
end

.attribute_namesObject

The names of all of the attributes of this model, in CamelCase.



48
49
50
# File 'lib/amazon_flex_pay/model.rb', line 48

def attribute_names
  @attributes ||= (self == Model) ? [] : superclass.attribute_names.dup
end

.collection_attribute(attr, data_type = nil) ⇒ Object

:nodoc:



82
83
84
85
86
87
88
89
90
91
92
# File 'lib/amazon_flex_pay/model.rb', line 82

def collection_attribute(attr, data_type = nil) #:nodoc:
  class_eval <<-END
    def #{attr}
      @#{attr} ||= []
    end

    def #{attr}=(array)
      @#{attr} = [array].flatten.map{|hash| AmazonFlexPay::DataTypes::#{data_type.to_s.camelcase}.new(hash)}
    end
  END
end

.complex_attribute(attr, data_type = nil) ⇒ Object

:nodoc:



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/amazon_flex_pay/model.rb', line 69

def complex_attribute(attr, data_type = nil) #:nodoc:
  data_type ||= attr
  class_eval <<-END
    def #{attr}
      @#{attr} ||= AmazonFlexPay::DataTypes::#{data_type.to_s.camelcase}.new
    end

    def #{attr}=(hash)
      @#{attr} = AmazonFlexPay::DataTypes::#{data_type.to_s.camelcase}.new(hash)
    end
  END
end

.enumerated_attribute(attr, source = nil) ⇒ Object

:nodoc:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/amazon_flex_pay/model.rb', line 52

def enumerated_attribute(attr, source = nil) #:nodoc:
  source ||= attr
  class_eval <<-END
    def #{attr}
      @#{attr}
    end

    def #{attr}=(val)
      options = AmazonFlexPay::Enumerations::#{source.to_s.camelcase}
      unless [val].flatten.all?{ |v| options.include?(v) }
        raise ArgumentError.new("\#{val} is not an allowed option (\#{options.join(', ')})")
      end
      @#{attr} = val
    end
  END
end

Instance Method Details

#to_hashObject

Formats all attributes into a hash of parameters.



100
101
102
103
104
105
# File 'lib/amazon_flex_pay/model.rb', line 100

def to_hash
  self.class.attribute_names.inject({}) do |hash, name|
    val = format_value(send(name.underscore))
    val.empty? ? hash : hash.merge(format_key(name) => val)
  end
end