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, verb) ⇒ Class

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

Parameters:

  • verb (String)

Returns:

  • (Class)

    default operation class



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/skinny_controllers/lookup/operation.rb', line 22

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

  operation_class_name = "#{namespace.name}::#{verb}"
  default = operation_class_name.safe_constantize

  unless default
    SkinnyControllers.logger.warn("#{operation_class_name} not found. Creating default...")
  end

  default || namespace.const_set(verb, 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



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

def default_operation_namespace_for(model_name)
  desired_namespace = namespace_from_model(model_name)
  namespace = desired_namespace.safe_constantize

  unless namespace
    SkinnyControllers.logger.warn("#{desired_namespace} not found. Creating...")
  end

  namespace || Namespace.create_namespace(desired_namespace)
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
# File 'lib/skinny_controllers/lookup/operation.rb', line 59

def name_from_model(model_name, verb)
  namespace = Lookup::Operation.namespace_from_model(model_name)
  "#{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



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

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, verb)
end