Module: Utilise::Augment::Bool

Included in:
Array, Hash, Integer, String, Symbol
Defined in:
lib/utilise/augment/bool.rb

Overview

Extends classes that could be treated as a boolean

Instance Method Summary collapse

Instance Method Details

#bool_array(array) ⇒ Object

Iterates through array and updates each element to a boolean if it matches rules

Parameters:

  • array (Array)

    the array



35
36
37
38
39
40
# File 'lib/utilise/augment/bool.rb', line 35

def bool_array(array)
  array.each_index do |index|
    val = to_bool(array[index])
    array[index] = val unless val.nil?
  end
end

#bool_hash(hash) ⇒ Object

Iterates through hash and updates each key value to a boolean if it matches rules

Parameters:

  • hash (Hash)

    the hash



24
25
26
27
28
29
# File 'lib/utilise/augment/bool.rb', line 24

def bool_hash(hash)
  hash.each do |key, val|
    val = to_bool(val)
    hash[key] = val unless val.nil?
  end
end

#bool_it(object) ⇒ Object

Returns boolean value if object matches rules

Parameters:

  • object (Object)

    object to bool



46
47
48
49
50
51
52
53
# File 'lib/utilise/augment/bool.rb', line 46

def bool_it(object)
  case object.to_s
  when /^true$/i, /^yes$/i, /^t$/i, /^1$/i
    true
  when /^false$/i, /^no$/i, /^f$/i, /^0$/i
    false
  end
end

#to_bool(object = self) ⇒ Object

Returns bool based on object passed

Parameters:

  • object (Object) (defaults to: self)

    the object

Returns:

  • (Object)

    returns object boolean



9
10
11
12
13
14
15
16
17
18
# File 'lib/utilise/augment/bool.rb', line 9

def to_bool(object = self)
  case object
  when String, Integer, Symbol
    bool_it object
  when Hash
    bool_hash object
  when Array
    bool_array object
  end
end