Class: LcpRuby::Search::OperatorRegistry

Inherits:
Object
  • Object
show all
Defined in:
lib/lcp_ruby/search/operator_registry.rb

Constant Summary collapse

NUMERIC_OPERATORS =
%i[eq not_eq gt gteq lt lteq between present blank null not_null].freeze
RELATIVE_DATE_OPERATORS =

Operators resolved at query time to absolute date ranges (not native Ransack predicates)

%i[last_n_days this_week this_month this_quarter this_year].freeze
TEMPORAL_OPERATORS =
(NUMERIC_OPERATORS + RELATIVE_DATE_OPERATORS).freeze
OPERATORS_BY_TYPE =
{
  string:   %i[eq not_eq cont not_cont start not_start end not_end in not_in present blank null not_null],
  text:     %i[cont not_cont present blank null not_null],
  integer:  %i[eq not_eq gt gteq lt lteq between in not_in present blank null not_null],
  float:    NUMERIC_OPERATORS,
  decimal:  NUMERIC_OPERATORS,
  boolean:  %i[true not_true false not_false null not_null],
  date:     TEMPORAL_OPERATORS,
  datetime: TEMPORAL_OPERATORS,
  enum:     %i[eq not_eq in not_in present blank null not_null],
  uuid:     %i[eq not_eq in not_in present blank null not_null],
  array:    %i[array_contains array_overlaps present blank null not_null]
}.freeze
ALL_OPERATORS =

Union of all operators across all types (used for validation and metadata)

OPERATORS_BY_TYPE.values.flatten.uniq.freeze
NO_VALUE_OPERATORS =

Operators that require no value input

%i[present blank null not_null true not_true false not_false
this_week this_month this_quarter this_year].freeze
MULTI_VALUE_OPERATORS =

Operators that accept multiple values

%i[in not_in].freeze
RANGE_OPERATORS =

Operators that accept two values (from + to)

%i[between].freeze
PARAMETERIZED_OPERATORS =

Operators that require a numeric parameter (e.g., “last N days” -> N)

%i[last_n_days].freeze

Class Method Summary collapse

Class Method Details

.label_for(operator) ⇒ Object

Returns the i18n-backed label for an operator.



46
47
48
49
50
51
# File 'lib/lcp_ruby/search/operator_registry.rb', line 46

def self.label_for(operator)
  I18n.t(
    "lcp_ruby.search.operators.#{operator}",
    default: operator.to_s.humanize
  )
end

.multi_value?(operator) ⇒ Boolean

Returns true if the operator accepts multiple values.

Returns:

  • (Boolean)


59
60
61
# File 'lib/lcp_ruby/search/operator_registry.rb', line 59

def self.multi_value?(operator)
  MULTI_VALUE_OPERATORS.include?(operator.to_sym)
end

.no_value?(operator) ⇒ Boolean

Returns true if the operator requires no value input.

Returns:

  • (Boolean)


54
55
56
# File 'lib/lcp_ruby/search/operator_registry.rb', line 54

def self.no_value?(operator)
  NO_VALUE_OPERATORS.include?(operator.to_sym)
end

.operators_for(field_type) ⇒ Object

Returns the list of operator symbols for a given field type.



41
42
43
# File 'lib/lcp_ruby/search/operator_registry.rb', line 41

def self.operators_for(field_type)
  OPERATORS_BY_TYPE[field_type.to_sym] || []
end

.parameterized?(operator) ⇒ Boolean

Returns true if the operator requires a numeric parameter.

Returns:

  • (Boolean)


69
70
71
# File 'lib/lcp_ruby/search/operator_registry.rb', line 69

def self.parameterized?(operator)
  PARAMETERIZED_OPERATORS.include?(operator.to_sym)
end

.range?(operator) ⇒ Boolean

Returns true if the operator accepts two values (from + to).

Returns:

  • (Boolean)


64
65
66
# File 'lib/lcp_ruby/search/operator_registry.rb', line 64

def self.range?(operator)
  RANGE_OPERATORS.include?(operator.to_sym)
end

.relative_date?(operator) ⇒ Boolean

Returns true if the operator is a relative date operator (resolved at query time).

Returns:

  • (Boolean)


74
75
76
# File 'lib/lcp_ruby/search/operator_registry.rb', line 74

def self.relative_date?(operator)
  RELATIVE_DATE_OPERATORS.include?(operator.to_sym)
end