Sha256: 64feaf18af607e93aa7f2bc51179d0210228add96b2f1c81bb4defde0109a0e7
Contents?: true
Size: 1.68 KB
Versions: 1
Compression:
Stored size: 1.68 KB
Contents
module PagarMe class NestedModel < Model attr_accessor :parent_id def initialize(hash = Hash.new) hash = hash.dup @parent_id = hash.delete(:parent_id) || hash.delete('parent_id') super hash end def create update PagarMe::Request.post(self.class.url(parent_id), params: to_hash).run self end def url(*params) raise RequestError.new('Invalid ID') unless id.present? self.class.url parent_id, CGI.escape(id.to_s), *params end class << self def parent_resource_name raise NotImplementedError end 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 alias :find :find_by_id def find_by(parent_id, hash, page = 1, count = 10) raise RequestError.new('Invalid page count') if page < 1 or count < 1 PagarMe::Request.get(url(parent_id), params: hash.merge( page: page, count: count )).call.map do |object| object.parent_id = parent_id object end end alias :find_by_hash :find_by def all(parent_id, page = 1, count = 10) find_by parent_id, Hash.new, page, count end 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 end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
pagarme-2.1.3 | lib/pagarme/nested_model.rb |