Class: Query
Instance Attribute Summary collapse
-
#callback_blocks ⇒ Object
todo: rename all these *_block attributes to remove the ‘_block’ from the name.
-
#partial_search_fields ⇒ Object
todo: clean this up.
-
#query_block ⇒ Object
todo: rename all these *_block attributes to remove the ‘_block’ from the name.
-
#results_block ⇒ Object
todo: rename all these *_block attributes to remove the ‘_block’ from the name.
-
#transform_block ⇒ Object
todo: rename all these *_block attributes to remove the ‘_block’ from the name.
Class Method Summary collapse
-
.all ⇒ Object
todo: change this to queries.
- .find_by_name(name) ⇒ Object
Instance Method Summary collapse
-
#allow_partial_match?(field = nil) ⇒ Boolean
todo: remove to_sym (everywhere in code), use hash_with_indifferent_attributes.
-
#allow_partial_search_for(*fields) ⇒ Object
todo: change naming of this method to make it more consistent.
- #collection(name = nil) ⇒ Object
- #data ⇒ Object
-
#default_sort_by(sort_fields) ⇒ Object
todo: rename for consistensy.
- #define_query(&block) ⇒ Object
- #exact_match ⇒ Object
-
#initialize(name) ⇒ Query
constructor
A new instance of Query.
- #limit ⇒ Object
-
#limit_results_to(count) ⇒ Object
todo: refactor to use attribute for consistency.
- #listen_for(callback, &block) ⇒ Object
- #offset ⇒ Object
- #params ⇒ Object
- #partial_match ⇒ Object
-
#query ⇒ Object
todo: refactor @filters and @default_sort_fields to method calls.
- #require_search? ⇒ Boolean
- #results(&block) ⇒ Object
-
#show_all_on_load ⇒ Object
todo: review since it is redundant in both Report and Query.
- #show_all_on_load? ⇒ Boolean
- #transform(&block) ⇒ Object
- #view_name ⇒ Object
- #where ⇒ Object
Methods included from 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_blocks ⇒ Object
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_fields ⇒ Object
todo: clean this up
4 5 6 |
# File 'lib/athergin/query.rb', line 4 def partial_search_fields @partial_search_fields end |
#query_block ⇒ Object
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_block ⇒ Object
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_block ⇒ Object
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
.all ⇒ Object
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
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 |
#data ⇒ Object
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_match ⇒ Object
67 68 69 |
# File 'lib/athergin/query.rb', line 67 def exact_match Platform.exact_match? || @exact_match || false end |
#limit ⇒ Object
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 |
#offset ⇒ Object
63 64 65 |
# File 'lib/athergin/query.rb', line 63 def offset Platform.query_offset || @offset || 0 end |
#params ⇒ Object
55 56 57 |
# File 'lib/athergin/query.rb', line 55 def params Platform.search_params end |
#partial_match ⇒ Object
71 72 73 |
# File 'lib/athergin/query.rb', line 71 def partial_match !exact_match end |
#query ⇒ Object
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
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_load ⇒ Object
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
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_name ⇒ Object
140 141 142 |
# File 'lib/athergin/query.rb', line 140 def view_name :query end |
#where ⇒ Object
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 |