Module: Teepee::CommanderMixins::Boolean

Included in:
Teepee::Commander
Defined in:
lib/teepee/commander-mixins/boolean.rb

Instance Method Summary collapse

Instance Method Details

#boolean_and(booleans) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/teepee/commander-mixins/boolean.rb', line 66

def boolean_and booleans
  if booleans.empty?
    return true_constant
  end
  b = booleans.first.to_html
  if false_constant? b
    false_constant
  elsif true_constant? b or booleans.first.kind_of? WhitespaceToken
    boolean_and booleans[1..-1]
  else
    command_error "Not a boolean value #{booleans.first}"
  end
end

#boolean_nand(booleans) ⇒ Object



80
81
82
# File 'lib/teepee/commander-mixins/boolean.rb', line 80

def boolean_nand booleans
  boolean_not boolean_and booleans
end

#boolean_nor(booleans) ⇒ Object



84
85
86
# File 'lib/teepee/commander-mixins/boolean.rb', line 84

def boolean_nor booleans
  boolean_not boolean_or booleans
end

#boolean_not(boolean) ⇒ Object



88
89
90
91
92
93
94
95
96
97
# File 'lib/teepee/commander-mixins/boolean.rb', line 88

def boolean_not boolean
  boolean = boolean.to_html
  if true_constant? boolean
    false_constant
  elsif false_constant? boolean
    true_constant
  else
    command_error "Not a boolean value"
  end
end

#boolean_or(booleans) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/teepee/commander-mixins/boolean.rb', line 99

def boolean_or booleans
  if booleans.empty?
    return false_constant
  end
  b = booleans.first.to_html
  if true_constant? b
    true_constant
  elsif false_constant? b or booleans.first.kind_of? WhitespaceToken
    boolean_or booleans[1..-1]
  else
    command_error "Not a boolean value"
  end
end

#boolean_xnor(booleans) ⇒ Object



113
114
115
# File 'lib/teepee/commander-mixins/boolean.rb', line 113

def boolean_xnor booleans
  boolean_not boolean_xor booleans
end

#boolean_xor(booleans) ⇒ Object



117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/teepee/commander-mixins/boolean.rb', line 117

def boolean_xor booleans
  # There are two schools of thought as to what a multi-variable XOR is.
  # 1. Chained XORs, giving a parity check.
  # 2. 'Exclusively' one true for ALL inputs.
  # I'm going with the second: one and only one true, the rest false.
  # It seems therefore that the zero-argument version should be false then.
  if booleans.empty?
    false_constant
  else
    any_trues = false
    booleans.each do |boolean|
      if true_constant? boolean
        if any_trues
          return false_constant
        else
          any_trues = true
        end
      elsif false_constant? boolean
      # do nothing
      elsif boolean.kind_of? WhitespaceToken
      # do nothing
      else
        return command_error "Not a boolean value"
      end
    end
    return any_trues.to_html
  end
end

#ensure_boolean(boolean) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/teepee/commander-mixins/boolean.rb', line 42

def ensure_boolean boolean
  if boolean.to_html == "true" or boolean.to_html == "false"
    boolean
  else
    command_error "Non-boolean value."
  end
end

#false_constantObject



54
55
56
# File 'lib/teepee/commander-mixins/boolean.rb', line 54

def false_constant
  "false"
end

#false_constant?(expression) ⇒ Boolean

Returns:



62
63
64
# File 'lib/teepee/commander-mixins/boolean.rb', line 62

def false_constant? expression
  expression.to_html == "false"
end

#true_constantObject



50
51
52
# File 'lib/teepee/commander-mixins/boolean.rb', line 50

def true_constant
  "true"
end

#true_constant?(expression) ⇒ Boolean

Returns:



58
59
60
# File 'lib/teepee/commander-mixins/boolean.rb', line 58

def true_constant? expression
  expression.to_html == "true"
end