Class: Trailblazer::Finder::Find

Inherits:
Object
  • Object
show all
Defined in:
lib/trailblazer/finder/find.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity, params, filters, paging = nil, sorting = nil, config = nil) ⇒ Find

Returns a new instance of Find.



8
9
10
11
12
13
14
15
# File 'lib/trailblazer/finder/find.rb', line 8

def initialize(entity, params, filters, paging = nil, sorting = nil, config = nil)
  @entity   = entity
  @filters  = filters
  @params   = params
  @paging   = paging || {}
  @sorting = sorting || {}
  @config = config || {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



6
7
8
# File 'lib/trailblazer/finder/find.rb', line 6

def config
  @config
end

#filtersObject (readonly)

Returns the value of attribute filters.



6
7
8
# File 'lib/trailblazer/finder/find.rb', line 6

def filters
  @filters
end

#pagingObject (readonly)

Returns the value of attribute paging.



6
7
8
# File 'lib/trailblazer/finder/find.rb', line 6

def paging
  @paging
end

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'lib/trailblazer/finder/find.rb', line 6

def params
  @params
end

#sortingObject (readonly)

Returns the value of attribute sorting.



6
7
8
# File 'lib/trailblazer/finder/find.rb', line 6

def sorting
  @sorting
end

Instance Method Details

#process_filters(ctx) ⇒ Object



17
18
19
20
21
22
23
# File 'lib/trailblazer/finder/find.rb', line 17

def process_filters(ctx)
  @params.reduce(@entity) do |entity, (name, value)|
    filter = @filters[name.to_sym] || @filters[name]
    new_entity = ctx.instance_exec entity, filter[:name], value, &filter[:handler]
    new_entity || entity
  end
end

#process_paging(ctx) ⇒ Object



25
26
27
28
# File 'lib/trailblazer/finder/find.rb', line 25

def process_paging(ctx)
  ctx.instance_exec @paging[:current_page], @paging[:per_page],
                    (@sorting.empty? ? (process_filters ctx) : (process_sorting ctx)), &@paging[:handler]
end

#process_sorting(ctx) ⇒ Object



30
31
32
# File 'lib/trailblazer/finder/find.rb', line 30

def process_sorting(ctx)
  ctx.instance_exec @sorting, (process_filters ctx), &@sorting[:handler]
end

#query(ctx) ⇒ Object



34
35
36
37
38
39
# File 'lib/trailblazer/finder/find.rb', line 34

def query(ctx)
  return process_paging ctx unless @paging.empty? || @paging.nil?
  return process_sorting ctx unless @sorting.empty? || @sorting.nil?

  process_filters ctx
end