Sha256: 6c9e73f42aa40888f8292281ffe0588a58d5bc12c14b5456ea836fc05e5ad3c0

Contents?: true

Size: 723 Bytes

Versions: 2

Compression:

Stored size: 723 Bytes

Contents

# frozen_string_literal: true

require_relative "base"

class StoreSchema::Converter::Float < StoreSchema::Converter::Base

  # @return [Regexp] an (int | float) value format.
  #
  INT_FLOAT_FORMAT = /^\d+|\d+\.\d+$/

  # Converts the {#value} to a database-storable value.
  #
  # @return [String, false] false if {#value} is an invalid date-type.
  #
  def to_db
    case value
    when ::Integer
      value.to_f.to_s
    when ::Float
      value.to_s
    when ::String
      if value.match(INT_FLOAT_FORMAT)
        value.to_f.to_s
      else
        false
      end
    else
      false
    end
  end

  # Converts the {#value} to a Ruby-type value.
  #
  # @return [Float]
  #
  def from_db
    value.to_f
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
store_schema-2.0.0 lib/store_schema/converter/float.rb
store_schema-1.1.1 lib/store_schema/converter/float.rb