class Mincer::Base

Attributes

args[RW]
relation[RW]
sql[RW]

Public Class Methods

active_processors() click to toggle source
# File lib/mincer/base.rb, line 23
def self.active_processors
  @processors ||= Mincer.processors.clone
end
new(scope, args = {}) click to toggle source

Builds query object

# File lib/mincer/base.rb, line 9
def initialize(scope, args = {})
  @args = if defined?(ActionController::Parameters) && args.is_a?(ActionController::Parameters) && args.respond_to?(:to_unsafe_h)
    ::ActiveSupport::HashWithIndifferentAccess.new(args.to_unsafe_h)
  else
    ::ActiveSupport::HashWithIndifferentAccess.new(args)
  end
  @scope, @relation = scope, build_query(scope, @args)
  execute_processors
end

Public Instance Methods

build_query(relation, args) click to toggle source

Must be implemented in any subclass

# File lib/mincer/base.rb, line 43
def build_query(relation, args)
  relation
end
each(&block) click to toggle source

Allows enumerable methods to be called directly on object

# File lib/mincer/base.rb, line 33
def each(&block)
  @collection ||= if @relation.is_a?(ActiveRecord::Relation)
    @relation.to_a
  else
    @relation.all
  end
  @collection.each(&block)
end
execute_processors() click to toggle source
# File lib/mincer/base.rb, line 19
def execute_processors
  self.class.active_processors.each {|processor| @relation = processor.new(self).apply }
end
method_missing(method_id, *params) click to toggle source

Pass methods to relation object

# File lib/mincer/base.rb, line 48
def method_missing(method_id, *params)
  @relation.send(method_id, *params)
end