Module: Wallaby::Inflector

Extended by:
Inflector
Included in:
Inflector
Defined in:
lib/utils/wallaby/inflector.rb

Overview

Convert strings

Instance Method Summary collapse

Instance Method Details

#to_authorizer_name(script_name, resources_name) ⇒ String

Returns authorizer name.

Parameters:

  • script_name (String)
  • resources_name (Class, String)

Returns:

  • (String)

    authorizer name



44
45
46
# File 'lib/utils/wallaby/inflector.rb', line 44

def to_authorizer_name(script_name, resources_name)
  to_class_name(to_script(script_name, to_resource_name(resources_name), AUTHORIZER))
end

#to_class_name(name) ⇒ String?

Parameters:

  • name (String)

Returns:

  • (String)

    class name

  • (nil)

    if name is blank



11
12
13
14
15
16
# File 'lib/utils/wallaby/inflector.rb', line 11

def to_class_name(name)
  return EMPTY_STRING if name.blank?

  name = name.to_s unless name.is_a?(String)
  name.gsub(COLONS, SLASH).classify
end

#to_controller_name(script_name, resources_name) ⇒ String

Returns controller name.

Parameters:

  • script_name (String)
  • resources_name (Class, String)

Returns:

  • (String)

    controller name



30
31
32
# File 'lib/utils/wallaby/inflector.rb', line 30

def to_controller_name(script_name, resources_name)
  to_class_name(to_script(script_name, to_resources_name(resources_name), CONTROLLER))
end

#to_decorator_name(script_name, resources_name) ⇒ String

Returns decorator name.

Parameters:

  • script_name (String)
  • resources_name (Class, String)

Returns:

  • (String)

    decorator name



37
38
39
# File 'lib/utils/wallaby/inflector.rb', line 37

def to_decorator_name(script_name, resources_name)
  to_class_name(to_script(script_name, to_resource_name(resources_name), DECORATOR))
end

#to_model_label(model_class) ⇒ String

Produce model label (e.g. ‘Namespace / Product`) for model class (e.g. `Namespace::Product`)

Parameters:

  • model_class (Class, String)

    model class

Returns:

  • (String)

    model label



86
87
88
89
90
91
92
# File 'lib/utils/wallaby/inflector.rb', line 86

def to_model_label(model_class)
  # TODO: change to use i18n translation
  return EMPTY_STRING if model_class.blank?

  model_class_name = to_model_name model_class
  model_class_name.titleize.gsub(SLASH, SPACE + SLASH + SPACE)
end

#to_model_name(name) ⇒ String

Returns resource name.

Parameters:

  • name (Class, String)

Returns:

  • (String)

    resource name



79
80
81
# File 'lib/utils/wallaby/inflector.rb', line 79

def to_model_name(name)
  to_class_name(to_resource_name(name))
end

#to_paginator_name(script_name, resources_name) ⇒ String

Returns paginator name.

Parameters:

  • script_name (String)
  • resources_name (Class, String)

Returns:

  • (String)

    paginator name



58
59
60
# File 'lib/utils/wallaby/inflector.rb', line 58

def to_paginator_name(script_name, resources_name)
  to_class_name(to_script(script_name, to_resource_name(resources_name), PAGINATOR))
end

#to_resource_name(name) ⇒ String

Returns resource name.

Parameters:

  • name (Class, String)

Returns:

  • (String)

    resource name



73
74
75
# File 'lib/utils/wallaby/inflector.rb', line 73

def to_resource_name(name)
  to_resources_name(name).singularize
end

#to_resources_name(name) ⇒ String

Returns resources name.

Parameters:

  • name (Class, String)

Returns:

  • (String)

    resources name



64
65
66
67
68
69
# File 'lib/utils/wallaby/inflector.rb', line 64

def to_resources_name(name)
  return EMPTY_STRING if name.blank?

  name = name.to_s unless name.is_a?(String)
  name.tableize
end

#to_script(script_name, resources_name, suffix = nil) ⇒ String

Returns generate the prefix for all classes.

Parameters:

  • script_name (String)
  • resources_name (Class, String)
  • suffix (String, nil) (defaults to: nil)

Returns:

  • (String)

    generate the prefix for all classes



22
23
24
25
# File 'lib/utils/wallaby/inflector.rb', line 22

def to_script(script_name, resources_name, suffix = nil)
  normalized_suffix = suffix.try(:underscore).try(:gsub, /\A_?/, UNDERSCORE)
  "#{script_name}/#{resources_name}#{normalized_suffix}".gsub(%r{\A/+}, EMPTY_STRING)
end

#to_servicer_name(script_name, resources_name) ⇒ String

Returns servicer name.

Parameters:

  • script_name (String)
  • resources_name (Class, String)

Returns:

  • (String)

    servicer name



51
52
53
# File 'lib/utils/wallaby/inflector.rb', line 51

def to_servicer_name(script_name, resources_name)
  to_class_name(to_script(script_name, to_resource_name(resources_name), SERVICER))
end