Module: AppArchetype::Template::Helpers
- Defined in:
- lib/app_archetype/template/helpers.rb
Overview
Template rendering helpers
Instance Method Summary collapse
-
#camel_case(string) ⇒ String
Camelcases a given string.
-
#dash_case(string) ⇒ Object
Downcase and converts a string into dashcase string.
-
#dot ⇒ Object
dot provides a convenient way for a noop render at the beginning of dotfiles.
-
#downcase(string) ⇒ String
Converts a string to lower case.
-
#join(delim, *strings) ⇒ String
Joins a string with specified delimiter.
-
#pluralize(string) ⇒ String
Attempts to pluralize a word.
-
#random_string(length = '256') ⇒ Object
Generates a random string at specified length.
-
#randomize(string, size = '5') ⇒ String
Randomizes a given string by addding a slice of a hex to the end of it at the specified size.
-
#singularize(string) ⇒ String
Singularizes plural words.
-
#snake_case(string) ⇒ String
Changes a string into snake case.
-
#snake_to_camel(string) ⇒ String
Converts snake case string to camelcase.
-
#this_year ⇒ String
Returns this year as YYYY.
-
#timestamp_now ⇒ String
Returns timestamp at current time.
-
#timestamp_utc_now ⇒ String
Returns timestamp at utc current time.
-
#upcase(string) ⇒ String
Converts a string to upper case.
Instance Method Details
#camel_case(string) ⇒ String
Camelcases a given string
Usage: in_string = “an example” out_string = camel_case(in_string) => AnExample
154 155 156 157 |
# File 'lib/app_archetype/template/helpers.rb', line 154 def camel_case(string) str = snake_case(string) snake_to_camel(str) end |
#dash_case(string) ⇒ Object
Downcase and converts a string into dashcase string
134 135 136 137 138 139 140 141 142 143 |
# File 'lib/app_archetype/template/helpers.rb', line 134 def dash_case(string) return string.downcase if string =~ /\A[A-Z]+\z/ string .gsub(/([A-Z]+)([A-Z][a-z])/, '\1-\2') .gsub(/([a-z])([A-Z])/, '\1-\2') .tr(' ', '-') .tr('_', '-') .downcase end |
#dot ⇒ Object
dot provides a convenient way for a noop render at the beginning of dotfiles
9 10 11 |
# File 'lib/app_archetype/template/helpers.rb', line 9 def dot '' end |
#downcase(string) ⇒ String
Converts a string to lower case
89 90 91 |
# File 'lib/app_archetype/template/helpers.rb', line 89 def downcase(string) string.downcase end |
#join(delim, *strings) ⇒ String
Joins a string with specified delimiter
100 101 102 |
# File 'lib/app_archetype/template/helpers.rb', line 100 def join(delim, *strings) strings.join(delim) end |
#pluralize(string) ⇒ String
Attempts to pluralize a word
Usage: in_string = “Thing” out_string = pluralize(in_string) => “Things”
183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/app_archetype/template/helpers.rb', line 183 def pluralize(string) str = string.to_s if str.match(/([^aeiouy]|qu)y$/i) str = str.gsub(/y\Z/, 'ies') else str << 's' end str end |
#random_string(length = '256') ⇒ Object
Generates a random string at specified length
44 45 46 47 48 |
# File 'lib/app_archetype/template/helpers.rb', line 44 def random_string(length = '256') length = length.to_i key_set = ('a'..'z').to_a + ('A'..'Z').to_a + (0..9).to_a (0...length).map { key_set[Random.rand(0..key_set.length)] }.join end |
#randomize(string, size = '5') ⇒ String
Randomizes a given string by addding a slice of a hex to the end of it at the specified size.
The template will pass through a string as arguments for this function, thus it must accept a string as an argument.
61 62 63 64 65 66 67 68 69 70 |
# File 'lib/app_archetype/template/helpers.rb', line 61 def randomize(string, size = '5') size = size.to_i raise 'size must be an integer' unless size.is_a?(Integer) && size != 0 raise 'randomize supports up to 32 characters' if size > 32 hex = SecureRandom.hex suffix = hex[hex.length - size..hex.length] "#{string}_#{suffix}" end |
#singularize(string) ⇒ String
Singularizes plural words
Usage: in_string = “Things” out_string = singularize(in_string) => “Thing”
205 206 207 208 209 210 211 212 213 |
# File 'lib/app_archetype/template/helpers.rb', line 205 def singularize(string) str = string.to_s if str.end_with?('ies') str.gsub(/ies\Z/, 'y') else str.gsub(/s\Z/, '') end end |
#snake_case(string) ⇒ String
Changes a string into snake case. Useful for converting class names to function or file names.
116 117 118 119 120 121 122 123 124 125 |
# File 'lib/app_archetype/template/helpers.rb', line 116 def snake_case(string) return string.downcase if string =~ /\A[A-Z]+\z/ string .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2') .gsub(/([a-z])([A-Z])/, '\1_\2') .gsub(/\s/, '_') .tr('-', '_') .downcase end |
#snake_to_camel(string) ⇒ String
Converts snake case string to camelcase
Usage: in_string = “an_example” out_string = snake_to_camel(in_string) => AnExample
168 169 170 171 |
# File 'lib/app_archetype/template/helpers.rb', line 168 def snake_to_camel(string) str = snake_case(string) str.to_s.split('_').map(&:capitalize).join('') end |
#this_year ⇒ String
Returns this year as YYYY
18 19 20 |
# File 'lib/app_archetype/template/helpers.rb', line 18 def this_year Time.now.strftime('%Y') end |
#timestamp_now ⇒ String
Returns timestamp at current time
27 28 29 |
# File 'lib/app_archetype/template/helpers.rb', line 27 def Time.now.strftime('%Y%m%d%H%M%S%L') end |
#timestamp_utc_now ⇒ String
Returns timestamp at utc current time
36 37 38 |
# File 'lib/app_archetype/template/helpers.rb', line 36 def Time.now.utc.strftime('%Y%m%d%H%M%S%L') end |
#upcase(string) ⇒ String
Converts a string to upper case
79 80 81 |
# File 'lib/app_archetype/template/helpers.rb', line 79 def upcase(string) string.upcase end |