Class: Pursuit::PredicateTransform

Inherits:
Parslet::Transform
  • Object
show all
Defined in:
lib/pursuit/predicate_transform.rb

Overview

Transform for the tree produced by ‘Pursuit::PredicateParser`.

See Also:

Constant Summary collapse

AGGREGATE_MODIFIERS =

Returns The list of supported aggregate modifiers, and the method name to invoke on the attribute node in order to obtain the function node.

Returns:

  • (Hash<String, Symbol>)

    The list of supported aggregate modifiers, and the method name to invoke on the attribute node in order to obtain the function node.

{
  '#' => :count,
  '*' => :sum,
  '+' => :maximum,
  '-' => :minimum,
  '~' => :average
}.freeze

Class Method Summary collapse

Class Method Details

.aggregate_attribute(context) ⇒ Object



170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/pursuit/predicate_transform.rb', line 170

def aggregate_attribute(context)
  raise AggregateModifiersNotAvailable unless context[:permit_aggregate_modifiers]

  attribute_name = context[:attribute].to_sym
  attribute = context.dig(:permitted_attributes, attribute_name)
  raise AttributeNotFound, attribute_name if attribute.blank?

  aggregate_modifier_name = context[:aggregate_modifier].to_s
  aggregate_modifier = AGGREGATE_MODIFIERS[aggregate_modifier_name]
  raise AggregateModifierNotFound, aggregate_modifier_name unless aggregate_modifier

  attribute.public_send(aggregate_modifier)
end

.attribute(context) ⇒ Object

Raises:



161
162
163
164
165
166
167
168
# File 'lib/pursuit/predicate_transform.rb', line 161

def attribute(context)
  attribute_name = context[:attribute].to_sym
  attribute = context.dig(:permitted_attributes, attribute_name)
  raise AttributeNotFound, attribute_name if attribute.blank?
  raise AggregateModifierRequired, attribute_name if attribute.respond_to?(:name) && attribute.name == Arel.star

  attribute
end

.does_not_match_node(attribute, value) ⇒ Object



224
225
226
227
228
# File 'lib/pursuit/predicate_transform.rb', line 224

def does_not_match_node(attribute, value)
  value = ActiveRecord::Base.sanitize_sql_like(value) if value.is_a?(String)
  value = value.blank? ? '%' : "%#{value}%"
  attribute.does_not_match(value)
end

.eq_node(attribute, value) ⇒ Object



184
185
186
187
188
189
# File 'lib/pursuit/predicate_transform.rb', line 184

def eq_node(attribute, value)
  value = ActiveRecord::Base.sanitize_sql(value) if value.is_a?(String)
  return attribute.eq(value) if value.present?

  attribute.eq(nil).or(attribute.matches_regexp('^\s*$'))
end

.gt_node(attribute, value) ⇒ Object



198
199
200
201
# File 'lib/pursuit/predicate_transform.rb', line 198

def gt_node(attribute, value)
  value = ActiveRecord::Base.sanitize_sql(value) if value.is_a?(String)
  attribute.gt(value)
end

.gteq_node(attribute, value) ⇒ Object



203
204
205
206
# File 'lib/pursuit/predicate_transform.rb', line 203

def gteq_node(attribute, value)
  value = ActiveRecord::Base.sanitize_sql(value) if value.is_a?(String)
  attribute.gteq(value)
end

.lt_node(attribute, value) ⇒ Object



208
209
210
211
# File 'lib/pursuit/predicate_transform.rb', line 208

def lt_node(attribute, value)
  value = ActiveRecord::Base.sanitize_sql(value) if value.is_a?(String)
  attribute.lt(value)
end

.lteq_node(attribute, value) ⇒ Object



213
214
215
216
# File 'lib/pursuit/predicate_transform.rb', line 213

def lteq_node(attribute, value)
  value = ActiveRecord::Base.sanitize_sql(value) if value.is_a?(String)
  attribute.lteq(value)
end

.match_node(attribute, value) ⇒ Object



218
219
220
221
222
# File 'lib/pursuit/predicate_transform.rb', line 218

def match_node(attribute, value)
  value = ActiveRecord::Base.sanitize_sql_like(value) if value.is_a?(String)
  value = value.blank? ? '%' : "%#{value}%"
  attribute.matches(value)
end

.not_eq_node(attribute, value) ⇒ Object



191
192
193
194
195
196
# File 'lib/pursuit/predicate_transform.rb', line 191

def not_eq_node(attribute, value)
  value = ActiveRecord::Base.sanitize_sql(value) if value.is_a?(String)
  return attribute.not_eq(value) if value.present?

  attribute.not_eq(nil).and(attribute.does_not_match_regexp('^\s*$'))
end