Class: Puppet_X::Coi::Jboss::BuildinsUtils::ToBooleanConverter

Inherits:
Object
  • Object
show all
Defined in:
lib/puppet_x/coi/jboss/buildins_utils.rb

Overview

Thsi class handles convertion from various types to boolean

Instance Method Summary collapse

Constructor Details

#initialize(target) ⇒ ToBooleanConverter

Constructor

Parameters:

  • target (Object)

    to be converted



34
35
36
# File 'lib/puppet_x/coi/jboss/buildins_utils.rb', line 34

def initialize(target)
  @target = target
end

Instance Method Details

#to_boolObject

Converts given value to boolean value

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puppet_x/coi/jboss/buildins_utils.rb', line 38

def to_bool
  if @target.respond_to?(:empty?)
    str = @target
  else
    str = @target.to_s
  end
  if @target.is_a? Numeric
    return @target != 0
  end
  return true if @target == true || str =~ (/(true|t|yes|y)$/i)
  bm = BlankMatcher.new(@target)
  return false if @target == false || bm.blank? || str =~ (/(false|f|no|n)$/i)
  raise ArgumentError.new("invalid value for Boolean: \"#{@target}\"")
end