Module: SkinnyControllers::Lookup::Operation

Defined in:
lib/skinny_controllers/lookup/operation.rb

Class Method Summary collapse

Class Method Details

.default_operation_class_for(model_name) ⇒ Class

dynamically creates a module for the model if it isn’t already defined

Returns:

  • (Class)

    default operation class



29
30
31
32
33
34
35
# File 'lib/skinny_controllers/lookup/operation.rb', line 29

def default_operation_class_for(model_name)
  default_operation = SkinnyControllers::Operation::Default
  namespace = Lookup::Operation.default_operation_namespace_for(model_name)

  default = "#{namespace.name}::Default".safe_constantize
  default || namespace.const_set('Default'.freeze, default_operation.dup)
end

.default_operation_namespace_for(model_name) ⇒ Class

Returns namespace for the default operation class.

Returns:

  • (Class)

    namespace for the default operation class



38
39
40
41
42
43
44
# File 'lib/skinny_controllers/lookup/operation.rb', line 38

def default_operation_namespace_for(model_name)
  # binding.pry
  desired_namespace = namespace_from_model(model_name)
  parent_namespace = SkinnyControllers.operations_namespace
  namespace = "#{parent_namespace}::#{desired_namespace}".safe_constantize
  namespace || Namespace.create_namespace(desired_namespace)
end

.from_controller(controller, verb, model_name = nil) ⇒ Class

Returns the class or default.

Parameters:

  • controller (String)

    name of the controller class

  • verb (String)
  • model_name (String) (defaults to: nil)

    optional

Returns:

  • (Class)

    the class or default



21
22
23
24
# File 'lib/skinny_controllers/lookup/operation.rb', line 21

def from_controller(controller, verb, model_name = nil)
  model_name ||= Lookup::Controller.model_name(controller)
  operation_of(model_name, verb)
end

.name_from_model(model_name, verb) ⇒ String

Returns the operation based on the model name.

Examples:

‘Model’, ‘Verb’ => [optional namespace]::ModelOperations::Verb


Parameters:

  • model_name (String)

    name of the model

  • the (String)

    verb/action for the operation

Returns:

  • (String)

    the operation based on the model name



59
60
61
62
63
64
# File 'lib/skinny_controllers/lookup/operation.rb', line 59

def name_from_model(model_name, verb)
  # this namespace is '' by default
  prefix = SkinnyControllers.operations_namespace
  namespace = Lookup::Operation.namespace_from_model(model_name)
  "#{prefix}::#{namespace}::#{verb}"
end

.namespace_from_model(model_name) ⇒ String

Returns the operation namespace based on the model name.

Examples:

‘Object’ => ‘ObjectOperations’


Returns:

  • (String)

    the operation namespace based on the model name



50
51
52
# File 'lib/skinny_controllers/lookup/operation.rb', line 50

def namespace_from_model(model_name)
  "#{model_name}#{SkinnyControllers.operations_suffix}"
end

.operation_of(model_name, verb) ⇒ Class

Returns the operation based on the model name and the verb.

Examples:

ObjectOperations::Verb


Parameters:

  • model_name (String)

    name of the model

  • verb (String)

    the verb/action for the operation

Returns:

  • (Class)

    the operation based on the model name and the verb



11
12
13
14
15
# File 'lib/skinny_controllers/lookup/operation.rb', line 11

def operation_of(model_name, verb)
  klass_name = Lookup::Operation.name_from_model(model_name, verb)
  klass = klass_name.safe_constantize
  klass || default_operation_class_for(model_name)
end