Sha256: b88a7b23e7d1e922f86ba94abe1b6a3a8b32e859e549c232afe6c71b4a612870

Contents?: true

Size: 1.11 KB

Versions: 3

Compression:

Stored size: 1.11 KB

Contents

require_relative "base"

class StoreSchema::Converter::Boolean < StoreSchema::Converter::Base

  # @return [String] the database representation of a true value.
  #
  DB_TRUE_VALUE = "t"

  # @return [String] the database representation of a false value.
  #
  DB_FALSE_VALUE = "f"

  # @return [Array] all the values that are considered to be truthy.
  #
  TRUE_VALUES = [true, 1, "1", "t", "T", "true", "TRUE", "on", "ON"]

  # @return [Array] all the values that are considered to be falsy.
  #
  FALSE_VALUES = [false, 0, "0", "f", "F", "false", "FALSE", "off", "OFF"]

  # @return [Object]
  #
  attr_reader :value

  # @param [Object] value
  #
  def initialize(value)
    @value = value
  end

  # Converts the {#value} to a database-storable value.
  #
  # @return [String, false] false if {#value} is an invalid date-type.
  #
  def to_db
    if TRUE_VALUES.include?(value)
      DB_TRUE_VALUE
    elsif FALSE_VALUES.include?(value)
      DB_FALSE_VALUE
    else
      false
    end
  end

  # Converts the {#value} to a Ruby-type value.
  #
  # @return [true, false]
  #
  def from_db
    value == DB_TRUE_VALUE
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
store_schema-1.1.0 lib/store_schema/converter/boolean.rb
store_schema-1.0.1 lib/store_schema/converter/boolean.rb
store_schema-1.0.0 lib/store_schema/converter/boolean.rb