Class: Humanizer::Sanitize

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.params(params, options) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/humanizer.rb', line 49

def self.params(params, options)
  params = Hash(params)
  options = Hash(options)

  sanitizer = Sanitize.new

  options.each do |param, type|
    params[param] = sanitizer.from params[param], to: type
  end

  params
end

Instance Method Details

#from(value, to: nil) ⇒ Object



45
46
47
# File 'lib/humanizer.rb', line 45

def from(value, to: nil)
  self.send "to_#{to}", value
end

#to_array(value) ⇒ Object



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

def to_array(value)
  value = String(value)

  return nil if value.empty?

  array = String(value).split Humanizer.config.array_delimeter
  array.map &:strip
end

#to_hash(value) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/humanizer.rb', line 29

def to_hash(value)
  value = String(value)
  hash = {}

  return nil if value.empty?
  
  value.split(Humanizer.config.hash_delimeter).each do |key_val|
    k, v = key_val.split(Humanizer.config.key_val_delimeter)
    k = String(k)
    v = String(v)
    hash[k.strip] = v.strip
  end

  hash
end