Class: Yargi::Predicate

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

Overview

class IndexPredicate

Constant Summary collapse

ALL =

Predicates that always return true

TruePredicate.new
NONE =

Predicates that always return false

FalsePredicate.new

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.to_predicate(what = nil, &block) ⇒ Object

Builds a ‘what and block’ predicate, according the automatic conversions descibed above.



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/yargi/predicate.rb', line 20

def to_predicate(what=nil, &block)
  (what, block = block, nil) if what.nil?
  p = case what
    when Predicate
      what
    when Module
      TagPredicate.new(what)
    when Proc
      LambdaPredicate.new(&what)
    when TrueClass, NilClass
      ALL
    when FalseClass
      NONE
    when Integer
      IndexPredicate.new(what)
    else
      raise ArgumentError, "Unable to convert #{what} to a predicate"
  end
  if block
    p & LambdaPredicate.new(&block)
  else
    p
  end
end

Instance Method Details

#&(right) ⇒ Object

Builds a ‘self and right’ predicate



47
48
49
# File 'lib/yargi/predicate.rb', line 47

def &(right)
  AndPredicate.new(*[self, Predicate.to_predicate(right)])
end

#notObject

Builds a ‘not(self)’ predicate



57
58
59
# File 'lib/yargi/predicate.rb', line 57

def not()
  NotPredicate.new(self)
end

#|(right) ⇒ Object

Builds a ‘self or right’ predicate



52
53
54
# File 'lib/yargi/predicate.rb', line 52

def |(right)
  OrPredicate.new(*[self, Predicate.to_predicate(right)])
end