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

#convert, #source, #target

Instance Method Summary collapse

Methods inherited from Converter

create, #fail_conversion_type, #initialize

Constructor Details

This class inherits a constructor from Necromancer::Converter

Instance Method Details

#call(value, options = {}) ⇒ Object

Convert boolean to integer

Examples:

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


74
75
76
77
78
79
80
81
# File 'lib/necromancer/converters/boolean.rb', line 74

def call(value, options = {})
  strict = options.fetch(:strict, config.strict)
  if ['TrueClass', 'FalseClass'].include?(value.class.name)
    value ? 1 : 0
  else
    strict ? fail_conversion_type(value) : value
  end
end