module Objectified::ClassMethods

e.g. define object_type :finder for ApplicationFinder

Attributes

object_type_string[RW]

Public Instance Methods

base_klass_string() click to toggle source

Returns the object name regardless of it being plural or singular

Example:

SomeController.base_klass_string
  => 'Some'

Example:

SomesController.base_klass_string
  => 'Somes'
# File lib/objectified.rb, line 78
def base_klass_string
  unless object_type_klass_string
    raise 'No Object Type Defined. please override object_type in your class => e.g. object_type :controller'
  end

  to_s.gsub(object_type_klass_string, '')
end
object_klass_for(object_type) click to toggle source

Mixin to extract class names on demand.

Example:

SomeNamespace::SomeController.record_instance_variable_name
  => 'some_namespace_some'

Example:

SomeNamespace::SomeController.record_instance_variable_name(namespace: true)
  => 'some'
# File lib/objectified.rb, line 186
def object_klass_for(object_type)
  str = "#{resource_klass_string.pluralize}#{object_type.to_s.camelize}"
  str.constantize
end
object_type(object_type_string) click to toggle source

The entry point of the api is the :object_type method.

Note:

Do not use for classes with the prefix 'Application'.

Args:

object_type_string: (String)

Example:

class SomeController
  object_type :controller
end

SomeController.resource_klass_string
  => 'Some'
# File lib/objectified.rb, line 48
def object_type(object_type_string)
  @object_type_string = object_type_string
end
object_type_klass_string() click to toggle source

Returns the 'object_type' but capitalized and stringified

Example:

SomeController.object_type_klass_string
  => 'Controller'
# File lib/objectified.rb, line 61
def object_type_klass_string
  @object_type_string&.to_s&.camelcase
end
record_instance_variable_name(namespace: false) click to toggle source

Returns the name of a the **member variable** for the resource class, with the option of stripping it's namespaces.

Example:

SomeNamespace::SomeController.record_instance_variable_name
  => 'some_namespace_some'

Example:

SomeNamespace::SomeController.record_instance_variable_name(namespace: true)
  => 'some'
# File lib/objectified.rb, line 164
def record_instance_variable_name(namespace: false)
  return if resource_klass_string == 'Application'

  if namespace
    records_klass.to_s.underscore.downcase
  else
    records_klass.to_s.underscore.downcase.split('/').last
  end
end
records_instance_variable_name(namespace: false) click to toggle source

Returns the name of a the **collection variable** for the resource class, with the option of stripping it's namespaces.

Example:

SomeNamespace::SomeController.records_instance_variable_name
  => 'some_namespace_somes'

Example:

SomeNamespace::SomeController.records_instance_variable_name(namespace: true)
  => 'somes'
# File lib/objectified.rb, line 141
def records_instance_variable_name(namespace: false)
  return if resource_klass_string == 'Application'

  if namespace
    records_klass.to_s.underscore.downcase.pluralize.gsub('/', '_')
  else
    records_klass.to_s.underscore.downcase.pluralize.split('/').last
  end
end
records_klass() click to toggle source

Returns the constantized class name of the resource

Example:

SomeController.records_klass
  => 'Some'
# File lib/objectified.rb, line 120
def records_klass
  return if resource_klass_string == 'Application'

  resource_klass_string&.constantize
rescue NameError
  resource_klass_string
end
resources_klass_string() click to toggle source

Returns the pluralized resource name

Example:

SomeController.resources_klass_string
  => 'Somes'
# File lib/objectified.rb, line 94
def resources_klass_string
  base_klass_string.pluralize
end