Sha256: 7fcbc57a21689d5d91a30eb48856f6f298a65e647c6116914a67f0607072d3a4

Contents?: true

Size: 1.72 KB

Versions: 26

Compression:

Stored size: 1.72 KB

Contents

module Octopi
  module Resource
    def self.included(base)
      base.extend ClassMethods
      base.set_resource_name(base.name)
      (@@resources||={})[base.resource_name(:singular)] = base
      (@@resources||={})[base.resource_name(:plural)] = base
    end
    
    def self.for(name)
      @@resources[name]
    end
    
    module ClassMethods
      def set_resource_name(singular, plural = "#{singular}s")
        @resource_name = {:singular => declassify(singular), :plural => declassify(plural)}
      end
      
      def resource_name(key)
        @resource_name[key]
      end
      
      def create_path(path)
        (@path_spec||={})[:create] = path
      end
      
      def find_path(path)
        (@path_spec||={})[:find] = path
      end
    
      def resource_path(path)
        (@path_spec||={})[:resource] = path
      end
  
      def delete_path(path)
        (@path_spec||={})[:delete] = path
      end
      
      def find(*args)
        args = args.join('/') if args.is_a? Array
        result = Api.api.find(path_for(:resource), @resource_name[:singular], args, self, @cache)
        key = result.keys.first

        if result[key].is_a? Array
          result[key].map { |r| new(r) }
        else
          Resource.for(key).new(result[key])
        end
      end

      def find_all(*s)
        find_plural(s, :find)
      end
      
      def find_plural(s, path)
        s = s.join('/') if s.is_a? Array
        resources = Api.api.find_all(path_for(path), @resource_name[:plural], s, self)
        resources.map { |item| self.new(item) }
      end
      
      def declassify(s)
        (s.split('::').last || '').downcase if s
      end
    
      def path_for(type)
        @path_spec[type]
      end
    end
  end
end

Version data entries

26 entries across 26 versions & 4 rubygems

Version Path
octopi-0.4.5 lib/octopi/resource.rb
octopi-0.4.4 lib/octopi/resource.rb
octopi-0.4.3 lib/octopi/resource.rb
octopi-0.4.2 lib/octopi/resource.rb
octopi-0.4.1 lib/octopi/resource.rb
octopi-0.4.0 lib/octopi/resource.rb
octopi-0.3.0 lib/octopi/resource.rb
dcuddeback-octopi-0.2.12 lib/octopi/resource.rb
dcuddeback-octopi-0.2.11 lib/octopi/resource.rb
dcuddeback-octopi-0.2.9 lib/octopi/resource.rb
dcuddeback-octopi-0.2.8 lib/octopi/resource.rb
devver-octopi-0.2.14 lib/octopi/resource.rb
tpitale-octopi-0.3.0 lib/octopi/resource.rb
devver-octopi-0.2.13 lib/octopi/resource.rb
devver-octopi-0.2.12 lib/octopi/resource.rb
octopi-0.2.8 lib/octopi/resource.rb
devver-octopi-0.2.11 lib/octopi/resource.rb
devver-octopi-0.2.10 lib/octopi/resource.rb
devver-octopi-0.2.9 lib/octopi/resource.rb
devver-octopi-0.2.8 lib/octopi/resource.rb