Sha256: 9e58b8909a18ea826800cdc74f1d6ea3f81e9d37f963ee77137d6bdce27703aa

Contents?: true

Size: 1.11 KB

Versions: 7

Compression:

Stored size: 1.11 KB

Contents

module SparkApi
  module Models
    # =Rails-like finders module
    # Adds the base set of finder class methods to the models that support them (not all of them do) 
    module Finders

      def find(*arguments)
        scope = arguments.slice!(0)
        options = arguments.slice!(0) || {}
        case scope
          when :all   then find_every(options)
          when :first then find_every(options).first
          when :last  then find_every(options).last
          when :one   then find_one(options)
          else             find_single(scope, options)
        end
      end
      
      def first(*arguments)
        find(:first, *arguments)
      end
      
      def last(*arguments)
        find(:last, *arguments)
      end
      
      private

      def find_every(options)
        collect(connection.get("/#{element_name}", options))
      end

      def find_one(options)
        raise NotImplementedError # TODO um... what?
      end

      def find_single(scope, options)
        resp = connection.get("/#{element_name}/#{scope}", options)
        new(resp.first)
      end
            
    end
  end
end
    

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spark_api-1.2.0 lib/spark_api/models/finders.rb
spark_api-1.1.1 lib/spark_api/models/finders.rb
spark_api-1.1.0 lib/spark_api/models/finders.rb
spark_api-1.0.4 lib/spark_api/models/finders.rb
spark_api-1.0.2 lib/spark_api/models/finders.rb
spark_api-1.0.1 lib/spark_api/models/finders.rb
spark_api-1.0.0 lib/spark_api/models/finders.rb