Sha256: 7b9338e6585eff9d406a63ac877e45a3f7150b8619aede4ab579b6245f584583

Contents?: true

Size: 1.47 KB

Versions: 7

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 get!
      klass = xml['type'] == 'array' ? ResourceCollection : SingleResource
      @resource_object = klass.new(@url, @auth, @xml, @options)
    end
    
    def respond_to?(meth)
      super || resource_object(meth).respond_to?(meth)
    end
  
    private
  
    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

7 entries across 7 versions & 1 rubygems

Version Path
almodovar-1.2.0 lib/almodovar/resource.rb
almodovar-1.1.2 lib/almodovar/resource.rb
almodovar-1.1.1 lib/almodovar/resource.rb
almodovar-1.1.0 lib/almodovar/resource.rb
almodovar-1.1.0-x86_64-darwin-13 lib/almodovar/resource.rb
almodovar-1.0.0 lib/almodovar/resource.rb
almodovar-1.0.0.pre lib/almodovar/resource.rb