Sha256: a58eb35e04eda1341108b2b07c5abebb678673cee944d588cf40974be6a75e58

Contents?: true

Size: 662 Bytes

Versions: 7

Compression:

Stored size: 662 Bytes

Contents

# frozen_string_literal: true

require 'set'

module HoneyFormat
  # String values considered truthy
  TRUTHY = Set.new(%w[t T 1 y Y true TRUE] + [true]).freeze
  # String values considered falsy
  FALSY = Set.new(%w[f F 0 n N false FALSE] + [false]).freeze

  # Tries to convert value boolean to, returns nil if it can't convert
  ConvertBoolean = proc do |v|
    if TRUTHY.include?(v)
      true
    elsif FALSY.include?(v)
      false
    end
  end

  # Convert to boolean or raise error
  StrictConvertBoolean = proc do |v|
    ConvertBoolean.call(v).tap do |value|
      raise(ArgumentError, "can't convert #{v} to boolean") if value.nil?
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
honey_format-0.27.0 lib/honey_format/converters/convert_boolean.rb
honey_format-0.26.0 lib/honey_format/converters/convert_boolean.rb
honey_format-0.25.0 lib/honey_format/converters/convert_boolean.rb
honey_format-0.24.0 lib/honey_format/converters/convert_boolean.rb
honey_format-0.23.0 lib/honey_format/converters/convert_boolean.rb
honey_format-0.22.0 lib/honey_format/converters/convert_boolean.rb
honey_format-0.21.1 lib/honey_format/converters/convert_boolean.rb