# Author:: Eric Crane (mailto:eric.crane@mac.com) # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved. # # An object with an boolean value. # module Gloo module Objs class Boolean < Gloo::Core::Obj KEYWORD = 'boolean' KEYWORD_SHORT = 'bool' TRUE = 'true' FALSE = 'false' # # The name of the object type. # def self.typename return KEYWORD end # # The short name of the object type. # def self.short_typename return KEYWORD_SHORT end # # Set the value with any necessary type conversions. # def set_value new_value self.value = Gloo::Objs::Boolean.coerse_to_bool( new_value ) end # # Coerse the new value to a boolean value. # def self.coerse_to_bool new_value if new_value.nil? return false elsif new_value.class.name == "String" return true if new_value.strip.downcase == TRUE return false if new_value.strip.downcase == FALSE return true if new_value.strip.downcase == 't' return false if new_value.strip.downcase == 'f' elsif new_value.class.name == "Integer" return false if new_value == 0 return true else return new_value == true end end # # Is the given token a boolean? # def self.is_boolean? token return true if token == true return true if token == false if token.class.name == "String" return true if token.strip.downcase == TRUE return true if token.strip.downcase == FALSE end return false end # # Get the value for display purposes. # def value_display return value ? TRUE : FALSE end # --------------------------------------------------------------------- # Messages # --------------------------------------------------------------------- # # Get a list of message names that this object receives. # def self.messages return super + [ "not", "true", "false" ] end # Set the value to the opposite of what it is. def msg_not v = ! value set_value v $engine.heap.it.set_to v return v end # Set the value to true. def msg_true set_value true $engine.heap.it.set_to true return true end # Set the value to false. def msg_false set_value false $engine.heap.it.set_to false return false end end end end