Module: Wayfarer::Config::Strconv

Defined in:
lib/wayfarer/config/strconv.rb

Class Method Summary collapse

Class Method Details

.array(str) ⇒ Object



29
30
31
# File 'lib/wayfarer/config/strconv.rb', line 29

def array(str)
  str.split(",").map(&:strip)
end

.hash(str) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/wayfarer/config/strconv.rb', line 20

def hash(str)
  array(str).reduce({}) do |acc, pair|
    k, v = pair.split(":", 2)
    next acc unless k && v

    acc.merge({ parse(k, Symbol) => primitive(v) })
  end
end

.parse(str, type = nil) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
# File 'lib/wayfarer/config/strconv.rb', line 8

def parse(str, type = nil)
  return primitive(str) unless type

  case type.name
  when "Hash" then hash(str)
  when "Array" then array(str)
  when "Symbol" then str.to_sym
  when "Integer" then Integer(str)
  else str
  end
end

.primitive(str) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# File 'lib/wayfarer/config/strconv.rb', line 33

def primitive(str)
  return true if str == "true"
  return false if str == "false"

  begin
    parse(str, Integer)
  rescue StandardError
    str
  end
end