Module: ConsulApplicationSettings::Utils

Defined in:
lib/consul_application_settings/utils.rb

Overview

Utility methods to cast values and work with path

Constant Summary collapse

SEPARATOR =
'/'.freeze
PARSING_CLASSES =
[Integer, Float, ->(value) { JSON.parse(value) }].freeze

Class Method Summary collapse

Class Method Details

.cast_consul_value(value) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/consul_application_settings/utils.rb', line 8

def cast_consul_value(value)
  return nil if value.nil?
  return false if value == 'false'
  return true if value == 'true'
  return value if value.is_a?(Hash)
  return convert_to_hash(value) if value.is_a?(Array)

  cast_complex_value(value)
end

.decompose_path(path) ⇒ Object



24
25
26
27
# File 'lib/consul_application_settings/utils.rb', line 24

def decompose_path(path)
  parts = path.to_s.split(SEPARATOR).compact
  parts.reject(&:empty?)
end

.generate_path(*parts) ⇒ Object



18
19
20
21
22
# File 'lib/consul_application_settings/utils.rb', line 18

def generate_path(*parts)
  strings = parts.map(&:to_s)
  all_parts = strings.map { |s| s.split(SEPARATOR) }.flatten
  all_parts.reject(&:empty?).join('/')
end