Module: Clearly::Query::Compose::Range

Includes:
Validate
Included in:
Conditions, Custom, Definition
Defined in:
lib/clearly/query/compose/range.rb

Overview

Methods for composing range queries.

Instance Method Summary collapse

Methods included from Validate

#like_syntax, #sanitize_like_value, #sanitize_similar_to_value, #validate_array, #validate_array_items, #validate_association, #validate_boolean, #validate_condition, #validate_definition, #validate_definition_instance, #validate_float, #validate_hash, #validate_integer, #validate_model, #validate_name, #validate_node_or_attribute, #validate_not_blank, #validate_query, #validate_spec_association, #validate_symbol, #validate_table, #validate_table_column

Instance Method Details

#parse_interval(value) ⇒ Array<String>

Parse an interval.

Parameters:

  • value (String)

Returns:

  • (Array<String>)

    captures


12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/clearly/query/compose/range.rb', line 12

def parse_interval(value)
  range_regex = /(\[|\()(.*),(.*)(\)|\])/i
  matches = value.match(range_regex)
  fail Clearly::Query::QueryArgumentError.new(
           "range string must be in the form (|[.*,.*]|), got '#{value}'") unless matches

  captures = matches.captures
  {
      start_include: captures[0] == '[',
      start_value: captures[1],
      end_value: captures[2],
      end_include: captures[3] == ']'
  }
end

#parse_range(hash) ⇒ Hash

Validate a range.

Parameters:

  • hash (Hash)

Returns:

  • (Hash)

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
# File 'lib/clearly/query/compose/range.rb', line 30

def parse_range(hash)
  unless hash.is_a?(Hash)
    fail Clearly::Query::QueryArgumentError.new(
             "range filter must be {'from': 'value', 'to': 'value'} " +
                 "or {'interval': '(|[.*,.*]|)'} got '#{hash}'", {hash: hash})

  end

  from = hash[:from]
  to = hash[:to]
  interval = hash[:interval]

  if !from.blank? && !to.blank? && !interval.blank?
    fail Clearly::Query::QueryArgumentError.new(
             "range filter must use either ('from' and 'to') or ('interval'), not both", {hash: hash})
  elsif from.blank? && !to.blank?
    fail Clearly::Query::QueryArgumentError.new(
             "range filter missing 'from'", {hash: hash})
  elsif !from.blank? && to.blank?
    fail Clearly::Query::QueryArgumentError.new(
             "range filter missing 'to'", {hash: hash})
  elsif !from.blank? && !to.blank?
    parse_interval("[#{from},#{to})")
  elsif !interval.blank?
    parse_interval(interval)
  else
    fail Clearly::Query::QueryArgumentError.new(
             "range filter did not contain ('from' and 'to') or ('interval'), got '#{hash}'", {hash: hash})
  end
end