Class: Query
- Inherits:
-
Object
- Object
- Query
- Defined in:
- lib/query.rb
Constant Summary collapse
- TERMINATING_FUNCTIONS =
[:pluck]
Instance Attribute Summary collapse
-
#argument ⇒ Object
readonly
Returns the value of attribute argument.
-
#attribute ⇒ Object
readonly
Returns the value of attribute attribute.
-
#condition ⇒ Object
readonly
Returns the value of attribute condition.
Instance Method Summary collapse
- #argument_is_block? ⇒ Boolean
- #attribute_present?(records) ⇒ Boolean
- #conditional(row, condition, argument) ⇒ Object
- #filter(results) ⇒ Object
- #handle_terminating_function(results) ⇒ Object
-
#initialize(attribute, condition, argument) ⇒ Query
constructor
A new instance of Query.
- #terminating_function? ⇒ Boolean
- #valid?(records) ⇒ Boolean
Constructor Details
#initialize(attribute, condition, argument) ⇒ Query
Returns a new instance of Query.
5 6 7 8 9 |
# File 'lib/query.rb', line 5 def initialize(attribute, condition, argument) @attribute = attribute @condition = condition @argument = argument end |
Instance Attribute Details
#argument ⇒ Object (readonly)
Returns the value of attribute argument.
2 3 4 |
# File 'lib/query.rb', line 2 def argument @argument end |
#attribute ⇒ Object (readonly)
Returns the value of attribute attribute.
2 3 4 |
# File 'lib/query.rb', line 2 def attribute @attribute end |
#condition ⇒ Object (readonly)
Returns the value of attribute condition.
2 3 4 |
# File 'lib/query.rb', line 2 def condition @condition end |
Instance Method Details
#argument_is_block? ⇒ Boolean
21 22 23 |
# File 'lib/query.rb', line 21 def argument_is_block? @argument.respond_to?(:call) end |
#attribute_present?(records) ⇒ Boolean
17 18 19 |
# File 'lib/query.rb', line 17 def attribute_present?(records) records.any? { |record| record.keys.include? @attribute } end |
#conditional(row, condition, argument) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/query.rb', line 48 def conditional(row, condition, argument) thing_to_check = row[@attribute] # for direct comparison methods case condition when :gt thing_to_check > argument when :lt thing_to_check < argument when :eq thing_to_check == argument when :between argument.include?(thing_to_check) when :ilike thing_to_check.downcase.include? argument.downcase when :lambda argument.call(OpenStruct.new(row)) # lambda takes the whole row else raise "Unknown Conditional: #{condition}" end end |
#filter(results) ⇒ Object
29 30 31 32 33 34 35 36 37 |
# File 'lib/query.rb', line 29 def filter(results) if @attribute.empty? handle_terminating_function(results) else results.select do |result| conditional(result, @condition, @argument) end end end |
#handle_terminating_function(results) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'lib/query.rb', line 39 def handle_terminating_function(results) case @condition when :pluck results.map { |r| r[@argument] } else raise "Unknown Terminating Function: #{@condition}" end end |
#terminating_function? ⇒ Boolean
25 26 27 |
# File 'lib/query.rb', line 25 def terminating_function? TERMINATING_FUNCTIONS.include?(@condition) end |
#valid?(records) ⇒ Boolean
11 12 13 14 15 |
# File 'lib/query.rb', line 11 def valid?(records) valid = attribute_present?(records) || argument_is_block? || terminating_function? raise ArgumentError.new("The attribute `#{@attribute}` is not present in the PStore.") unless valid valid end |