class Paysafe::Result

Attributes

attributes[R]

@return [Hash]

Public Class Methods

attributes(*attrs) click to toggle source

Define methods that retrieve the value from attributes

@param attrs [Array, Symbol]

# File lib/paysafe/result.rb, line 8
def attributes(*attrs)
  attrs.each do |attr|
    define_attribute_method(attr)
    define_predicate_method(attr)
  end
end
define_attribute_method(key1, klass = nil) click to toggle source

Dynamically define a method for an attribute

@param key1 [Symbol] @param klass [Symbol]

# File lib/paysafe/result.rb, line 28
def define_attribute_method(key1, klass = nil)
  define_method(key1) do
    if instance_variable_defined?("@#{key1}")
      return instance_variable_get("@#{key1}")
    end
    instance_variable_set("@#{key1}", get_value_by(key1, klass))
  end
end
define_predicate_method(key1, key2 = key1) click to toggle source

Dynamically define a predicate method for an attribute

@param key1 [Symbol] @param key2 [Symbol]

# File lib/paysafe/result.rb, line 41
def define_predicate_method(key1, key2 = key1)
  define_method(:"#{key1}?") do
    !attr_falsey_or_empty?(key2)
  end
end
new(attributes={}) click to toggle source

Initializes a new object

@param attributes [Hash] @return [Paysafe::Result]

# File lib/paysafe/result.rb, line 52
def initialize(attributes={})
  @attributes = attributes || {}
end
object_attribute(klass, key1) click to toggle source

Define object methods from attributes

@param klass [Symbol] @param key1 [Symbol]

# File lib/paysafe/result.rb, line 19
def object_attribute(klass, key1)
  define_attribute_method(key1, klass)
  define_predicate_method(key1)
end

Public Instance Methods

empty?() click to toggle source

@return [Boolean]

# File lib/paysafe/result.rb, line 60
def empty?
  @attributes.empty?
end

Private Instance Methods

attr_falsey_or_empty?(key) click to toggle source
# File lib/paysafe/result.rb, line 66
def attr_falsey_or_empty?(key)
  !@attributes[key] || @attributes[key].respond_to?(:empty?) && @attributes[key].empty?
end
get_value_by(key, klass = nil) click to toggle source
# File lib/paysafe/result.rb, line 70
def get_value_by(key, klass = nil)
  return @attributes[key] if klass.nil?

  case @attributes[key]
  when Hash
    Paysafe.const_get(klass).new(@attributes[key])
  when Array
    @attributes[key].map do |value|
      Paysafe.const_get(klass).new(value)
    end
  else
    @attributes[key]
  end
end