Class: Pickel::Predicate

Inherits:
Object
  • Object
show all
Defined in:
lib/pickel/predicate.rb

Constant Summary collapse

AREL =
%i[eq in not_eq not_in lt lteq gt gteq matches].freeze
MATCHES =
%i[cont start end].flat_map { |x| [x, "not_#{x}".to_sym] }.freeze
LITERAL =
%i[true false null].flat_map { |x| [x, "not_#{x}".to_sym] }.freeze
DERIVED =
%i[blank present].freeze
ESCAPE_ADAPTERS =
%w[Mysql2 PostgreSQL].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id) ⇒ Predicate

Returns a new instance of Predicate.



22
23
24
# File 'lib/pickel/predicate.rb', line 22

def initialize(id)
  @id = id
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



25
26
27
# File 'lib/pickel/predicate.rb', line 25

def id
  @id
end

Class Method Details

.allObject



12
13
14
15
# File 'lib/pickel/predicate.rb', line 12

def all
  @all ||= [AREL, MATCHES, LITERAL, DERIVED].flatten
    .sort_by { |id| -id.size }.map { |id| new(id) }
end

.find(id) ⇒ Object



17
18
19
# File 'lib/pickel/predicate.rb', line 17

def find(id)
  all.find { |predicate| id.end_with?("_#{predicate.id}") }
end

Instance Method Details

#build(klass, column, value) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/pickel/predicate.rb', line 27

def build(klass, column, value)
  v = convert(value)
  args =
    case id
    when *AREL
      klass.arel_table[column].public_send(id, v)
    when *MATCHES
      klass.arel_table[column].matches(v)
    when :blank
      klass.arel_table[column].eq_any([nil, ''])
    when :present
      klass.arel_table[column].not_eq_all([nil, ''])
    else
      { column => v }
    end

  when_not?(value) ? klass.where.not(args) : klass.where(args)
end