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
# File 'lib/mincer/base.rb', line 9

def initialize(scope, args = {})
  @scope, @args, @relation = scope, ::ActiveSupport::HashWithIndifferentAccess.new(args), 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



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

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



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

def sql
  @sql
end

Class Method Details

.active_processorsObject



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

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

Instance Method Details

#build_query(relation, args) ⇒ Object

Must be implemented in any subclass



38
39
40
# File 'lib/mincer/base.rb', line 38

def build_query(relation, args)
  relation
end

#each(&block) ⇒ Object

Allows enumerable methods to be called directly on object



28
29
30
31
32
33
34
35
# File 'lib/mincer/base.rb', line 28

def each(&block)
  @collection ||= if @relation.is_a?(ActiveRecord::Relation)
                    @relation.to_a
                  else
                    @relation.all
                  end
  @collection.each(&block)
end

#execute_processorsObject



14
15
16
# File 'lib/mincer/base.rb', line 14

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