Module: Wallaby::Baseable::ClassMethods

Included in:
ModelAuthorizer, ModelPaginator, ModelServicer, ResourceDecorator
Defined in:
lib/concerns/wallaby/baseable.rb

Overview

Configurable attributes:

  1. mark a class as a base class

  2. guess the model class if model class isn’t given

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#base_classClass (readonly)

Returns The base class or the one from super class.

Returns:

  • (Class)

    The base class or the one from super class



25
26
27
# File 'lib/concerns/wallaby/baseable.rb', line 25

def base_class
  @base_class || superclass.try(:base_class)
end

#model_classClass?

Examples:

To configure the model class

class Admin::ProductAuthorizer < Admin::ApplicationAuthorizer
  self.model_class = Product
end

To configure the model class for version below 5.2.0

class Admin::ProductAuthorizer < Admin::ApplicationAuthorizer
  def self.model_class
    Product
  end
end

Returns:

  • (Class)

    assigned model class or Wallaby will guess it (see Guesser.class_for)

  • (nil)

    if current class is marked as base class

Raises:

  • (ClassNotFound)

    if model class isn’t found

  • (ArgumentError)

    if base class is empty



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/concerns/wallaby/baseable.rb', line 52

def model_class
  return if base_class?

  @model_class ||= Guesser.class_for(name) || raise(
    ClassNotFound, <<~INSTRUCTION
      The `model_class` hasn't been provided for Class `#{name}` and Wallaby cannot guess it right.
      If `#{name}` is supposed to be a base class, add the following line to its class declaration:

        class #{name}
          base_class!
        end

      Otherwise, please specify the `model_class` in `#{name}`'s declaration as follows:

        class #{name}
          self.model_class = CorrectModelClass
        end
    INSTRUCTION
  )
end

Instance Method Details

#base_class!Object

Mark the current class as the base class



19
20
21
# File 'lib/concerns/wallaby/baseable.rb', line 19

def base_class!
  @base_class = self
end

#base_class?true, false

Returns:

  • (true)

    if class is a base class

  • (false)

    if class is not a base class



14
15
16
# File 'lib/concerns/wallaby/baseable.rb', line 14

def base_class?
  @base_class == self
end

#namespaceObject

Deprecated.


79
80
81
# File 'lib/concerns/wallaby/baseable.rb', line 79

def namespace
  Deprecator.alert method(__callee__), from: '0.3.0'
end

#namespace=(_namespace) ⇒ Object

Deprecated.


74
75
76
# File 'lib/concerns/wallaby/baseable.rb', line 74

def namespace=(_namespace)
  Deprecator.alert method(__callee__), from: '0.3.0'
end