Module: Caprese::Versioning

Extended by:
ActiveSupport::Concern
Included in:
Controller, Controller, Serializer, Serializer
Defined in:
lib/caprese/concerns/versioning.rb

Instance Method Summary collapse

Instance Method Details

#namespaced_dot_path(suffix = nil) ⇒ String

Get full namespaced dot path (translations)

Examples:

namespaced_dot_path('orders') => 'api/v1/orders'

Parameters:

  • suffix (String) (defaults to: nil)

    the string to append, hypothetically an extension in this namespace’s dot space

Returns:

  • (String)

    the full namespaced dot path of the suffix passed in



44
45
46
# File 'lib/caprese/concerns/versioning.rb', line 44

def namespaced_dot_path(suffix = nil)
  namespaced_module.downcase.split('::').push(suffix).reject(&:nil?).join('.')
end

#namespaced_module(suffix = nil) ⇒ String

Get full namespaced module name of object (class)

Examples:

namespaced_module('OrdersController') => 'API::V1::OrdersController'

Parameters:

  • suffix (String) (defaults to: nil)

    the string to append, hypothetically a module in this namespace’s module space

Returns:

  • (String)

    the namespaced modulized name of the suffix passed in



15
16
17
18
19
20
21
22
# File 'lib/caprese/concerns/versioning.rb', line 15

def namespaced_module(suffix = nil)
  mod = (self.is_a?(Class) ? self : self.class).name.deconstantize

  name = suffix || mod
  name = name.prepend("#{mod}::") unless suffix.nil? || (/^#{mod}::\.*/).match(suffix)

  name
end

#namespaced_name(suffix = nil) ⇒ String

Get full namespaced underscore name (routes)

Examples:

namespaced_name('orders') => 'api_v1_orders'

Parameters:

  • suffix (String) (defaults to: nil)

    the string to append, hypothetically a name that needs to be namespaced

Returns:

  • (String)

    the namespaced name of the suffix passed in



56
57
58
# File 'lib/caprese/concerns/versioning.rb', line 56

def namespaced_name(suffix = nil)
  namespaced_module.downcase.split('::').push(suffix).reject(&:nil?).join('_')
end

#namespaced_path(suffix = nil) ⇒ String

Get full namespaced path (url)

Examples:

namespaced_path('orders') => 'api/v1/orders'

Parameters:

  • suffix (String) (defaults to: nil)

    the string to append, hypothetically an extension in this namespace’s path space

Returns:

  • (String)

    the full namespaced path name of the suffix passed in



32
33
34
# File 'lib/caprese/concerns/versioning.rb', line 32

def namespaced_path(suffix = nil)
  namespaced_module.downcase.split('::').push(suffix).reject(&:nil?).join('/')
end

#unnamespace(str) ⇒ String

Strips string to raw unnamespaced format regardless of input format

Parameters:

  • str (String)

    the string to unversion

Returns:

  • (String)

    the stripped, unnamespaced name of the string passed in



64
65
66
67
68
69
70
# File 'lib/caprese/concerns/versioning.rb', line 64

def unnamespace(str)
  str
  .remove(namespaced_module(''))
  .remove(namespaced_path(''))
  .remove(namespaced_name(''))
  .remove(namespaced_dot_path(''))
end

#unversion(str) ⇒ String

Strips string to raw unversioned format regardless of input format

Parameters:

  • str (String)

    the string to unversion

Returns:

  • (String)

    the stripped, unversioned name of the string passed in



132
133
134
135
136
137
138
# File 'lib/caprese/concerns/versioning.rb', line 132

def unversion(str)
  str
  .remove(version_module(''))
  .remove(version_path(''))
  .remove(version_name(''))
  .remove(version_dot_path(''))
end

#version_dot_path(suffix = nil) ⇒ String

Get version dot path (translations)

Examples:

version_dot_path('orders') => 'api/v1/orders'

Parameters:

  • suffix (String) (defaults to: nil)

    the string to append, hypothetically an extension in this version’s dot space

Returns:

  • (String)

    the versioned dot path of the suffix passed in



110
111
112
# File 'lib/caprese/concerns/versioning.rb', line 110

def version_dot_path(suffix = nil)
  version_module.downcase.split('::').push(suffix).reject(&:nil?).join('.')
end

#version_module(suffix = nil) ⇒ String

Note:

The difference between namespaced and versioned module names is that if an isolated_namespace is present, version_module will return a module name without the isolated namespace

Get versioned module name of object (class)

Examples:

version_module('OrdersController') => 'API::V1::OrdersController'

Parameters:

  • suffix (String) (defaults to: nil)

    the string to append, hypothetically a module in this version’s module space

Returns:

  • (String)

    the versioned modulized name of the suffix passed in



82
83
84
85
86
87
88
# File 'lib/caprese/concerns/versioning.rb', line 82

def version_module(suffix = nil)
  name = namespaced_module(suffix)

  name = name.gsub("#{Caprese.config.isolated_namespace}::", '') if Caprese.config.isolated_namespace

  name
end

#version_name(suffix = nil) ⇒ String

Note:

The difference between namespaced and versioned module names is that if an isolated_namespace is present, version_module will return a module name without the isolated namespace

Get versioned underscore name (routes)

Examples:

version_name('orders') => 'api_v1_orders'

Parameters:

  • suffix (String) (defaults to: nil)

    the string to append, hypothetically a name that needs to be versioned

Returns:

  • (String)

    the versioned name of the suffix passed in



124
125
126
# File 'lib/caprese/concerns/versioning.rb', line 124

def version_name(suffix = nil)
  version_module.downcase.split('::').push(suffix).reject(&:nil?).join('_')
end

#version_path(suffix = nil) ⇒ String

Get versioned path (url)

Examples:

version_path('orders') => 'api/v1/orders'

Parameters:

  • suffix (String) (defaults to: nil)

    the string to append, hypothetically an extension in this version’s path space

Returns:

  • (String)

    the versioned path name of the suffix passed in



98
99
100
# File 'lib/caprese/concerns/versioning.rb', line 98

def version_path(suffix = nil)
  version_module.downcase.split('::').push(suffix).reject(&:nil?).join('/')
end