class Pickel::Predicate
Constants
- AREL
- DERIVED
- ESCAPE_ADAPTERS
- LITERAL
- MATCHES
Attributes
id[R]
Public Class Methods
all()
click to toggle source
# 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)
click to toggle source
# File lib/pickel/predicate.rb, line 17 def find(id) all.find { |predicate| id.end_with?("_#{predicate.id}") } end
new(id)
click to toggle source
# File lib/pickel/predicate.rb, line 22 def initialize(id) @id = id end
Public Instance Methods
build(klass, column, value)
click to toggle source
# 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
Private Instance Methods
cast_bool(value)
click to toggle source
# File lib/pickel/predicate.rb, line 67 def cast_bool(value) ActiveModel::Type::Boolean.new.cast(value) end
convert(value)
click to toggle source
# File lib/pickel/predicate.rb, line 48 def convert(value) case id when :cont, :not_cont "%#{escape_wildcards(value)}%" when :start, :not_start "#{escape_wildcards(value)}%" when :end, :not_end "%#{escape_wildcards(value)}" when :true, :not_true true when :false, :not_false false when :null, :not_null nil else value end end
escape_wildcards(unescaped)
click to toggle source
replace % \ to % \
# File lib/pickel/predicate.rb, line 72 def escape_wildcards(unescaped) if ESCAPE_ADAPTERS.include?(ActiveRecord::Base.connection.adapter_name) unescaped.to_s.gsub(/([\\%_.])/, '\\\\\\1') else unescaped end end
when_not?(value)
click to toggle source
# File lib/pickel/predicate.rb, line 80 def when_not?(value) return false if AREL.include?(id) id.to_s.start_with?('not') || !cast_bool(value) end