Class: Necromancer::BooleanConverters::BooleanToIntegerConverter

Inherits:
Converter
  • Object
show all
Defined in:
lib/necromancer/converters/boolean.rb

Overview

An object that converts a Boolean to an Integer

Instance Attribute Summary

Attributes inherited from Converter

#config, #convert, #source, #target

Instance Method Summary collapse

Methods inherited from Converter

create, #initialize, #raise_conversion_type

Constructor Details

This class inherits a constructor from Necromancer::Converter

Instance Method Details

#call(value, strict: config.strict) ⇒ Object

Convert boolean to integer

Examples:

converter.call(true)   # => 1
converter.call(false)  # => 0


70
71
72
73
74
75
76
# File 'lib/necromancer/converters/boolean.rb', line 70

def call(value, strict: config.strict)
  if %w[TrueClass FalseClass].include?(value.class.name)
    value ? 1 : 0
  else
    strict ? raise_conversion_type(value) : value
  end
end