Sha256: e013e27fdd28ce7189d49be6452a210cf5575dcda43c52e46316df0a748add0a

Contents?: true

Size: 1.06 KB

Versions: 5

Compression:

Stored size: 1.06 KB

Contents

require 'spyke/exceptions'

module Spyke
  class Relation
    include Enumerable

    attr_reader :klass, :params
    delegate :to_ary, :[], :empty?, :last, :size, :metadata, to: :find_some

    def initialize(klass, options = {})
      @klass, @options, @params = klass, options, {}
    end

    def all
      where
    end

    def where(conditions = {})
      @params.merge!(conditions)
      self
    end

    def find_one
      @find_one ||= klass.new_from_result(fetch)
    end

    def find_some
      @find_some ||= klass.new_collection_from_result(fetch)
    end

    def each
      find_some.each { |record| yield record }
    end

    def uri
      @options[:uri]
    end

    private

      def method_missing(name, *args, &block)
        if klass.respond_to?(name)
          scoping { klass.send(name, *args) }
        else
          super
        end
      end

      # Keep hold of current scope while running a method on the class
      def scoping
        klass.current_scope = self
        yield
      ensure
        klass.current_scope = nil
      end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
spyke-1.1.1 lib/spyke/relation.rb
spyke-1.1.0 lib/spyke/relation.rb
spyke-1.0.2 lib/spyke/relation.rb
spyke-1.0.1 lib/spyke/relation.rb
spyke-1.0.0 lib/spyke/relation.rb