Module: Objectified::ClassMethods

Defined in:
lib/objectified.rb

Overview

e.g. define object_type :finder for ApplicationFinder

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#object_type_stringObject

Returns the value of attribute object_type_string.



27
28
29
# File 'lib/objectified.rb', line 27

def object_type_string
  @object_type_string
end

Instance Method Details

#base_klass_stringObject

Returns the object name regardless of it being plural or singular

Example:

SomeController.base_klass_string
  => 'Some'

Example:

SomesController.base_klass_string
  => 'Somes'


78
79
80
81
82
83
84
# 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) ⇒ Object

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'


186
187
188
189
# 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) ⇒ Object

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'


48
49
50
# File 'lib/objectified.rb', line 48

def object_type(object_type_string)
  @object_type_string = object_type_string
end

#object_type_klass_stringObject

Returns the ‘object_type’ but capitalized and stringified

Example:

SomeController.object_type_klass_string
  => 'Controller'


61
62
63
# File 'lib/objectified.rb', line 61

def object_type_klass_string
  @object_type_string&.to_s&.camelcase
end

#record_instance_variable_name(namespace: false) ⇒ Object

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'


164
165
166
167
168
169
170
171
172
# 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) ⇒ Object

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'


141
142
143
144
145
146
147
148
149
# 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_klassObject

Returns the constantized class name of the resource

Example:

SomeController.records_klass
  => 'Some'


120
121
122
123
124
125
126
# 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_stringObject

Returns the singularized resource name

Example:

SomeController.resource_klass_string
  => 'Some'


94
95
96
# File 'lib/objectified.rb', line 94

def resources_klass_string
  base_klass_string.pluralize
end