Class: ForestAdminDatasourceToolkit::Components::Query::ConditionTree::Nodes::ConditionTreeLeaf

Inherits:
ConditionTree
  • Object
show all
Includes:
Exceptions, Utils
Defined in:
lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ConditionTree

#apply, #nest, #replace_fields, #unnest

Constructor Details

#initialize(field, operator, value = nil) ⇒ ConditionTreeLeaf

Returns a new instance of ConditionTreeLeaf.



15
16
17
18
19
20
21
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 15

def initialize(field, operator, value = nil)
  @field = field
  @operator = operator.underscore
  @value = value
  valid_operator(@operator) if @operator
  super()
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



13
14
15
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 13

def field
  @field
end

#operatorObject (readonly)

Returns the value of attribute operator.



13
14
15
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 13

def operator
  @operator
end

#valueObject (readonly)

Returns the value of attribute value.



13
14
15
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 13

def value
  @value
end

Instance Method Details

#every_leaf {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



120
121
122
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 120

def every_leaf
  yield(self)
end

#for_each_leaf {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



116
117
118
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 116

def for_each_leaf
  yield(self)
end

#inverseObject



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 37

def inverse
  return override(operator: "not_#{@operator}") if Operators.exist?("not_#{@operator}")
  return override(operator: @operator[4..]) if @operator.start_with?('not')

  case @operator
  when Operators::BLANK
    override(operator: Operators::PRESENT)
  when Operators::PRESENT
    override(operator: Operators::BLANK)
  else
    raise ForestException, "Operator: #{@operator} cannot be inverted."
  end
end

#match(record, collection, timezone) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 62

def match(record, collection, timezone)
  field_value = Record.field_value(record, @field)
  column_type = ForestAdminDatasourceToolkit::Utils::Collection.get_field_schema(collection,
                                                                                 @field).column_type

  supported = [
    Operators::IN,
    Operators::EQUAL,
    Operators::LESS_THAN,
    Operators::GREATER_THAN,
    Operators::MATCH,
    Operators::STARTS_WITH,
    Operators::ENDS_WITH,
    Operators::LONGER_THAN,
    Operators::SHORTER_THAN,
    Operators::INCLUDES_ALL,
    Operators::NOT_IN,
    Operators::NOT_EQUAL,
    Operators::NOT_CONTAINS
  ]

  case @operator
  when Operators::IN
    @value.include?(field_value)
  when Operators::EQUAL
    field_value == @value
  when Operators::LESS_THAN
    field_value < @value
  when Operators::GREATER_THAN
    field_value > @value
  when Operators::MATCH
    field_value.is_a?(String) && @value.match(field_value)
  when Operators::STARTS_WITH
    field_value.is_a?(String) && field_value.start_with?(@value)
  when Operators::ENDS_WITH
    field_value.is_a?(String) && field_value.end_with?(@value)
  when Operators::LONGER_THAN
    field_value.is_a?(String) && field_value.length > @value
  when Operators::SHORTER_THAN
    field_value.is_a?(String) && field_value.length < @value
  when Operators::INCLUDES_ALL
    Array(@value).all? { |v| field_value.include?(v) }
  when Operators::NOT_IN, Operators::NOT_EQUAL, Operators::NOT_CONTAINS
    !inverse.match(record, collection, timezone)
  else
    ConditionTreeEquivalent.get_equivalent_tree(
      self,
      supported,
      column_type,
      timezone
    )&.match(record, collection, timezone)
  end
end

#override(args) ⇒ Object



132
133
134
135
136
137
138
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 132

def override(args)
  ConditionTreeLeaf.new(
    args[:field] || @field,
    args[:operator] || @operator,
    args[:value].nil? ? @value : args[:value]
  )
end

#projectionObject



128
129
130
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 128

def projection
  Projection.new([@field])
end

#replace_leafsObject



51
52
53
54
55
56
57
58
59
60
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 51

def replace_leafs
  result = yield(self)
  if result.nil?
    nil
  elsif result.is_a?(ConditionTree)
    result
  else
    ConditionTreeFactory.from_plain_object(result)
  end
end

#some_leaf {|_self| ... } ⇒ Object

Yields:

  • (_self)

Yield Parameters:



124
125
126
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 124

def some_leaf
  yield(self)
end

#to_hObject



23
24
25
26
27
28
29
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 23

def to_h
  {
    field: @field,
    operator: @operator,
    value: @value
  }
end

#use_interval_operatorObject



140
141
142
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 140

def use_interval_operator
  Operators.interval_operators.include?(@operator)
end

#valid_operator(value) ⇒ Object

Raises:



31
32
33
34
35
# File 'lib/forest_admin_datasource_toolkit/components/query/condition_tree/nodes/condition_tree_leaf.rb', line 31

def valid_operator(value)
  return if Operators.exist?(value)

  raise ForestException, "Invalid operators, the #{value} operator does not exist."
end