Sha256: e21290f109dd0c02d02cc555f21160e143d5e3687d1e0f3aaa4d501919fcc16b

Contents?: true

Size: 1.19 KB

Versions: 11

Compression:

Stored size: 1.19 KB

Contents

module ApiResource
  module Scopes
    extend ActiveSupport::Concern
    
    
    module ClassMethods
      def scopes
        return self.related_objects[:scope]
      end
      
      def scope(name, hsh)
        raise ArgumentError, "Expecting an attributes hash given #{hsh.inspect}" unless hsh.is_a?(Hash)
        self.related_objects[:scope][name.to_sym] = hsh
        # we also need to define a class method for each scope
        self.instance_eval <<-EOE, __FILE__, __LINE__ + 1
          def #{name}(*args)
            return #{ApiResource::Associations::ResourceScope.class_factory(hsh)}.new(self, :#{name}, *args)
          end
        EOE
      end
      
      def scope?(name)
        self.related_objects[:scope][name.to_sym].present?
      end
      
      def scope_attributes(name)
        raise "No such scope #{name}" unless self.scope?(name)
        self.related_objects[:scope][name.to_sym]
      end
    end
    module InstanceMethods
      def scopes
        return self.class.scopes
      end
      
      def scope?(name)
        return self.class.scope?(name)
      end
      
      def scope_attributes(name)
        return self.class.scope_attributes(name)
      end
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
api_resource-0.2.11 lib/api_resource/scopes.rb
api_resource-0.2.10 lib/api_resource/scopes.rb
api_resource-0.2.9 lib/api_resource/scopes.rb
api_resource-0.2.8 lib/api_resource/scopes.rb
api_resource-0.2.7 lib/api_resource/scopes.rb
api_resource-0.2.6 lib/api_resource/scopes.rb
api_resource-0.2.5 lib/api_resource/scopes.rb
api_resource-0.2.4 lib/api_resource/scopes.rb
api_resource-0.2.3 lib/api_resource/scopes.rb
api_resource-0.2.2 lib/api_resource/scopes.rb
api_resource-0.2.1 lib/api_resource/scopes.rb