Class: PagarMe::NestedModel
Constant Summary
PagarMeObject::RESOURCES
Instance Attribute Summary collapse
#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
#==, #[]=, 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_id ⇒ Object
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 = (*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
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 = (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
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_name ⇒ Object
23
24
25
|
# File 'lib/pagarme/nested_model.rb', line 23
def parent_resource_name
raise NotImplementedError
end
|
.url(parent_id, *params) ⇒ Object
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
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
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
|