Class: String
- Inherits:
-
Object
- Object
- String
- 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.
['sequence'].freeze
- KEYWORDS =
Returns 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
-
#handle_initial_characters ⇒ String
If all characters were stripped out, put a placeholder ‘X’.
-
#humanize_numbered_string ⇒ Object
Convert string starting with number to a human string.
-
#keyword? ⇒ Boolean
Whether string is a Ruby keyword.
-
#remove_macrons ⇒ String
Return string without macrons, used for filenames and ruby methods.
-
#to_class_name ⇒ String
Convert String to form a class could use.
-
#to_key_name ⇒ String
Remove many complex characters to make a key for a Ruby Hash that is usable in a method/key.
-
#to_key_name_for(hash) ⇒ String
Return a key name that is unique to the hash.
-
#to_ruby_friendly ⇒ String
Convert string to something that would be friendly to use as in Ruby.
-
#to_soql_array ⇒ String
Convert Ruby String representation of Array to SoqlArray.
-
#to_zulu_date_string ⇒ String
Zulu time stamp for string.
-
#type_of_time? ⇒ Boolean
Whether string can be parsed into a time.
-
#unused_ruby_name ⇒ String
A ruby friendly method name that is not taken by keywords like ‘class’, ‘alias’.
Instance Method Details
#handle_initial_characters ⇒ String
Returns If all characters were stripped out, put a placeholder ‘X’. If starts with underscore remove it.
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_string ⇒ Object
Convert string starting with number to a human string
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.
122 123 124 |
# File 'lib/leap_salesforce/ext/string.rb', line 122 def keyword? KEYWORDS.include?(self) || FACTORY_WORDS.include?(self) || SOQLDATA_METHODS.include?(self) end |
#remove_macrons ⇒ String
Return string without macrons, used for filenames and ruby methods
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_name ⇒ String
Returns Convert String to form a class could use.
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_name ⇒ String
Returns Remove many complex characters to make a key for a Ruby Hash that is usable in a method/key.
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
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_friendly ⇒ String
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.
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_array ⇒ String
Convert Ruby String representation of Array to SoqlArray
111 112 113 114 115 116 117 118 119 |
# File 'lib/leap_salesforce/ext/string.rb', line 111 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_string ⇒ String
Returns Zulu time stamp for string.
96 97 98 |
# File 'lib/leap_salesforce/ext/string.rb', line 96 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.
101 102 103 104 105 106 107 |
# File 'lib/leap_salesforce/ext/string.rb', line 101 def type_of_time? Date.strptime(self, '%Y-%m-%d') rescue ArgumentError false else true end |
#unused_ruby_name ⇒ String
A ruby friendly method name that is not taken by keywords like ‘class’, ‘alias’
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 |