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.
-
#uuid ⇒ Object
Generates a UUID.
Instance Method Details
#camel_case(string) ⇒ String
Camelcases a given string
Usage: in_string = “an example” out_string = camel_case(in_string) => AnExample
161 162 163 164 |
# File 'lib/app_archetype/template/helpers.rb', line 161 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
141 142 143 144 145 146 147 148 149 150 |
# File 'lib/app_archetype/template/helpers.rb', line 141 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
96 97 98 |
# File 'lib/app_archetype/template/helpers.rb', line 96 def downcase(string) string.downcase end |
#join(delim, *strings) ⇒ String
Joins a string with specified delimiter
107 108 109 |
# File 'lib/app_archetype/template/helpers.rb', line 107 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”
190 191 192 193 194 195 196 197 198 199 200 |
# File 'lib/app_archetype/template/helpers.rb', line 190 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
51 52 53 54 55 |
# File 'lib/app_archetype/template/helpers.rb', line 51 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.
68 69 70 71 72 73 74 75 76 77 |
# File 'lib/app_archetype/template/helpers.rb', line 68 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”
212 213 214 215 216 217 218 219 220 |
# File 'lib/app_archetype/template/helpers.rb', line 212 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.
123 124 125 126 127 128 129 130 131 132 |
# File 'lib/app_archetype/template/helpers.rb', line 123 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
175 176 177 178 |
# File 'lib/app_archetype/template/helpers.rb', line 175 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
86 87 88 |
# File 'lib/app_archetype/template/helpers.rb', line 86 def upcase(string) string.upcase end |
#uuid ⇒ Object
Generates a UUID
43 44 45 |
# File 'lib/app_archetype/template/helpers.rb', line 43 def uuid SecureRandom.hex end |