Sha256: e8c17d2955f5bac07e42e6d3fdfb83b11ac2aeaee1263775ee60e7c8121675fe
Contents?: true
Size: 1.62 KB
Versions: 2
Compression:
Stored size: 1.62 KB
Contents
# frozen_string_literal: true require "set" module SuperSettings # Utility functions for coercing values to other data types. class Coerce # rubocop:disable Lint/BooleanSymbol FALSE_VALUES = Set.new([ "0", :"0", "f", :f, "false", :false, "off", :off ]).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.to_s.downcase) 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-2.0.3 | lib/super_settings/coerce.rb |
super_settings-2.0.2 | lib/super_settings/coerce.rb |