Sha256: 738e6c7c1b5087ce052bc0c1f57975987c7e2b4b5510395119ebe8b071bc9a0c
Contents?: true
Size: 1.77 KB
Versions: 7
Compression:
Stored size: 1.77 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, 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 alias :find_by_hash :find_by def all(parent_id, *args, **params) params = extract_page_count_or_params(*args, **params) find_by parent_id, params 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
7 entries across 7 versions & 1 rubygems