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)


51
52
53
54
55
56
# File 'lib/ultra_settings/coerce.rb', line 51

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.

Parameters:

  • value (Object)

    The value to check.

Returns:

  • (Boolean)

    true if the value is nil or empty.



98
99
100
101
102
103
104
105
106
# File 'lib/ultra_settings/coerce.rb', line 98

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)


62
63
64
65
66
67
# File 'lib/ultra_settings/coerce.rb', line 62

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

  !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
# 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
  else
    value.to_s
  end
end

.numeric?(value) ⇒ Boolean

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

Parameters:

  • value (Object)

    The value to check.

Returns:

  • (Boolean)

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



92
93
94
# File 'lib/ultra_settings/coerce.rb', line 92

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.

Parameters:

  • value (Object)

    The value to check.

Returns:

  • (Boolean)

    true if the value is not nil and not empty.



110
111
112
# File 'lib/ultra_settings/coerce.rb', line 110

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

.time(value) ⇒ Time

Cast a value to a Time object.

Parameters:

  • value (Object)

Returns:

  • (Time)


73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/ultra_settings/coerce.rb', line 73

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