Module: Yamlr::Reader::Format

Defined in:
lib/yamlr/reader/format.rb

Class Method Summary collapse

Class Method Details

.adjust_options(phs) ⇒ Object

adjusts options



30
31
32
33
34
35
# File 'lib/yamlr/reader/format.rb', line 30

def self.adjust_options(phs)
  if phs[:opt][:auto_sym]
    phs[:opt][:auto_sym_keys] = true
    phs[:opt][:auto_sym_vals] = true
  end
end

.format(phs) ⇒ Object

format datatypes in the parsed hash



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/yamlr/reader/format.rb', line 6

def self.format(phs)
  self.adjust_options(phs)
  self.stripper(phs)
  self.key_to_int(phs)
  self.val_to_int(phs)
  self.key_to_sym(phs)
  self.val_to_sym(phs)
  self.key_to_true(phs)
  self.val_to_true(phs)
  phs
end

.key_to_int(phs) ⇒ Object

key is parsed as string, so try to_i



93
94
95
96
97
98
# File 'lib/yamlr/reader/format.rb', line 93

def self.key_to_int(phs)
  x = []
  x << phs[:opt][:int].is_a?(TrueClass)
  x << phs[:opt][:int_keys].is_a?(TrueClass)
  phs[:key] = self.string_to_int(phs[:key]) if x.any?
end

.key_to_sym(phs) ⇒ Object

symbolize key



67
68
69
70
71
72
73
74
75
76
# File 'lib/yamlr/reader/format.rb', line 67

def self.key_to_sym(phs)
  is_str = !phs[:key].is_a?(Integer)
  x = []
  x << (phs[:opt][:sym_str] && is_str).is_a?(TrueClass)
  x << (phs[:opt][:sym_str_keys] && is_str).is_a?(TrueClass)
  x << phs[:opt][:symbolize].is_a?(TrueClass)
  x << (phs[:ask] && phs[:opt][:auto_sym_keys] && is_str).is_a?(TrueClass)
  x << phs[:opt][:symbolize_keys].is_a?(TrueClass)
  phs[:key] = phs[:key].to_s.to_sym if x.any?
end

.key_to_true(phs) ⇒ Object

true key



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/yamlr/reader/format.rb', line 39

def self.key_to_true(phs)
  x = []
  x << phs[:opt][:auto_true].is_a?(TrueClass)
  x << phs[:opt][:auto_true_keys].is_a?(TrueClass)
  if x.any?
    case
    when (phs[:key].to_s.strip == "true") then phs[:key] = true
    when (phs[:key].to_s.strip == "false") then phs[:key] = false
    end
  end
end

.string_to_int(string) ⇒ Object

returns int if string is convertable



111
112
113
# File 'lib/yamlr/reader/format.rb', line 111

def self.string_to_int(string)
  string.to_s.strip =~ /^\d+$/ ? string.to_s.to_i : string
end

.stripper(phs) ⇒ Object

strips keys and values



20
21
22
23
24
25
26
# File 'lib/yamlr/reader/format.rb', line 20

def self.stripper(phs)
  psp = phs[:opt][:strip]
  psk = phs[:opt][:strip_keys]
  psv = phs[:opt][:strip_vals]
  phs[:key] = phs[:key].to_s.strip if psp or psk
  phs[:val] = phs[:val].to_s.strip if psp or psv
end

.val_to_int(phs) ⇒ Object

val is parsed as string, so try to_i



102
103
104
105
106
107
# File 'lib/yamlr/reader/format.rb', line 102

def self.val_to_int(phs)
  x = []
  x << phs[:opt][:int].is_a?(TrueClass)
  x << phs[:opt][:int_vals].is_a?(TrueClass)
  phs[:val] = self.string_to_int(phs[:val]) if x.any?
end

.val_to_sym(phs) ⇒ Object

symbolize val



80
81
82
83
84
85
86
87
88
89
# File 'lib/yamlr/reader/format.rb', line 80

def self.val_to_sym(phs)
  is_str = !phs[:val].is_a?(Integer)
  x = []
  x << (phs[:opt][:sym_str] && is_str).is_a?(TrueClass)
  x << (phs[:opt][:sym_str_vals] && is_str).is_a?(TrueClass)
  x << phs[:opt][:symbolize].is_a?(TrueClass)
  x << (phs[:asv] && phs[:opt][:auto_sym_vals] && is_str).is_a?(TrueClass)
  x << phs[:opt][:symbolize_vals].is_a?(TrueClass)
  phs[:val] = phs[:val].to_s.to_sym if (x.any? && !phs[:val].to_s.strip.empty?)
end

.val_to_true(phs) ⇒ Object

true val



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/yamlr/reader/format.rb', line 53

def self.val_to_true(phs)
  x = []
  x << phs[:opt][:auto_true].is_a?(TrueClass)
  x << phs[:opt][:auto_true_vals].is_a?(TrueClass)
  if x.any?
    case
    when (phs[:val].to_s.strip == "true") then phs[:val] = true
    when (phs[:val].to_s.strip == "false") then phs[:val] = false
    end
  end
end