module Clearly::Query::Compose::Conditions

Methods for building conditions.

Constants

OPERATORS

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

OPERATORS_COMPARISON

Comparison query operators

OPERATORS_LOGICAL

Logical query operators

OPERATORS_RANGE

range query operators

OPERATORS_REGEX

Regex query operators

OPERATORS_SPECIAL

Special query operators (null)

OPERATORS_SUBSET

Subset query operators

Public Instance Methods

condition_combine(combiner, *conditions) click to toggle source

Combine multiple conditions. @param [Symbol] combiner @param [Arel::Nodes::Node, Array<Arel::Nodes::Node>] conditions @return [Arel::Nodes::Node] condition

# 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) click to toggle source

Combine multiple conditions with a new condition. @param [Symbol] combiner @param [Arel::Nodes::Node, Array<Arel::Nodes::Node>] conditions @param [Arel::Nodes::Node] new_condition @return [Arel::Nodes::Node] condition

# 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) click to toggle source

Build a condition. @param [Symbol] operator @param [Arel::Table] table @param [Symbol] column_name @param [Array<symbol>] valid_fields @param [Object] value @return [Arel::Nodes::Node] condition

# 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) click to toggle source

Build a condition. @param [Symbol] operator @param [Arel::Nodes::Node, Arel::Attributes::Attribute, String] node @param [Object] value @return [Arel::Nodes::Node] condition

# 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) click to toggle source

Build a comparison condition. @param [Symbol] operator @param [Arel::Nodes::Node, Arel::Attributes::Attribute, String] node @param [Object] value @return [Arel::Nodes::Node] condition

# 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) click to toggle source

Build a range or subset condition. @param [Symbol] operator @param [Arel::Nodes::Node, Arel::Attributes::Attribute, String] node @param [Object] value @return [Arel::Nodes::Node] condition

# 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