Sha256: 1ece9f8270e959a3eac734b09fb5848e37d5006c4415625096e36782418f818b

Contents?: true

Size: 643 Bytes

Versions: 4

Compression:

Stored size: 643 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]).freeze
  # String values considered falsy
  FALSY = Set.new(%w[f F 0 n N 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

4 entries across 4 versions & 1 rubygems

Version Path
honey_format-0.21.0 lib/honey_format/converters/convert_boolean.rb
honey_format-0.20.0 lib/honey_format/converters/convert_boolean.rb
honey_format-0.19.0 lib/honey_format/converters/convert_boolean.rb
honey_format-0.18.0 lib/honey_format/converters/convert_boolean.rb