Class: PagarMe::NestedModel

Inherits:
Model show all
Defined in:
lib/pagarme/nested_model.rb

Direct Known Subclasses

BulkAnticipation

Constant Summary

Constants inherited from PagarMeObject

PagarMeObject::RESOURCES

Instance Attribute Summary collapse

Attributes inherited from PagarMeObject

#attributes

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Model

class_name, create, extract_page_count_or_params, #save, underscored_class_name

Methods inherited from PagarMeObject

#==, #[]=, convert, #empty?, #respond_to?, #to_hash, #to_s, #unsaved_attributes

Constructor Details

#initialize(hash = Hash.new) ⇒ NestedModel

Returns a new instance of NestedModel.



5
6
7
8
9
# File 'lib/pagarme/nested_model.rb', line 5

def initialize(hash = Hash.new)
  hash = hash.dup
  @parent_id = hash.delete(:parent_id) || hash.delete('parent_id')
  super hash
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class PagarMe::PagarMeObject

Instance Attribute Details

#parent_idObject

Returns the value of attribute parent_id.



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

def parent_id
  @parent_id
end

Class Method Details

.all(parent_id, *args, **params) ⇒ Object



50
51
52
53
# File 'lib/pagarme/nested_model.rb', line 50

def all(parent_id, *args, **params)
  params = extract_page_count_or_params(*args, **params)
  find_by parent_id, params
end

.find_by(parent_id, params = Hash.new, page = nil, count = nil) ⇒ Object Also known as: find_by_hash

Raises:



39
40
41
42
43
44
45
46
47
# File 'lib/pagarme/nested_model.rb', line 39

def find_by(parent_id, params = Hash.new, page = nil, count = nil)
  params = extract_page_count_or_params(page, count, **params)
  raise RequestError.new('Invalid page count') if params[:page] < 1 or params[:count] < 1

  PagarMe::Request.get(url(parent_id), params: params).call.map do |object|
    object.parent_id = parent_id
    object
  end
end

.find_by_id(parent_id, id) ⇒ Object Also known as: find

Raises:



27
28
29
30
31
32
33
34
35
36
# File 'lib/pagarme/nested_model.rb', line 27

def find_by_id(parent_id, id)
  raise RequestError.new('Invalid ID')        unless        id.present?
  raise RequestError.new('Invalid parent ID') unless parent_id.present?

  object = PagarMe::Request.get(url parent_id, id).call
  if object
    object.parent_id = parent_id
  end
  object
end

.parent_resource_nameObject

Raises:

  • (NotImplementedError)


23
24
25
# File 'lib/pagarme/nested_model.rb', line 23

def parent_resource_name
  raise NotImplementedError
end

.url(parent_id, *params) ⇒ Object

Raises:



55
56
57
58
# File 'lib/pagarme/nested_model.rb', line 55

def url(parent_id, *params)
  raise RequestError.new('Invalid parent ID') unless parent_id.present?
  ["/#{parent_resource_name}", parent_id, "#{ CGI.escape underscored_class_name }s", *params].join '/'
end

Instance Method Details

#createObject



11
12
13
14
# File 'lib/pagarme/nested_model.rb', line 11

def create
  update PagarMe::Request.post(self.class.url(parent_id), params: to_hash).run
  self
end

#url(*params) ⇒ Object

Raises:



16
17
18
19
# File 'lib/pagarme/nested_model.rb', line 16

def url(*params)
  raise RequestError.new('Invalid ID') unless id.present?
  self.class.url parent_id, CGI.escape(id.to_s), *params
end