Class: Poros::Query
Instance Attribute Summary collapse
-
#object ⇒ Object
Returns the value of attribute object.
-
#queries ⇒ Object
Returns the value of attribute queries.
Instance Method Summary collapse
- #each(&block) ⇒ Object
-
#initialize(object) ⇒ Query
constructor
A new instance of Query.
- #results ⇒ Object
- #where(query) ⇒ Object
Constructor Details
#initialize(object) ⇒ Query
Returns a new instance of Query.
6 7 8 9 |
# File 'lib/poros/query.rb', line 6 def initialize(object) @object = object @queries = {} end |
Instance Attribute Details
#object ⇒ Object
Returns the value of attribute object.
4 5 6 |
# File 'lib/poros/query.rb', line 4 def object @object end |
#queries ⇒ Object
Returns the value of attribute queries.
4 5 6 |
# File 'lib/poros/query.rb', line 4 def queries @queries end |
Instance Method Details
#each(&block) ⇒ Object
16 17 18 |
# File 'lib/poros/query.rb', line 16 def each(&block) results.each { |result| block.call(result) } end |
#results ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/poros/query.rb', line 20 def results indexed, table_scan = @queries.partition { |index, key| @object.poro_indexes.include?(index) } indexed_results = indexed.map { |key, value| case value when Regexp @object.index_data[key].keys.flat_map { |value_name| @object.index_data[key][value_name] if value =~ value_name }.compact when Array value.flat_map { |value_name| @object.index_data[key][value_name] } when Proc @object.index_data[key].keys.flat_map { |value_name| @object.index_data[key][value_name] if value.call(value_name) }.compact else @object.index_data[key].has_key?(value) ? @object.index_data[key][value] : [] end }.inject(:&) if table_scan.size > 0 scanned_results = Dir.glob(File.join(@object.data_directory, '*.yml')).map { |file| next if file == @object.index_file data = YAML.safe_load( File.read(file), permitted_classes: Poros::Config.configuration[:permitted_classes], ) data[:uuid] if table_scan.all? { |key, value| case value when Regexp value =~ data[key] when Array value.include?(data[key]) when Proc value.call(data[key]) else data[key] == value end } }.compact end if indexed.size > 0 && table_scan.size > 0 results = indexed_results & scanned_results elsif indexed.size > 0 results = indexed_results else results = scanned_results end results.map { |uuid| @object.find(uuid) } end |
#where(query) ⇒ Object
11 12 13 14 |
# File 'lib/poros/query.rb', line 11 def where(query) @queries.merge!(query) self end |