Module: AppArchetype::Template::Helpers

Defined in:
lib/app_archetype/template/helpers.rb

Overview

Template rendering helpers

Instance Method Summary collapse

Instance Method Details

#camel_case(string) ⇒ String

Camelcases a given string

Usage: in_string = “an example” out_string = camel_case(in_string) => AnExample

Parameters:

Returns:



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

Examples:

str = 'AGreatExample'
puts = dash_case(str) # => outputs 'a-great-example'


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

#dotObject

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

Parameters:

Returns:



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

Parameters:

  • delim (String)
  • strings (Array)

Returns:



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”

Parameters:

Returns:



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

Parameters:

  • length (String) (defaults to: '256')


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.

Parameters:

Returns:



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”

Parameters:

Returns:



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.

Examples:

str = 'AGreatExample'
puts snake_case(str) # => outputs 'a_great_example'

Parameters:

Returns:



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

Parameters:

Returns:



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_yearString

Returns this year as YYYY

Returns:



18
19
20
# File 'lib/app_archetype/template/helpers.rb', line 18

def this_year
  Time.now.strftime('%Y')
end

#timestamp_nowString

Returns timestamp at current time

Returns:



27
28
29
# File 'lib/app_archetype/template/helpers.rb', line 27

def timestamp_now
  Time.now.strftime('%Y%m%d%H%M%S%L')
end

#timestamp_utc_nowString

Returns timestamp at utc current time

Returns:



36
37
38
# File 'lib/app_archetype/template/helpers.rb', line 36

def timestamp_utc_now
  Time.now.utc.strftime('%Y%m%d%H%M%S%L')
end

#upcase(string) ⇒ String

Converts a string to upper case

Parameters:

Returns:



86
87
88
# File 'lib/app_archetype/template/helpers.rb', line 86

def upcase(string)
  string.upcase
end

#uuidObject

Generates a UUID



43
44
45
# File 'lib/app_archetype/template/helpers.rb', line 43

def uuid
  SecureRandom.hex
end