module ChaosDetector::Utils::CoreUtil

Public Class Methods

assert(expected_result=true, msg=nil, &block) click to toggle source
# File lib/chaos_detector/utils/core_util.rb, line 55
def assert(expected_result=true, msg=nil, &block)
  if block.nil? && !expected_result
    raise AssertError, "Assertion failed. #{msg}"
  elsif !block.nil? && block.call != expected_result
    raise AssertError, "Assertion failed. #{msg}\n\t#{block.source_location}"
  end
end
aught?(obj) click to toggle source
# File lib/chaos_detector/utils/core_util.rb, line 45
def aught?(obj)
  !naught?(obj)
end
enum(*values) click to toggle source
# File lib/chaos_detector/utils/core_util.rb, line 16
def enum(*values)
  Module.new do |mod|
    values.each_with_index do |v, _i|
      mod.const_set(v.to_s.upcase, v.to_s.downcase.to_sym)
      # mod.const_set(v.to_s.upcase, 2**i)
    end

    def mod.values
      constants
    end
  end
end
naught?(obj) click to toggle source
# File lib/chaos_detector/utils/core_util.rb, line 29
def naught?(obj)
  if obj.nil?
    true
  elsif obj.is_a?(FalseClass)
    true
  elsif obj.is_a?(TrueClass)
    false
  elsif obj.is_a?(String)
    obj.strip.empty?
  elsif obj.is_a?(Enumerable)
    obj.none? { |o| aught?(o) }
  elsif obj.is_a?(Numeric)
    obj == 0
  end
end
properties_complement(props, obj:) click to toggle source

@return subset of given properties not contained within given object

# File lib/chaos_detector/utils/core_util.rb, line 64
def properties_complement(props, obj:)
  return props if obj.nil?
  raise ArgumentError, 'props is required.' unless props

  puts "(#{obj.class} )props: #{props}"

  props - case obj
          when Hash
            puts 'KKKKK'
            puts "obj.keys: #{obj.keys}"
            obj.keys

          else
            puts "PPPPP #{obj.class}"
            puts "obj.public_methods: #{obj.public_methods}"
            obj.public_methods

          end
end
test() click to toggle source

Built-in self-testing: ChaosDetector::Utils::CoreUtil.test

# File lib/chaos_detector/utils/core_util.rb, line 86
def test
  puts('Testing ChaosDetector::Utils::CoreUtil')
  assert(true, 'Naught detects blank string') {naught?('')}
  assert(true, 'Naught detects blank string with space') {naught?(' ')}
  assert(true, 'Naught detects int 0') {naught?(0)}
  assert(true, 'Naught detects float 0.0') {naught?(0.0)}
  assert(true, 'Naught detects empty array') {naught?([])}
  assert(true, 'Naught detects empty hash') {naught?({})}
  assert(false, 'Naught real string') {naught?('foobar')}
  assert(false, 'Naught non-zero int') {naught?(1)}
  assert(false, 'Naught non-zero float') {naught?(33.33)}
  assert(false, 'Naught non-empty array') {naught?(['stuff'])}
  assert(false, 'Naught non-empty hash') {naught?({ foo: 'bar' })}
end
with(obj, aught:false) { |obj| ... } click to toggle source
# File lib/chaos_detector/utils/core_util.rb, line 49
def with(obj, aught:false)
  raise ArgumentError('with requires block') unless block_given?

  yield obj if (aught ? aught?(obj) : !!obj)
end