Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/leap_salesforce/ext/string.rb

Overview

Override string object to provide convenience methods for Strings

Constant Summary collapse

FACTORY_WORDS =

Returns List of keywords reserved for FactoryBot.

Returns:

  • (Array)

    List of keywords reserved for FactoryBot

['sequence'].freeze
KEYWORDS =

Returns List of ruby keywords. Some words are only key to FactoryBot like ‘sequence’.

Returns:

  • (Array)

    List of ruby keywords. Some words are only key to FactoryBot like ‘sequence’

%w[BEGIN END __ENCODING__ __END__ __FILE__ __LINE__ alias and begin break case class def defined?
do else elsif end ensure false for if in module next nil not or redo rescue retry return
self super then true undef unless until when while yield private].freeze
SOQLDATA_METHODS =
Exchange.new.public_methods.collect(&:to_s)

Instance Method Summary collapse

Instance Method Details

#handle_initial_charactersString

Returns If all characters were stripped out, put a placeholder ‘X’. If starts with underscore remove it.

Examples:

Remove stripped characters

''.handle_initial_characters # => 'X'

Remove leading underscores

'_leading'.handle_initial_characters # => 'leading'

Returns:

  • (String)

    If all characters were stripped out, put a placeholder ‘X’. If starts with underscore remove it

[View source]

43
44
45
46
47
48
49
50
# File 'lib/leap_salesforce/ext/string.rb', line 43

def handle_initial_characters
  if size.zero?
    +'X'
  else
    self[0] = '' if self[0] == '_'
    self
  end
end

#humanize_numbered_stringObject

Convert string starting with number to a human string

[View source]

78
79
80
81
# File 'lib/leap_salesforce/ext/string.rb', line 78

def humanize_numbered_string
  self[0] = ('0'..'9').cover?(self[0]) ? self[0].to_i.humanize : self[0]
  self
end

#keyword?Boolean

Returns Whether string is a Ruby keyword.

Returns:

  • (Boolean)

    Whether string is a Ruby keyword

[View source]

128
129
130
# File 'lib/leap_salesforce/ext/string.rb', line 128

def keyword?
  KEYWORDS.include?(self) || FACTORY_WORDS.include?(self) || SOQLDATA_METHODS.include?(self)
end

#remove_macronsString

Return string without macrons, used for filenames and ruby methods

Returns:

  • (String)

    Return string without macrons, used for filenames and ruby methods

[View source]

84
85
86
# File 'lib/leap_salesforce/ext/string.rb', line 84

def remove_macrons
  tr('ā', 'a').tr('ē', 'e').tr('ō', 'o').tr('ī', 'i').tr('ū', 'u')
end

#to_class_nameString

Returns Convert String to form a class could use.

Returns:

  • (String)

    Convert String to form a class could use

[View source]

53
54
55
# File 'lib/leap_salesforce/ext/string.rb', line 53

def to_class_name
  camelize.to_ruby_friendly.tr('_', '').camelize
end

#to_key_nameString

Returns Remove many complex characters to make a key for a Ruby Hash that is usable in a method/key.

Returns:

  • (String)

    Remove many complex characters to make a key for a Ruby Hash that is usable in a method/key

[View source]

58
59
60
# File 'lib/leap_salesforce/ext/string.rb', line 58

def to_key_name
  to_ruby_friendly.snakecase.gsub(/__+/, '_')
end

#to_key_name_for(hash) ⇒ String

Return a key name that is unique to the hash

Returns:

  • (String)

    Return a key name that is unique to the hash

[View source]

63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/leap_salesforce/ext/string.rb', line 63

def to_key_name_for(hash)
  key_name = to_key_name
  index = 0
  while hash[key_name]
    if ('1'..'9').cover? key_name[-1]
      key_name[-1] = (key_name[-1].to_i + 1).to_s
    else
      key_name += '_1'
    end
    break if (index += 1) >= 9
  end
  key_name
end

#to_ruby_friendlyString

Note:

This removes ‘?’ which is allowed in a method but is not desired for automatic creation of their names in the way that is being used here

Returns Convert string to something that would be friendly to use as in Ruby.

Returns:

  • (String)

    Convert string to something that would be friendly to use as in Ruby

[View source]

28
29
30
31
32
33
34
35
# File 'lib/leap_salesforce/ext/string.rb', line 28

def to_ruby_friendly
  tr('&|=', '_').gsub('<', '_lt_').gsub('>', '_gt_')
                .remove_macrons
                .gsub(/\s+/, '_')
                .gsub(/\W/, '') # Remove any other special characters
                .handle_initial_characters # Previous step could have removed all characters
                .humanize_numbered_string
end

#to_soql_arrayString

Convert Ruby String representation of Array to SoqlArray

Returns:

  • (String)

    String representation for use in Soql IN query

[View source]

117
118
119
120
121
122
123
124
125
# File 'lib/leap_salesforce/ext/string.rb', line 117

def to_soql_array
  arr = JSON.parse(self)
  unless arr.is_a? Array
    raise ArgumentError, "Cannot convert #{self} into Array" \
    "Called at #{caller_locations[0]}"
  end
  joined = arr.join("', '")
  "('#{joined}')"
end

#to_zulu_date_stringString

Returns Zulu time stamp for string.

Returns:

  • (String)

    Zulu time stamp for string

[View source]

102
103
104
# File 'lib/leap_salesforce/ext/string.rb', line 102

def to_zulu_date_string
  Time.parse(self).strftime('%Y-%m-%dT%H:%M:%S.000Z')
end

#type_of_time?Boolean

Returns Whether string can be parsed into a time.

Returns:

  • (Boolean)

    Whether string can be parsed into a time

[View source]

107
108
109
110
111
112
113
# File 'lib/leap_salesforce/ext/string.rb', line 107

def type_of_time?
  Date.strptime(self, '%Y-%m-%d')
rescue ArgumentError
  false
else
  true
end

#unused_camel_name(type = :upper) ⇒ String

Returns Camel case string that is not taken by keywords like ‘class’, ‘alias’.

Parameters:

  • type (Symbol) (defaults to: :upper)

    :upper or :lower, default :upper

Returns:

  • (String)

    Camel case string that is not taken by keywords like ‘class’, ‘alias’

[View source]

97
98
99
# File 'lib/leap_salesforce/ext/string.rb', line 97

def unused_camel_name(type = :upper)
  unused_ruby_name.camelize(type)
end

#unused_ruby_nameString

A ruby friendly method name that is not taken by keywords like ‘class’, ‘alias’

Returns:

  • (String)

    Ruby friendly name that can be used as method name

[View source]

90
91
92
93
# File 'lib/leap_salesforce/ext/string.rb', line 90

def unused_ruby_name
  name = to_key_name
  name.keyword? ? "my_#{name}" : name
end