Module: IronBank::Utils

Defined in:
lib/iron_bank/utils.rb

Overview

A few helper methods for interacting with Zuora

Class Method Summary collapse

Class Method Details

.camelize(term, type: :upper) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/iron_bank/utils.rb', line 20

def camelize(term, type: :upper)
  # Preserve custom field term postfix, in the '__c' or the '__NS' format
  custom_field   = (term.to_s =~ /__c$/i)
  netsuite_field = (term.to_s =~ /__NS$/i)
  lower_camelize = type == :lower

  output = term.to_s.dup.tap do |copy|
    copy.gsub!(/^_*/, "")
    copy.gsub!(/__(NS|c)$/i, "") if custom_field || netsuite_field

    lower_camelize ? lower_camelize(copy) : upper_camelize(copy)
  end

  if custom_field
    "#{output}__c"
  elsif netsuite_field
    "#{output}__NS"
  else
    output
  end
end

.kebab(term) ⇒ Object



42
43
44
# File 'lib/iron_bank/utils.rb', line 42

def kebab(term)
  underscore(term).tr!("_", "-")
end

.lower_camelize(term) ⇒ Object



46
47
48
49
50
51
52
53
# File 'lib/iron_bank/utils.rb', line 46

def lower_camelize(term)
  term.gsub!(/(^|_)(.)/) do
    match = Regexp.last_match
    char  = match[2]

    match.begin(2).zero? ? char : char.upcase
  end
end

.underscore(camel_cased_word) ⇒ Object

Inspired from ActiveSupport



10
11
12
13
14
15
16
17
18
# File 'lib/iron_bank/utils.rb', line 10

def underscore(camel_cased_word)
  return camel_cased_word unless /[A-Z-]|::/.match?(camel_cased_word)

  word = camel_cased_word.to_s.gsub(/::/, "/")
  word.gsub!(/([A-Z\d]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!("-", "_")
  word.downcase
end

.upper_camelize(term) ⇒ Object



55
56
57
# File 'lib/iron_bank/utils.rb', line 55

def upper_camelize(term)
  term.gsub!(/(^|_)(.)/) { Regexp.last_match(2).upcase }
end