Class: PayPal::SDK::Core::API::DataTypes::Base

Inherits:
Object
  • Object
show all
Includes:
SimpleTypes, Logging
Defined in:
lib/paypal-sdk/core/api/data_types/base.rb

Overview

Create attributes and restrict the object type.

Example

class ConvertCurrencyRequest < Core::API::DataTypes::Base
  object_of :baseAmountList,        CurrencyList
  object_of :convertToCurrencyList, CurrencyCodeList
  object_of :countryCode,           String
  object_of :conversionType,        String
end

Constant Summary collapse

HashOptions =
{ :attribute => true, :namespace => true, :symbol => false }
ContentKey =
:value

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Logging

#log_event, #logger, logger, logger=

Constructor Details

#initialize(options = {}, &block) ⇒ Base

Initialize options.


113
114
115
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 113

def initialize(options = {}, &block)
  merge!(options, &block)
end

Class Method Details

.add_attribute(name, options = {}) ⇒ Object

Add attribute

Arguments

  • name – attribute name

  • options – options


30
31
32
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 30

def add_attribute(name, options = {})
  add_member(name, SimpleTypes::String, options.merge( :attribute => true ))
end

.add_member(member_name, klass, options = {}) ⇒ Object

Add Field to class variable hash and generate methods

Example

add_member(:errorMessage, String)  # Generate Code
# attr_reader   :errorMessage
# alias_method  :error_message,  :errorMessage
# alias_method  :error_message=, :errorMessage=

50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 50

def add_member(member_name, klass, options = {})
  member_name = member_name.to_sym
  return if members[member_name]
  members[member_name] = options.merge( :type => klass )
  member_variable_name = "@#{member_name}"
  define_method "#{member_name}=" do |value|
    object = options[:array] ? convert_array(value, klass) : convert_object(value, klass)
    instance_variable_set(member_variable_name, object)
  end
  default_value = ( options[:array] ? [] : ( klass < Base ? Util::OrderedHash.new : nil ) )
  define_method member_name do |&block|
    value = instance_variable_get(member_variable_name) || ( default_value && send("#{member_name}=", default_value) )
    value.instance_eval(&block) if block
    value
  end
  define_alias_methods(member_name, options)
end

.array_of(key, klass, options = {}) ⇒ Object

define method for given member and the class name

Example

array_of(:errorMessage, ErrorMessage) # It Generate below code
# def errorMessage=(array)
#   @errorMessage = array.map{|options| ErrorMessage.new(options) }
# end
# add_member :errorMessage, ErrorMessage

98
99
100
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 98

def array_of(key, klass, options = {})
  add_member(key, klass, options.merge(:array => true))
end

.define_alias_methods(member_name, options) ⇒ Object

Define alias methods for getter and setter


69
70
71
72
73
74
75
76
77
78
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 69

def define_alias_methods(member_name, options)
  snakecase_name = snakecase(member_name)
  alias_method snakecase_name, member_name
  alias_method "#{snakecase_name}=", "#{member_name}="
  alias_method "#{options[:namespace]}:#{member_name}=", "#{member_name}=" if options[:namespace]
  if options[:attribute]
    alias_method "@#{member_name}=", "#{member_name}="
    alias_method "@#{options[:namespace]}:#{member_name}=", "#{member_name}=" if options[:namespace]
  end
end

.membersObject

Fields list for the DataTye


35
36
37
38
39
40
41
42
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 35

def members
  @members ||=
    begin
      superclass.load_members if defined? superclass.load_members
      parent_members = superclass.instance_variable_get("@members")
      parent_members ? parent_members.dup : Util::OrderedHash.new
    end
end

.object_of(key, klass, options = {}) ⇒ Object

define method for given member and the class name

Example

object_of(:errorMessage, ErrorMessage) # Generate Code
# def errorMessage=(options)
#   @errorMessage = ErrorMessage.new(options)
# end
# add_member :errorMessage, ErrorMessage

87
88
89
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 87

def object_of(key, klass, options = {})
  add_member(key, klass, options)
end

.snakecase(string) ⇒ Object

Generate snakecase string.

Example

snakecase(“errorMessage”) # error_message


106
107
108
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 106

def snakecase(string)
  string.to_s.gsub(/([a-z])([A-Z])/, '\1_\2').gsub(/([A-Z])([A-Z][a-z])/, '\1_\2').downcase
end

Instance Method Details

#convert_array(array, klass) ⇒ Object

Create array of objects.

Example

covert_array([{ :amount => “55”, :code => “USD”}], CurrencyType) covert_array({ “0” => { :amount => “55”, :code => “USD”} }, CurrencyType) covert_array({ :amount => “55”, :code => “USD”}, CurrencyType) # @return # [ <CurrencyType#object @amount=“55” @code=“USD” > ]


151
152
153
154
155
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 151

def convert_array(array, klass)
  default_value = ( klass < Base ? Util::OrderedHash.new : nil )
  data_type_array = ArrayWithBlock.new{|object| convert_object(object || default_value, klass) }
  data_type_array.merge!(array)
end

#convert_object(object, klass) ⇒ Object

Create object based on given data.

Example

covert_object({ :amount => “55”, :code => “USD”}, CurrencyType ) # @return # <CurrencyType#object @amount=“55” @code=“USD” >


162
163
164
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 162

def convert_object(object, klass)
  object.is_a?(klass) ? object : ( ( object.nil? or object == "" ) ? nil : klass.new(object) )
end

#hash_key(key, options = {}) ⇒ Object

Generate Hash key for given member name based on configuration

Example

hash_key(:amount) # @return :“ebl:amount” hash_key(:type) # @return :“@type”


196
197
198
199
200
201
202
203
204
205
206
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 196

def hash_key(key, options = {})
  unless key == ContentKey
    member_option = members[key]
    key = member_option[:name] if member_option.include? :name
    if !member_option[:attribute] and member_option[:namespace] and options[:namespace]
      key = "#{member_option[:namespace]}:#{key}"
    end
    key = "@#{key}" if member_option[:attribute] and options[:attribute]
  end
  options[:symbol] ? key.to_sym : key.to_s
end

#member_namesObject

Get configured member names


172
173
174
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 172

def member_names
  members.keys
end

#membersObject

Alias instance method for the class method.


167
168
169
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 167

def members
  self.class.members
end

#merge!(options, &block) ⇒ Object

Merge values


118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 118

def merge!(options, &block)
  if options.is_a? Hash
    options.each do |key, value|
      set(key, value)
    end
  elsif members[ContentKey]
    set(ContentKey, options)
  else
    raise ArgumentError, "invalid data(#{options.inspect}) for #{self.class.name} class"
  end
  self.instance_eval(&block) if block
  self
end

#set(key, value) ⇒ Object

Set value for given member

Arguments

  • key – member name

  • value – value for member


136
137
138
139
140
141
142
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 136

def set(key, value)
  send("#{key}=", value)
rescue NoMethodError => error
  logger.warn error.message
rescue TypeError, ArgumentError => error
  raise TypeError, "#{error.message}(#{value.inspect}) for #{self.class.name}.#{key} member"
end

#skip_value?(value) ⇒ Boolean

Skip nil, empty array and empty hash.

Returns:


188
189
190
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 188

def skip_value?(value)
  value.nil? || ( ( value.is_a?(Array) || value.is_a?(Hash) ) && value.empty? )
end

#to_hash(options = {}) ⇒ Object

Create Hash based configured members


177
178
179
180
181
182
183
184
185
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 177

def to_hash(options = {})
  options = HashOptions.merge(options)
  hash    = Util::OrderedHash.new
  member_names.each do |member|
    value = value_to_hash(instance_variable_get("@#{member}"), options)
    hash[hash_key(member, options)] = value unless skip_value?(value)
  end
  hash
end

#value_to_hash(value, options = {}) ⇒ Object

Covert the object to hash based on class.


209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/paypal-sdk/core/api/data_types/base.rb', line 209

def value_to_hash(value, options = {})
  case value
  when Array
    value = value.map{|object| value_to_hash(object, options) }
    value.delete_if{|v| skip_value?(v) }
    value
  when Base
    value.to_hash(options)
  else
    value
  end
end