class Predicate::Dsl

Public Class Methods

new(var = nil, allow_currying = true) click to toggle source
# File lib/predicate/dsl.rb, line 4
def initialize(var = nil, allow_currying = true)
  @var = var || ::Predicate.var(".", :dig)
  @allow_currying = allow_currying
end

Public Instance Methods

method_missing(n, *args, &bl) click to toggle source
Calls superclass method
# File lib/predicate/dsl.rb, line 96
def method_missing(n, *args, &bl)
  snaked, to_negate = missing_method_pair(n)
  if snaked == n.to_s && !to_negate
    super
  elsif self.respond_to?(snaked)
    got = __send__(snaked.to_sym, *args, &bl)
    to_negate ? !got : got
  else
    super
  end
end
respond_to_missing?(n, include_private = false) click to toggle source
Calls superclass method
# File lib/predicate/dsl.rb, line 108
def respond_to_missing?(n, include_private = false)
  snaked, to_negate = missing_method_pair(n)
  return super if snaked == n.to_s
  self.respond_to?(snaked)
end

Private Instance Methods

apply_curry(name, args, on) click to toggle source
# File lib/predicate/dsl.rb, line 128
def apply_curry(name, args, on)
  m = on.instance_method(name)
  if @allow_currying and m.arity == 1+args.length
    [@var] + args
  else
    args
  end
end
missing_method_pair(n) click to toggle source
# File lib/predicate/dsl.rb, line 116
def missing_method_pair(n)
  name, to_negate = n.to_s, false
  if name.to_s[0..2] == "not"
    name, to_negate = name[3..-1], true
  end
  [to_snake_case(name), to_negate]
end
to_snake_case(str) click to toggle source
# File lib/predicate/dsl.rb, line 124
def to_snake_case(str)
  str.gsub(/[A-Z]/){|x| "_#{x.downcase}" }.gsub(/^_/, "")
end