Module: Ldbws::Utils

Defined in:
lib/ldbws/utils.rb

Overview

:nodoc: all

Class Method Summary collapse

Class Method Details

.deep_hashify_value(value) ⇒ Object

Deep converts a passed object as a hash.

Parameters

value

the value to convert



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ldbws/utils.rb', line 27

def self.deep_hashify_value(value)
  if value.nil?
    nil
  elsif value.is_a?(Array)
    value.map { |v| deep_hashify_value(v) }
  elsif value.respond_to?(:to_h)
    value.to_h
  else
    value
  end
end

.deep_to_soap(xml, hsh) ⇒ Object

Deep-serialises a hash to XML using Nokogiri

Parameters

xml

the instance of Nokogiri to build within

hsh

the hash to serialise



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/ldbws/utils.rb', line 44

def self.deep_to_soap(xml, hsh)
  hsh.each do |key, value|
    key = to_snake_case(key)

    if value.is_a?(Hash)
      xml.send(key) { deep_to_soap(xml, value) }
    elsif value.is_a?(Array)
      xml.send(key) {
        value.each { |v| deep_to_soap(xml, v) }
      }
    else
      xml.send(key, value)
    end
  end
end

.to_snake_case(str, ucfirst = false) ⇒ Object

Converts the passed string from CamelCase to snake_case

Parameters

str

the input string

ucfirst
Boolean

whether or not to call [#ucfirst] as well



17
18
19
20
21
# File 'lib/ldbws/utils.rb', line 17

def self.to_snake_case(str, ucfirst = false)
  output = str.to_s.gsub(/_([a-z])/) { $1.upcase }

  ucfirst ? ucfirst(output) : output
end

.ucfirst(str) ⇒ Object

Upcases the first letter of the passed string.

Parameters

str

the input string



8
9
10
# File 'lib/ldbws/utils.rb', line 8

def self.ucfirst(str)
  str[0].upcase + str[1..]
end