Class: BB::Converter::Formula

Inherits:
Object
  • Object
show all
Defined in:
lib/b_b/converter/formula.rb

Constant Summary collapse

TEMPLATE =
{
  between:      "%s BETWEEN %s",
  contains:     "%s CONTAINS %s",
  equals:       "%s = %s",
  gt:           "%s > %s",
  gteq:         "%s >= %s",
  in:           "%s IN %s",
  is:           "%s IS %s",
  lt:           "%s < %s",
  lteq:         "%s <= %s",
  match:        "REGEXP_MATCH(%s, %s)",
  not_between:  "NOT %s BETWEEN %s",
  not_contains: "NOT %s CONTAINS %s",
  not_equals:   "%s <> %s",
  not_gt:       "NOT %s > %s",
  not_gteq:     "NOT %s >= %s",
  not_in:       "NOT %s IN %s",
  not_is:       "%s IS NOT %s",
  not_lt:       "NOT %s < %s",
  not_lteq:     "NOT %s <= %s",
  not_match:    "NOT REGEXP_MATCH(%s, %s)"
}.freeze
OPERATORS_DICTIONARY =
{
  cont:         :contains,
  contains:     :contains,
  eq:           :equals,
  eql:          :equals,
  equals:       :equals,
  gt:           :gt,
  gteq:         :gteq,
  like:         :contains,
  lt:           :lt,
  lteq:         :lteq,
  not_cont:     :not_contains,
  not_contains: :not_contains,
  not_eq:       :not_equals,
  not_eql:      :not_equals,
  not_equals:   :not_equals,
  not_gt:       :not_gt,
  not_gteq:     :not_gteq,
  not_like:     :not_contains,
  not_lt:       :not_lt,
  not_lteq:     :not_lteq
}.freeze
SEARCHABLE_COLUMN_FORMAT =
/(?:(\w+)_(not_\w+)|(\w+)_(\w+))/

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(column, value, options = {}) ⇒ Formula

Returns a new instance of Formula.



54
55
56
57
58
# File 'lib/b_b/converter/formula.rb', line 54

def initialize(column, value, options = {})
  @column  = format_column(column)
  @value   = format_value(value)
  @options = options
end

Instance Attribute Details

#columnObject (readonly)

Returns the value of attribute column.



52
53
54
# File 'lib/b_b/converter/formula.rb', line 52

def column
  @column
end

#operatorObject (readonly)

Returns the value of attribute operator.



52
53
54
# File 'lib/b_b/converter/formula.rb', line 52

def operator
  @operator
end

#optionsObject (readonly)

Returns the value of attribute options.



52
53
54
# File 'lib/b_b/converter/formula.rb', line 52

def options
  @options
end

#valueObject (readonly)

Returns the value of attribute value.



52
53
54
# File 'lib/b_b/converter/formula.rb', line 52

def value
  @value
end

Class Method Details

.convert(column, value, options = {}) ⇒ Object



81
82
83
# File 'lib/b_b/converter/formula.rb', line 81

def convert(column, value, options = {})
  new(column, value, options).convert
end

Instance Method Details

#convertObject



60
61
62
63
# File 'lib/b_b/converter/formula.rb', line 60

def convert
  evaluated_type = Evaluator::Formula.eval_type(value, options.merge(operator: operator))
  evaluated_type && format(TEMPLATE[evaluated_type], column, value.convert)
end