Class: UltraSettings::Coerce

Inherits:
Object
  • Object
show all
Defined in:
lib/ultra_settings/coerce.rb

Overview

Utility functions for coercing values to other data types.

Constant Summary collapse

FALSE_VALUES =

rubocop:disable Lint/BooleanSymbol

Set.new([
  false, 0,
  "0", :"0",
  "f", :f,
  "false", :false,
  "off", :off
]).freeze
NUMERIC_REGEX =

rubocop:enable Lint/BooleanSymbol

/\A-?\d+(?:\.\d+)?\z/

Class Method Summary collapse

Class Method Details

.array(value) ⇒ Array

Cast value of array

Parameters:

  • value (Object)

Returns:

  • (Array)


57
58
59
60
61
62
# File 'lib/ultra_settings/coerce.rb', line 57

def array(value)
  return [] if blank?(value)
  return value.collect(&:to_s) if value.is_a?(Array)

  parse_csv_line(value.to_s)
end

.blank?(value) ⇒ Boolean

Returns true if the value is nil or empty.

Returns:

  • (Boolean)

    true if the value is nil or empty.



102
103
104
105
106
107
108
109
110
# File 'lib/ultra_settings/coerce.rb', line 102

def blank?(value)
  return true if value.nil?

  if value.respond_to?(:empty?)
    value.empty?
  else
    value.to_s.empty?
  end
end

.boolean(value) ⇒ Boolean

Cast variations of booleans (i.e. “true”, “false”, 1, 0, etc.) to actual boolean objects.

Parameters:

  • value (Object)

Returns:

  • (Boolean)


68
69
70
71
72
73
# File 'lib/ultra_settings/coerce.rb', line 68

def boolean(value)
  return nil if blank?(value)
  return false if value == false

  !FALSE_VALUES.include?(value.to_s.downcase)
end

.coerce_value(value, type) ⇒ Object

Cast a value to a specific type.

Parameters:

  • value (Object)
  • type (Symbol)

Returns:

  • (Object)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ultra_settings/coerce.rb', line 26

def coerce_value(value, type)
  return nil if value.nil? || value == ""

  case type
  when :integer
    value.is_a?(Integer) ? value : value.to_s&.to_i
  when :float
    value.is_a?(Float) ? value : value.to_s&.to_f
  when :boolean
    boolean(value)
  when :datetime
    time(value)
  when :array
    array(value).map(&:to_s)
  when :symbol
    value.to_s.to_sym
  when :rollout
    if numeric?(value)
      value.to_f
    else
      boolean(value)
    end
  else
    value.to_s
  end
end

.numeric?(value) ⇒ Boolean

Returns true if the value is a numeric type or a string representing a number.

Returns:

  • (Boolean)

    true if the value is a numeric type or a string representing a number.



97
98
99
# File 'lib/ultra_settings/coerce.rb', line 97

def numeric?(value)
  value.is_a?(Numeric) || (value.is_a?(String) && value.to_s.match?(NUMERIC_REGEX))
end

.present?(value) ⇒ Boolean

Returns true if the value is not nil and not empty.

Returns:

  • (Boolean)

    true if the value is not nil and not empty.



113
114
115
# File 'lib/ultra_settings/coerce.rb', line 113

def present?(value)
  !blank?(value)
end

.time(value) ⇒ Time

Cast a value to a Time object.

Parameters:

  • value (Object)

Returns:

  • (Time)


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/ultra_settings/coerce.rb', line 79

def time(value)
  value = nil if value.nil? || value.to_s.empty?
  return nil if value.nil?

  time = if numeric?(value)
    Time.at(value.to_f)
  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