Class: Code::Object::Boolean

Inherits:
Code::Object show all
Defined in:
lib/code/object/boolean.rb

Constant Summary collapse

CLASS_DOCUMENTATION =
{
  name: "Boolean",
  description: "represents true or false.",
  examples: %w[Boolean Boolean.new(true) Boolean.new(false)]
}.freeze
INSTANCE_FUNCTIONS =
{
  "&" => {
    name: "&",
    description: "returns true when both booleans are true.",
    examples: ["true & true", "true & false", "false & false"]
  },
  "bitwise_and" => {
    name: "bitwise_and",
    description: "returns true when both booleans are true.",
    examples: %w[
      true.bitwise_and(true)
      true.bitwise_and(false)
      false.bitwise_and(false)
    ]
  },
  "|" => {
    name: "|",
    description: "returns true when either boolean is true.",
    examples: ["true | false", "false | true", "false | false"]
  },
  "bitwise_or" => {
    name: "bitwise_or",
    description: "returns true when either boolean is true.",
    examples: %w[
      true.bitwise_or(false)
      false.bitwise_or(true)
      false.bitwise_or(false)
    ]
  },
  "^" => {
    name: "^",
    description: "returns true when exactly one boolean is true.",
    examples: ["true ^ false", "true ^ true", "false ^ false"]
  },
  "bitwise_xor" => {
    name: "bitwise_xor",
    description: "returns true when exactly one boolean is true.",
    examples: %w[
      true.bitwise_xor(false)
      true.bitwise_xor(true)
      false.bitwise_xor(false)
    ]
  }
}.freeze

Constants inherited from Code::Object

CLASS_FUNCTIONS, NUMBER_CLASSES

Constants included from Concerns::Shared

Concerns::Shared::COMPOUND_ASSIGNMENT_OPERATORS, Concerns::Shared::OPERATOR_METHOD_ALIASES, Concerns::Shared::SHARED_OPERATORS

Instance Attribute Summary

Attributes included from Concerns::Shared

#functions, #raw

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Code::Object

class_documentation, class_functions, code_new, #code_new, documentation, documentation_for, documented_functions_for, function_documentation_for, function_documentation_registry_for, functions, inherited_function_documentation_for, instance_functions, maybe, #name, repeat, sorted_dictionary, |

Methods included from Concerns::Shared

#<=>, #==, #as_json, #blank?, #code_and, #code_as_json, #code_blank?, #code_class_functions, #code_compare, #code_deep_duplicate, #code_different, #code_documentable_functions, #code_documentation, #code_duplicate, #code_dynamic_call, #code_equal, #code_exclamation_mark, #code_exclusive_range, #code_false?, #code_falsy?, code_fetch, #code_fetch, #code_functions, code_get, #code_get, #code_greater, #code_greater_or_equal, #code_has_key?, #code_inclusive_range, #code_inspect, #code_instance_functions, #code_instance_of?, #code_is_a?, #code_itself, #code_less, #code_less_or_equal, #code_name, #code_nothing?, #code_or, #code_presence, #code_presence_in, #code_present?, #code_respond_to?, #code_same_object?, #code_self, #code_send, code_set, #code_set, #code_something?, #code_strict_different, #code_strict_equal, #code_tap, #code_then, #code_to_boolean, #code_to_class, #code_to_date, #code_to_decimal, #code_to_dictionary, #code_to_duration, #code_to_integer, #code_to_json, #code_to_list, #code_to_nothing, #code_to_parameter, #code_to_range, #code_to_string, #code_to_time, #code_true?, #code_truthy?, #eql?, #falsy?, #hash, #inspect, #multi_fetch, #nothing?, #sig, #something?, #succ, #to_code, #to_i, #to_json, #to_s

Constructor Details

#initialize(*args, **_kargs, &_block) ⇒ Boolean

Returns a new instance of Boolean.



62
63
64
65
# File 'lib/code/object/boolean.rb', line 62

def initialize(*args, **_kargs, &_block)
  self.raw =
    (args.first.is_an?(Object) ? args.first.truthy? : !!args.first)
end

Class Method Details

.function_documentation(scope) ⇒ Object



56
57
58
59
60
# File 'lib/code/object/boolean.rb', line 56

def self.function_documentation(scope)
  return INSTANCE_FUNCTIONS if scope == :instance

  {}
end

Instance Method Details

#call(**args) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/code/object/boolean.rb', line 67

def call(**args)
  code_operator = args.fetch(:operator, nil).to_code
  code_arguments = args.fetch(:arguments, []).to_code
  code_value = code_arguments.code_first

  case code_operator.to_s
  when "&", "bitwise_and"
    sig(args) { Boolean }
    code_bitwise_and(code_value)
  when "^", "bitwise_xor"
    sig(args) { Boolean }
    code_bitwise_xor(code_value)
  when "|", "bitwise_or"
    sig(args) { Boolean }
    code_bitwise_or(code_value)
  else
    super
  end
end

#code_bitwise_and(other) ⇒ Object



87
88
89
90
91
# File 'lib/code/object/boolean.rb', line 87

def code_bitwise_and(other)
  code_other = other.to_code

  Boolean.new(raw & code_other.raw)
end

#code_bitwise_or(other) ⇒ Object



93
94
95
96
97
# File 'lib/code/object/boolean.rb', line 93

def code_bitwise_or(other)
  code_other = other.to_code

  Boolean.new(raw | code_other.raw)
end

#code_bitwise_xor(other) ⇒ Object



99
100
101
102
103
# File 'lib/code/object/boolean.rb', line 99

def code_bitwise_xor(other)
  code_other = other.to_code

  Boolean.new(raw ^ code_other.raw)
end

#present?Boolean

Returns:



109
110
111
# File 'lib/code/object/boolean.rb', line 109

def present?
  raw.present?
end

#truthy?Boolean

Returns:



105
106
107
# File 'lib/code/object/boolean.rb', line 105

def truthy?
  raw
end