Class: Mincer::Base

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/mincer/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(scope, args = {}) ⇒ Base

Builds query object



9
10
11
12
13
14
15
16
17
# 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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *params) ⇒ Object

Pass methods to relation object



48
49
50
# File 'lib/mincer/base.rb', line 48

def method_missing(method_id, *params)
  @relation.send(method_id, *params)
end

Instance Attribute Details

#argsObject

Returns the value of attribute args.



6
7
8
# File 'lib/mincer/base.rb', line 6

def args
  @args
end

#relationObject

Returns the value of attribute relation.



6
7
8
# File 'lib/mincer/base.rb', line 6

def relation
  @relation
end

#sqlObject

Grabs relation raw sql



28
29
30
# File 'lib/mincer/base.rb', line 28

def sql
  @sql
end

Class Method Details

.active_processorsObject



23
24
25
# File 'lib/mincer/base.rb', line 23

def self.active_processors
  @processors ||= Mincer.processors.clone
end

Instance Method Details

#build_query(relation, args) ⇒ Object

Must be implemented in any subclass



43
44
45
# File 'lib/mincer/base.rb', line 43

def build_query(relation, args)
  relation
end

#each(&block) ⇒ Object

Allows enumerable methods to be called directly on object



33
34
35
36
37
38
39
40
# 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_processorsObject



19
20
21
# File 'lib/mincer/base.rb', line 19

def execute_processors
  self.class.active_processors.each {|processor| @relation = processor.new(self).apply }
end