Module: MultiSafePay::Util

Defined in:
lib/multisafepay/util.rb

Class Method Summary collapse

Class Method Details

.camelize(term) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/multisafepay/util.rb', line 37

def camelize(term)
  string = term.to_s
  string = string.sub(/^(?:(?=\b|[A-Z_])|\w)/, &:downcase)
  string.gsub!(/(?:_|(\/))([a-z\d]*)/i) { "#{Regexp.last_match(1)}#{Regexp.last_match(2).capitalize}" }
  string.gsub!('/'.freeze, '::'.freeze)
  string
end

.camelize_keys(hash) ⇒ Object



17
18
19
20
21
# File 'lib/multisafepay/util.rb', line 17

def camelize_keys(hash)
  hash.each_with_object({}) do |(key, value), camelized|
    camelized[camelize(key)] = value
  end
end

.nested_openstruct(obj) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/multisafepay/util.rb', line 45

def nested_openstruct(obj)
  if obj.is_a?(Hash)
    obj.each_with_object(OpenStruct.new) do |(key, value), openstructed|
      openstructed[key] = nested_openstruct(value)
    end
  elsif obj.is_a?(Array)
    obj.map { |v| nested_openstruct(v) }
  else
    obj
  end
end

.nested_underscore_keys(obj) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/multisafepay/util.rb', line 5

def nested_underscore_keys(obj)
  if obj.is_a?(Hash)
    obj.each_with_object({}) do |(key, value), underscored|
      underscored[underscore(key)] = nested_underscore_keys(value)
    end
  elsif obj.is_a?(Array)
    obj.map { |v| nested_underscore_keys(v) }
  else
    obj
  end
end

.pluralize(string) ⇒ Object

Dirty pluralize function, but currently holds for all required plurals Not worth to include another library like ActiveSupport



33
34
35
# File 'lib/multisafepay/util.rb', line 33

def pluralize(string)
  "#{string}s"
end

.underscore(string) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/multisafepay/util.rb', line 23

def underscore(string)
  string.to_s.gsub(/::/, '/')
        .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
        .gsub(/([a-z\d])([A-Z])/, '\1_\2')
        .tr('-', '_')
        .downcase.to_s
end