Sha256: d1758672597428f61587eea67282e55365e7c2003dde21921551b0a5297ed7e8
Contents?: true
Size: 1.47 KB
Versions: 13
Compression:
Stored size: 1.47 KB
Contents
module Almodovar class Resource include HttpAccessor undef_method :id if instance_methods.include?("id") undef_method :type if instance_methods.include?("type") delegate :inspect, to: :get! def self.from_xml(xml, auth = nil) new(nil, auth, Nokogiri::XML.parse(xml).root) end def initialize(url, auth, xml = nil, options = {}) @url = url @auth = auth @xml = xml @options = options end def method_missing(meth, *args, &blk) resource_object(meth).send(meth, *args, &blk) end def respond_to?(meth, include_all=false) super || resource_object(meth).respond_to?(meth, include_all) end private def get! klass = xml['type'] == 'array' ? ResourceCollection : SingleResource @resource_object = klass.new(@url, @auth, @xml, @options) end def collection_call?(meth, *args) ([].respond_to?(meth) && !["delete", "id", "[]"].include?(meth.to_s)) || ResourceCollection.instance_methods(false).map(&:to_s).include?(meth.to_s) || (meth.to_s == "[]" && args.size == 1 && args.first.is_a?(Fixnum)) end def resource_object(meth) @resource_object ||= resource_class(meth).new(@url, @auth, @xml, @options) end def resource_class(meth, *args) @resource_class ||= collection_call?(meth, *args) ? ResourceCollection : SingleResource end end def self.Resource(url, auth = nil, params = {}) Resource.new(url, auth, nil, params) end end
Version data entries
13 entries across 13 versions & 1 rubygems