Sha256: 020899a442afd9340f4261e9ff54096301d27a9b69b14d35d9986811799b55ec

Contents?: true

Size: 1.67 KB

Versions: 2

Compression:

Stored size: 1.67 KB

Contents

# frozen_string_literal: true

module SuperSettings
  # Utility functions for coercing values to other data types.
  class Coerce
    # rubocop:disable Lint/BooleanSymbol
    FALSE_VALUES = [
      false, 0,
      "0", :"0",
      "f", :f,
      "F", :F,
      "false", :false,
      "FALSE", :FALSE,
      "off", :off,
      "OFF", :OFF
    ].to_set.freeze
    # rubocop:enable Lint/BooleanSymbol

    class << self
      # Cast variations of booleans (i.e. "true", "false", 1, 0, etc.) to actual boolean objects.
      #
      # @param value [Object]
      # @return [Boolean]
      def boolean(value)
        if value == false
          false
        elsif blank?(value)
          nil
        else
          !FALSE_VALUES.include?(value)
        end
      end

      # Cast a value to a Time object.
      #
      # @param value [Object]
      # @return [Time]
      def time(value)
        value = nil if value.nil? || value.to_s.empty?
        return nil if value.nil?
        time = if value.is_a?(Numeric)
          Time.at(value)
        elsif value.respond_to?(:to_time)
          value.to_time
        else
          Time.parse(value.to_s)
        end
        if time.respond_to?(:in_time_zone) && Time.respond_to?(:zone)
          time = time.in_time_zone(Time.zone)
        end
        time
      end

      # @return [Boolean] true if the value is nil or empty.
      def blank?(value)
        return true if value.nil?
        if value.respond_to?(:empty?)
          value.empty?
        else
          value.to_s.empty?
        end
      end

      # @return [Boolean] true if the value is not nil and not empty.
      def present?(value)
        !blank?(value)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
super_settings-0.0.1.rc2 lib/super_settings/coerce.rb
super_settings-0.0.1.rc1 lib/super_settings/coerce.rb