Sha256: cd0818428545f4d4d01f996ea166cd9ff49b2a23b1a4c412c22492a4e13d67e6

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

module Morpheus
  module Finders

    def self.included(base)
      base.extend(ClassMethods)
    end

    module ClassMethods

      def find(*args)
        options = args.extract_options!
        if args.length == 1
          case args.first
          when Integer, String
            attributes = [UrlBuilder.find_one(self, args.first.to_i), nil, { :id => args.first.to_i }]
            get(*attributes)
          when Array
            attributes = UrlBuilder.find_some(self, args.first.sort).push({ :ids => args.first })
            get(*attributes)
          else
            raise ArgumentError, "Unrecognized argument (#{args.first.inspect})."
          end
        else
          attributes = UrlBuilder.find_some(self, args.sort).push({ :ids => args })
          get(*attributes)
        end
      end

      def all
        attributes = [UrlBuilder.find_all(self), nil, {}]
        get(*attributes)
      end

      def first
        limit(1).first
      end

      def where(query_attributes)
        Relation.new(self).where(query_attributes)
      end

      def limit(amount)
        Relation.new(self).limit(amount)
      end

      def page(page_number)
        Relation.new(self).page(page_number)
      end

      def results_per_page
        @results_per_page || 10
      end

      def results_per_page=(_results_per_page)
        @results_per_page = _results_per_page
      end

    end

  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
morpheus-0.3.9 lib/morpheus/mixins/finders.rb
morpheus-0.3.8 lib/morpheus/mixins/finders.rb
morpheus-0.3.7 lib/morpheus/mixins/finders.rb