Module: Clearly::Query::Compose::Conditions

Includes:
Comparison, Core, Range, Special, Subset, Validate
Included in:
Clearly::Query::Composer
Defined in:
lib/clearly/query/compose/conditions.rb

Overview

Methods for building conditions.

Constant Summary collapse

OPERATORS_LOGICAL =

Logical query operators

[:and, :or, :not]
OPERATORS_COMPARISON =

Comparison query operators

[
:eq, :equal,
:not_eq, :not_equal,
:lt, :less_than,
:not_lt, :not_less_than,
:gt, :greater_than,
:not_gt, :not_greater_than,
:lteq, :less_than_or_equal,
:not_lteq, :not_less_than_or_equal,
:gteq, :greater_than_or_equal,
:not_gteq, :not_greater_than_or_equal]
OPERATORS_RANGE =

range query operators

[
:range, :in_range,
:not_range, :not_in_range]
OPERATORS_SUBSET =

Subset query operators

[
:in, :is_in,
:not_in, :is_not_in,
:contains, :contain,
:not_contains, :not_contain, :does_not_contain,
:starts_with, :start_with,
:not_starts_with, :not_start_with, :does_not_start_with,
:ends_with, :end_with,
:not_ends_with, :not_end_with, :does_not_end_with]
OPERATORS_REGEX =

Regex query operators

[
:regex, :regex_match, :matches,
:not_regex, :not_regex_match, :does_not_match, :not_match]
OPERATORS_SPECIAL =

Special query operators (null)

[:null, :is_null]
OPERATORS =

All query operators except logical operators All query operators except logical operators

OPERATORS_COMPARISON +
OPERATORS_RANGE +
OPERATORS_SUBSET +
OPERATORS_REGEX +
OPERATORS_SPECIAL

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

Methods included from Range

#parse_interval, #parse_range

Instance Method Details

#condition_combine(combiner, *conditions) ⇒ Arel::Nodes::Node

Combine multiple conditions.

Parameters:

  • combiner (Symbol)
  • conditions (Arel::Nodes::Node, Array<Arel::Nodes::Node>)

Returns:

  • (Arel::Nodes::Node)

    condition


67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/clearly/query/compose/conditions.rb', line 67

def condition_combine(combiner, *conditions)
  conditions = [conditions].flatten
  validate_not_blank(conditions)
  validate_array(conditions)
  validate_condition(conditions[0])
  validate_array_items(conditions)

  combined_conditions = nil
  conditions.each do |condition|
    combined_conditions = condition_combine_new(combiner, combined_conditions, condition)
  end
  combined_conditions
end

#condition_combine_new(combiner, conditions, new_condition) ⇒ Arel::Nodes::Node

Combine multiple conditions with a new condition.

Parameters:

  • combiner (Symbol)
  • conditions (Arel::Nodes::Node, Array<Arel::Nodes::Node>)
  • new_condition (Arel::Nodes::Node)

Returns:

  • (Arel::Nodes::Node)

    condition


86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/clearly/query/compose/conditions.rb', line 86

def condition_combine_new(combiner, conditions, new_condition)
  case combiner
    when :and
      conditions.nil? ? new_condition : compose_and(conditions, new_condition)
    when :or
     conditions.nil? ? new_condition : compose_or(conditions, new_condition)
    when :not
      not_condition = compose_not(new_condition)
      conditions.nil? ? not_condition : compose_and(conditions, not_condition)
    else
      fail Clearly::Query::QueryArgumentError.new("unrecognised logical operator '#{combiner}'")
  end
end

#condition_components(operator, table, column_name, valid_fields, value) ⇒ Arel::Nodes::Node

Build a condition.

Parameters:

  • operator (Symbol)
  • table (Arel::Table)
  • column_name (Symbol)
  • valid_fields (Array<symbol>)
  • value (Object)

Returns:

  • (Arel::Nodes::Node)

    condition


108
109
110
111
# File 'lib/clearly/query/compose/conditions.rb', line 108

def condition_components(operator, table, column_name, valid_fields, value)
  validate_table_column(table, column_name, valid_fields)
  condition_node(operator, table[column_name], value)
end

#condition_node(operator, node, value) ⇒ Arel::Nodes::Node

Build a condition.

Parameters:

  • operator (Symbol)
  • node (Arel::Nodes::Node, Arel::Attributes::Attribute, String)
  • value (Object)

Returns:

  • (Arel::Nodes::Node)

    condition


118
119
120
121
122
123
124
125
# File 'lib/clearly/query/compose/conditions.rb', line 118

def condition_node(operator, node, value)
  new_condition = condition_node_comparison(operator, node, value)
  new_condition = condition_node_subset(operator, node, value) if new_condition.nil?
  new_condition = compose_null_node(node, value) if new_condition.nil? && [:null, :is_null].include?(operator)

  fail Clearly::Query::QueryArgumentError.new("unrecognised operator '#{operator}'") if new_condition.nil?
  new_condition
end

#condition_node_comparison(operator, node, value) ⇒ Arel::Nodes::Node

Build a comparison condition.

Parameters:

  • operator (Symbol)
  • node (Arel::Nodes::Node, Arel::Attributes::Attribute, String)
  • value (Object)

Returns:

  • (Arel::Nodes::Node)

    condition


132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/clearly/query/compose/conditions.rb', line 132

def condition_node_comparison(operator, node, value)
  case operator
    when :eq, :equal
      compose_eq_node(node, value)
    when :not_eq, :not_equal
      compose_not_eq_node(node, value)
    when :lt, :less_than
      compose_lt_node(node, value)
    when :not_lt, :not_less_than
      compose_not_lt_node(node, value)
    when :gt, :greater_than
      compose_gt_node(node, value)
    when :not_gt, :not_greater_than
      compose_not_gt_node(node, value)
    when :lteq, :less_than_or_equal
      compose_lteq_node(node, value)
    when :not_lteq, :not_less_than_or_equal
      compose_not_lteq_node(node, value)
    when :gteq, :greater_than_or_equal
      compose_gteq_node(node, value)
    when :not_gteq, :not_greater_than_or_equal
      compose_not_gteq_node(node, value)
    else
      nil
  end
end

#condition_node_subset(operator, node, value) ⇒ Arel::Nodes::Node

Build a range or subset condition.

Parameters:

  • operator (Symbol)
  • node (Arel::Nodes::Node, Arel::Attributes::Attribute, String)
  • value (Object)

Returns:

  • (Arel::Nodes::Node)

    condition


164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/clearly/query/compose/conditions.rb', line 164

def condition_node_subset(operator, node, value)
  case operator
    when :range, :in_range
      compose_range_node(node, value)
    when :not_range, :not_in_range
      compose_not_range_node(node, value)
    when :in, :is_in
      compose_in_node(node, value)
    when :not_in, :is_not_in
      compose_not_in_node(node, value)
    when :contains, :contain
      compose_contains_node(node, value)
    when :not_contains, :not_contain, :does_not_contain
      compose_not_contains_node(node, value)
    when :starts_with, :start_with
      compose_starts_with_node(node, value)
    when :not_starts_with, :not_start_with, :does_not_start_with
      compose_not_starts_with_node(node, value)
    when :ends_with, :end_with
      compose_ends_with_node(node, value)
    when :not_ends_with, :not_end_with, :does_not_end_with
      compose_not_ends_with_node(node, value)
    when :regex, :regex_match, :matches
      compose_regex_node(node, value)
    when :not_regex, :not_regex_match, :does_not_match, :not_match
      compose_not_regex_node(node, value)
    else
      nil
  end
end