Class: Query

Inherits:
Object
  • Object
show all
Extended by:
Attribute
Defined in:
lib/athergin/query.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Attribute

attribute

Constructor Details

#initialize(name) ⇒ Query

Returns a new instance of Query.



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/athergin/query.rb', line 18

def initialize(name)
  @name = name
  @fields = {}
  @search_mapping = {}
  @partial_search_fields = []
  @default_sort_fields = {}
  @filters = {}
  @show_all_on_load = false
  @transform_block = Proc.new { raise 'Please implement the transform block in your query.' }
  @results_block = Proc.new { query.to_a.map { |row| self.instance_exec row, &transform_block } }
  @callback_blocks = {}
end

Instance Attribute Details

#callback_blocksObject

todo: rename all these *_block attributes to remove the ‘_block’ from the name



5
6
7
# File 'lib/athergin/query.rb', line 5

def callback_blocks
  @callback_blocks
end

#partial_search_fieldsObject

todo: clean this up



4
5
6
# File 'lib/athergin/query.rb', line 4

def partial_search_fields
  @partial_search_fields
end

#query_blockObject

todo: rename all these *_block attributes to remove the ‘_block’ from the name



5
6
7
# File 'lib/athergin/query.rb', line 5

def query_block
  @query_block
end

#results_blockObject

todo: rename all these *_block attributes to remove the ‘_block’ from the name



5
6
7
# File 'lib/athergin/query.rb', line 5

def results_block
  @results_block
end

#transform_blockObject

todo: rename all these *_block attributes to remove the ‘_block’ from the name



5
6
7
# File 'lib/athergin/query.rb', line 5

def transform_block
  @transform_block
end

Class Method Details

.allObject

todo: change this to queries



9
10
11
# File 'lib/athergin/query.rb', line 9

def all
  Report.all.map(&:query_objects).flatten
end

.find_by_name(name) ⇒ Object



13
14
15
# File 'lib/athergin/query.rb', line 13

def find_by_name(name)
  all.find { |query| query.name == name.try(:to_sym) }
end

Instance Method Details

#allow_partial_match?(field = nil) ⇒ Boolean

todo: remove to_sym (everywhere in code), use hash_with_indifferent_attributes

Returns:

  • (Boolean)


76
77
78
79
80
81
82
# File 'lib/athergin/query.rb', line 76

def allow_partial_match?(field=nil)
  if field.present?
    partial_match && partial_search_fields.include?(field.to_sym)
  else
    @partial_search_fields.present?
  end
end

#allow_partial_search_for(*fields) ⇒ Object

todo: change naming of this method to make it more consistent



46
47
48
# File 'lib/athergin/query.rb', line 46

def allow_partial_search_for(*fields)
  @partial_search_fields = fields.map(&:to_sym)
end

#collection(name = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/athergin/query.rb', line 31

def collection(name=nil)
  if name
    if name.is_a? Symbol
      @collection = name
    elsif name.is_a? String
      db,tbl = name.split('/').map(&:to_sym)
      database db
      collection tbl
    end
  else
    @collection
  end
end

#dataObject



118
119
120
# File 'lib/athergin/query.rb', line 118

def data
  self.instance_eval &results_block
end

#default_sort_by(sort_fields) ⇒ Object

todo: rename for consistensy



123
124
125
# File 'lib/athergin/query.rb', line 123

def default_sort_by(sort_fields)
  @default_sort_fields = sort_fields
end

#define_query(&block) ⇒ Object



106
107
108
# File 'lib/athergin/query.rb', line 106

def define_query(&block)
  @query_block = block
end

#exact_matchObject



67
68
69
# File 'lib/athergin/query.rb', line 67

def exact_match
  Platform.exact_match? || @exact_match || false
end

#limitObject



59
60
61
# File 'lib/athergin/query.rb', line 59

def limit
  Platform.query_limit || @limit || 1000
end

#limit_results_to(count) ⇒ Object

todo: refactor to use attribute for consistency



51
52
53
# File 'lib/athergin/query.rb', line 51

def limit_results_to(count)
  @limit = count
end

#listen_for(callback, &block) ⇒ Object



144
145
146
# File 'lib/athergin/query.rb', line 144

def listen_for(callback,&block)
  @callback_blocks[callback] = block
end

#offsetObject



63
64
65
# File 'lib/athergin/query.rb', line 63

def offset
  Platform.query_offset || @offset || 0
end

#paramsObject



55
56
57
# File 'lib/athergin/query.rb', line 55

def params
  Platform.search_params
end

#partial_matchObject



71
72
73
# File 'lib/athergin/query.rb', line 71

def partial_match
  !exact_match
end

#queryObject

todo: refactor @filters and @default_sort_fields to method calls



95
96
97
98
99
100
101
102
103
104
# File 'lib/athergin/query.rb', line 95

def query
  return self.instance_eval(&query_block) if query_block

  # todo: add namespace and report name in error message
  raise "No collection specified for '#{ name }'" if collection.nil?
  raise "No database specified for '#{ name }'" if database.nil?

  puts "find: #{ Platform.database_name(database.to_s) }/#{ collection } -> #{ where.inspect }"
  Platform.database(database.to_s)[collection.to_s].find(where).sort(@default_sort_fields).limit(limit).skip(offset)
end

#require_search?Boolean

Returns:

  • (Boolean)


132
133
134
# File 'lib/athergin/query.rb', line 132

def require_search?
  !show_all_on_load?
end

#results(&block) ⇒ Object



110
111
112
# File 'lib/athergin/query.rb', line 110

def results(&block)
  @results_block = block
end

#show_all_on_loadObject

todo: review since it is redundant in both Report and Query



128
129
130
# File 'lib/athergin/query.rb', line 128

def show_all_on_load
  @show_all_on_load = true
end

#show_all_on_load?Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/athergin/query.rb', line 136

def show_all_on_load?
  @show_all_on_load
end

#transform(&block) ⇒ Object



114
115
116
# File 'lib/athergin/query.rb', line 114

def transform(&block)
  @transform_block = block
end

#view_nameObject



140
141
142
# File 'lib/athergin/query.rb', line 140

def view_name
  :query
end

#whereObject



84
85
86
87
88
89
90
91
92
# File 'lib/athergin/query.rb', line 84

def where
  params.map do |field,value|
    field = search_mapping[field.to_sym]
    next if field.nil?

    value = /#{ value }/i if allow_partial_match? field
    [field,value]
  end.compact.to_h.merge @filters
end