Class: PagarMe::PagarMeObject

Inherits:
Object
  • Object
show all
Defined in:
lib/pagarme/object.rb

Constant Summary collapse

RESOURCES =
Dir[File.expand_path('../resources/*.rb', __FILE__)].map do |path|
  File.basename(path, '.rb').to_sym
end

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = {}) ⇒ PagarMeObject

Returns a new instance of PagarMeObject.



9
10
11
12
13
# File 'lib/pagarme/object.rb', line 9

def initialize(response = {})
  @attributes         = Hash.new
  @unsaved_attributes = Set.new
  update response
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (protected)



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/pagarme/object.rb', line 84

def method_missing(name, *args, &block)
  name = name.to_s

  unless block_given?
    if name.end_with?('=') && args.size == 1
      attribute_name = name[0...-1]
      return self[attribute_name] = args[0]
    end

    if args.size == 0
      return self[name] || self[name.to_sym]
    end
  end

  if attributes.respond_to? name
    return attributes.public_send name, *args, &block
  end

  super name, *args, &block
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



3
4
5
# File 'lib/pagarme/object.rb', line 3

def attributes
  @attributes
end

Class Method Details

.convert(response) ⇒ Object



107
108
109
110
111
112
113
114
115
116
# File 'lib/pagarme/object.rb', line 107

def convert(response)
  case response
  when Array
    response.map{ |i| convert i }
  when Hash
    resource_class_for(response['object']).new(response)
  else
    response
  end
end

Instance Method Details

#==(other) ⇒ Object



24
25
26
# File 'lib/pagarme/object.rb', line 24

def ==(other)
  self.class == other.class && id == other.id
end

#[]=(key, value) ⇒ Object



15
16
17
18
# File 'lib/pagarme/object.rb', line 15

def []=(key,value)
  @attributes[key] = value
  @unsaved_attributes.add key
end

#empty?Boolean

Returns:

  • (Boolean)


20
21
22
# File 'lib/pagarme/object.rb', line 20

def empty?
  @attributes.empty?
end

#respond_to?(name, include_all = false) ⇒ Boolean

Returns:

  • (Boolean)


40
41
42
43
44
# File 'lib/pagarme/object.rb', line 40

def respond_to?(name, include_all = false)
  return true if name.to_s.end_with? '='

  @attributes.has_key?(name.to_s) || super
end

#to_hashObject



34
35
36
37
38
# File 'lib/pagarme/object.rb', line 34

def to_hash
  Hash[@attributes.map do |key, value|
    [ key, to_hash_value(value, :to_hash) ]
  end]
end

#to_sObject Also known as: inspect



46
47
48
49
50
51
52
# File 'lib/pagarme/object.rb', line 46

def to_s
  attributes_str = ''
  (attributes.keys - ['id', 'object']).sort.each do |key|
    attributes_str += " \033[1;33m#{key}:\033[0m#{self[key].inspect}" unless self[key].nil?
  end
  "\033[1;31m#<#{self.class.name}:\033[0;32m#{id}#{attributes_str}\033[0m\033[0m\033[1;31m>\033[0;32m"
end

#unsaved_attributesObject



28
29
30
31
32
# File 'lib/pagarme/object.rb', line 28

def unsaved_attributes
  Hash[@unsaved_attributes.map do |key|
    [ key, to_hash_value(self[key], :unsaved_attributes) ]
  end]
end