Sha256: ce4a63efc4fb43c21f120aa5653482fa516f9ce5f7fbd404c0c53a4d2e954e95

Contents?: true

Size: 1.1 KB

Versions: 1

Compression:

Stored size: 1.1 KB

Contents

module FlexmlsApi
  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[0])
      end
            
    end
  end
end
    

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
flexmls_api-0.3.6 lib/flexmls_api/models/finders.rb