Sha256: 1702ef312145038a7731b3063ddb4f401777e6b7072bb66237de5b5f3f8f9134

Contents?: true

Size: 1.18 KB

Versions: 49

Compression:

Stored size: 1.18 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 nil    then raise ArgumentError, "Argument for find() can't be nil"
          when :all   then find_every(options)
          when :first then find_every(options).first
          when :last  then find_every(options).last
          when :one   then find_every(options.merge(:_limit => 1)).first
          else             find_single(scope, options)
        end
      end

      def find_one(*arguments)
        find(:one, *arguments)
      end
      
      def first(*arguments)
        find(:first, *arguments)
      end
      
      def last(*arguments)
        find(:last, *arguments)
      end
      
      private

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

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

Version data entries

49 entries across 49 versions & 1 rubygems

Version Path
spark_api-1.4.24 lib/spark_api/models/finders.rb
spark_api-1.4.23 lib/spark_api/models/finders.rb
spark_api-1.4.22 lib/spark_api/models/finders.rb
spark_api-1.4.21 lib/spark_api/models/finders.rb
spark_api-1.4.20 lib/spark_api/models/finders.rb
spark_api-1.4.19 lib/spark_api/models/finders.rb
spark_api-1.4.18 lib/spark_api/models/finders.rb
spark_api-1.4.17 lib/spark_api/models/finders.rb
spark_api-1.4.16 lib/spark_api/models/finders.rb
spark_api-1.4.15 lib/spark_api/models/finders.rb
spark_api-1.4.14 lib/spark_api/models/finders.rb
spark_api-1.4.13 lib/spark_api/models/finders.rb
spark_api-1.4.12 lib/spark_api/models/finders.rb
spark_api-1.4.11 lib/spark_api/models/finders.rb
spark_api-1.4.10 lib/spark_api/models/finders.rb
spark_api-1.4.9 lib/spark_api/models/finders.rb
spark_api-1.4.8 lib/spark_api/models/finders.rb
spark_api-1.4.6 lib/spark_api/models/finders.rb
spark_api-1.4.5 lib/spark_api/models/finders.rb
spark_api-1.4.4 lib/spark_api/models/finders.rb