Class: Paysafe::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/paysafe/result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Paysafe::Result

Initializes a new object

Parameters:

  • attributes (Hash) (defaults to: {})


52
53
54
# File 'lib/paysafe/result.rb', line 52

def initialize(attributes={})
  @attributes = attributes || {}
end

Instance Attribute Details

#attributesHash (readonly)

Returns:

  • (Hash)


57
58
59
# File 'lib/paysafe/result.rb', line 57

def attributes
  @attributes
end

Class Method Details

.attributes(*attrs) ⇒ Object

Define methods that retrieve the value from attributes

Parameters:

  • attrs (Array, Symbol)


8
9
10
11
12
13
# 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) ⇒ Object

Dynamically define a method for an attribute

Parameters:

  • key1 (Symbol)
  • klass (Symbol) (defaults to: nil)


28
29
30
31
32
33
34
35
# 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) ⇒ Object

Dynamically define a predicate method for an attribute

Parameters:

  • key1 (Symbol)
  • key2 (Symbol) (defaults to: key1)


41
42
43
44
45
# 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

.object_attribute(klass, key1) ⇒ Object

Define object methods from attributes

Parameters:

  • klass (Symbol)
  • key1 (Symbol)


19
20
21
22
# File 'lib/paysafe/result.rb', line 19

def object_attribute(klass, key1)
  define_attribute_method(key1, klass)
  define_predicate_method(key1)
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/paysafe/result.rb', line 60

def empty?
  @attributes.empty?
end