Sha256: 15cf1dc3ac3579709b04c400349d007c3d457c9ac260fe6c99e5c7c8240f9188

Contents?: true

Size: 839 Bytes

Versions: 4

Compression:

Stored size: 839 Bytes

Contents

module DataMapper
  module SQLFinders
    def sql(*models)
      raise ArgumentError, "Block required" unless block_given?
      yield *TableRepresentation.from_models(*models)
    end

    def by_sql(*additional_models, &block)
      options = if additional_models.last.kind_of?(Hash)
        additional_models.pop
      else
        {}
      end

      sql, *bind_values = sql(self, *additional_models, &block)
      parts = SQLParser.new(sql).parse

      options[:limit]  ||= parts[:limit]  if parts[:limit]
      options[:offset] ||= parts[:offset] if parts[:offset]

      Collection.new(Query.new(repository, self, options).tap { |q| q.send(:sql=, parts, bind_values) })
    end

    def default_order(repository_name = default_repository_name)
      Array(super).map { |d| Query::DefaultDirection.new(d) }.freeze
    end
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
sbf-dm-sql-finders-0.0.4 lib/data_mapper/sql_finders.rb
dm-sql-finders-0.0.3 lib/data_mapper/sql_finders.rb
dm-sql-finders-0.0.2 lib/data_mapper/sql_finders.rb
dm-sql-finders-0.0.1 lib/data_mapper/sql_finders.rb