-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathboolean.rb
50 lines (41 loc) · 965 Bytes
/
boolean.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module Locator
module Boolean
class Terms < Array
def and!(other)
replace(self.dup.and(other))
end
def and(other)
other.empty? ? self : And.new(self, other)
end
alias & and
def or!(other)
replace(self.dup.or(other))
end
def or(other)
other.empty? ? self : Or.new(self, other)
end
alias | and
def to_s
flatten.join(Or.operator)
end
end
module Accessors
def operator=(operator); @operator = operator end
def operator; @operator end
end
class And < Terms
extend Accessors
self.operator = ' AND '
def initialize(lft, rgt)
replace(Array(lft).map { |l| Array(rgt).map { |r| "#{l}#{self.class.operator}#{r}" } })
end
end
class Or < Terms
extend Accessors
self.operator = ' OR '
def initialize(lft, rgt)
replace([lft, rgt])
end
end
end
end