Module: Typekit::Helper

Defined in:
lib/typekit/helper.rb

Class Method Summary collapse

Class Method Details

.constantize(object) ⇒ Symbol

Constructs a symbol suitable for looking up modules among the constants defined in a module.

Parameters:

  • object (String, Symbol)

Returns:

  • (Symbol)


38
39
40
# File 'lib/typekit/helper.rb', line 38

def self.constantize(object)
  object.to_s.sub(/^.*::/, '').capitalize.to_sym
end

.extract_array!(array) ⇒ Array

Removes and returns the last element of the given array if that element is an array; otherwise, a new array is returned.

Parameters:

  • array (Array)

Returns:

  • (Array)


74
75
76
# File 'lib/typekit/helper.rb', line 74

def self.extract_array!(array)
  array.last.is_a?(Array) ? array.pop : {}
end

.extract_hash!(array) ⇒ Hash

Removes and returns the last element of the given array if that element is a hash; otherwise, a new hash is returned.

Parameters:

  • array (Array)

Returns:

  • (Hash)


65
66
67
# File 'lib/typekit/helper.rb', line 65

def self.extract_hash!(array)
  array.last.is_a?(Hash) ? array.pop : {}
end

.pluralize(word) ⇒ String

Returns the plural form of the given word.

Parameters:

  • word (String)

Returns:

  • (String)


7
8
9
10
11
12
13
14
15
16
# File 'lib/typekit/helper.rb', line 7

def self.pluralize(word)
  case word
  when /^.*s$/
    word
  when /^(?<root>.*)y$/
    "#{Regexp.last_match(:root)}ies"
  else
    "#{word}s"
  end
end

.singularize(word) ⇒ String

Returns the singular form of the given word.

Parameters:

  • word (String)

Returns:

  • (String)


22
23
24
25
26
27
28
29
30
31
# File 'lib/typekit/helper.rb', line 22

def self.singularize(word)
  case word
  when /^(?<root>.*)ies$/
    "#{Regexp.last_match(:root)}y"
  when /^(?<root>.*)s$/
    Regexp.last_match(:root)
  else
    word
  end
end

.symbolize_keys(hash) ⇒ Hash

Copies the given hash while converting its keys into symbols.

Parameters:

  • hash (Hash)

Returns:

  • (Hash)


56
57
58
# File 'lib/typekit/helper.rb', line 56

def self.symbolize_keys(hash)
  Hash[hash.map { |k, v| [k.to_sym, v] }]
end

.tokenize(object, options = {}) ⇒ Object

Constructs a symbolic representation of the given object.

Parameters:

  • object
  • options (Hash) (defaults to: {})

    a customizable set of options

Options Hash (options):

  • :pluralize (Boolean) — default: true

    indicates if the result should be plural



47
48
49
50
# File 'lib/typekit/helper.rb', line 47

def self.tokenize(object, options = {})
  name = object.to_s.sub(/^.*::/, '').downcase
  (options.fetch(:pluralize, true) ? pluralize(name) : name).to_sym
end