Class: LtdTemplate::Value::Boolean

Inherits:
Code
  • Object
show all
Defined in:
lib/ltdtemplate/value/boolean.rb

Instance Attribute Summary

Attributes inherited from Code

#tpl_methods

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code

#do_method, #do_set, #get_item, #has_item?, #is_set?, #set_item, #set_value

Constructor Details

#initialize(template, bool) ⇒ Boolean

Returns a new instance of Boolean.



17
18
19
20
# File 'lib/ltdtemplate/value/boolean.rb', line 17

def initialize (template, bool)
  super template
  @bool = bool
end

Class Method Details

.instance(template, bool) ⇒ Object

Use one shared true value and one shared false value per template.



12
13
14
15
# File 'lib/ltdtemplate/value/boolean.rb', line 12

def self.instance (template, bool)
  template.factory_singletons[bool ? :bool_true : :bool_false] ||=
    self.new(template, bool)
end

Instance Method Details

#do_and(opts) ⇒ Object

Implement */& (and): bool&(bool1, …, boolN) True if ALL booleans are true. Evaluates {} blocks until false.



56
57
58
59
60
61
62
63
64
# File 'lib/ltdtemplate/value/boolean.rb', line 56

def do_and (opts)
  if @bool and params = opts[:parameters]
 params.positional.each do |tval|
    return @template.factory :boolean, false unless
tval.get_value(:method => 'call').to_boolean
 end
  end
  self
end

#do_not(opts) ⇒ Object

Implement ! (not): bool!(bool1, …, boolN) True if ALL booleans are false. Evaluates {} blocks until true.



69
70
71
72
73
74
75
76
77
# File 'lib/ltdtemplate/value/boolean.rb', line 69

def do_not (opts)
  if !@bool and params = opts[:parameters]
 params.positional.each do |tval|
    return @template.factory :boolean, false if
tval.get_value(:method => 'call').to_boolean
 end
  end
  @template.factory :boolean, !@bool
end

#do_or(opts) ⇒ Object

Implement +/| (or): bool|(bool1, …, boolN) True if ANY boolean is true. Evaluates {} blocks until true.



43
44
45
46
47
48
49
50
51
# File 'lib/ltdtemplate/value/boolean.rb', line 43

def do_or (opts)
  if not @bool and params = opts[:parameters]
 params.positional.each do |tval|
    return @template.factory :boolean, true if
tval.get_value(:method => 'call').to_boolean
 end
  end
  self
end

#get_value(opts = {}) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/ltdtemplate/value/boolean.rb', line 26

def get_value (opts = {})
  case opts[:method]
  when nil, 'call' then self
  when 'class' then @template.factory :string, 'Boolean'
  when 'str', 'string' then @template.factory :string,
    (@bool ? 'true' : 'false')
  when 'type' then @template.factory :string, 'boolean'
  when '+', '|', 'or' then do_or opts
  when '*', '&', 'and' then do_and opts
  when '!', 'not' then do_not opts
  else do_method opts, 'Boolean'
  end
end

#to_booleanObject



22
# File 'lib/ltdtemplate/value/boolean.rb', line 22

def to_boolean; @bool; end

#to_nativeObject



23
# File 'lib/ltdtemplate/value/boolean.rb', line 23

def to_native; @bool; end

#to_textObject



24
# File 'lib/ltdtemplate/value/boolean.rb', line 24

def to_text; ''; end