Module: ConvenientService::Core::Concern::ClassMethods

Defined in:
lib/convenient_service/core/concern/class_methods.rb

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, **kwargs, &block) ⇒ void (private)

Note:

Config commitment via a missing class method is very common. Convenient Service Standard config does that by .new, .result and .step most of the time.

This method returns an undefined value.

Commits config. In other words, includes concerns into the mixing class. If method is still NOT defined, raises NoMethodError, otherwise - retries to call the method.

Parameters:

  • method (Symbol)
  • args (Array<Object>)
  • kwargs (Hash{Symbol => Object})
  • block (Proc, nil)


129
130
131
132
133
134
135
136
137
138
139
# File 'lib/convenient_service/core/concern/class_methods.rb', line 129

def method_missing(method, *args, **kwargs, &block)
  commit_config!(trigger: Constants::Triggers::CLASS_METHOD_MISSING)

  return ::ConvenientService.reraise { super } unless Utils::Module.class_method_defined?(self, method, public: true, protected: false, private: false)

  return ::ConvenientService.reraise { super } if middlewares(method, scope: :class).defined_without_super_method?

  ConvenientService.logger.debug { "[Core] Committed config for `#{self}` | Triggered by `method_missing` | Method: `.#{method}`" }

  __send__(method, *args, **kwargs, &block)
end

Instance Method Details

#__convenient_service_config__Object



16
17
18
# File 'lib/convenient_service/core/concern/class_methods.rb', line 16

def __convenient_service_config__
  @__convenient_service_config__ ||= Entities::Config.new(klass: self)
end

#commit_config!(trigger: ConvenientService::Core::Constants::Triggers::USER) ⇒ Boolean

Commits config when called for the first time. Does nothing for the subsequent calls.

Parameters:

Returns:

  • (Boolean)

    true if called for the first time, false otherwise (similarly as Kernel#require).

See Also:



55
56
57
58
# File 'lib/convenient_service/core/concern/class_methods.rb', line 55

def commit_config!(trigger: ConvenientService::Core::Constants::Triggers::USER)
  __convenient_service_config__.commit!(trigger: trigger)
    .tap { ConvenientService.logger.debug { "[Core] Committed config for `#{self}` | Triggered by `.commit_config!(trigger: #{trigger.inspect})` " } }
end

#concernsObject



28
29
30
# File 'lib/convenient_service/core/concern/class_methods.rb', line 28

def concerns(...)
  __convenient_service_config__.concerns(...)
end

#has_committed_config?Boolean

Returns true when config is committed, otherwise - false.

Returns:

  • (Boolean)

    Returns true when config is committed, otherwise - false.



42
43
44
# File 'lib/convenient_service/core/concern/class_methods.rb', line 42

def has_committed_config?
  __convenient_service_config__.committed?
end

#middlewaresObject



35
36
37
# File 'lib/convenient_service/core/concern/class_methods.rb', line 35

def middlewares(...)
  __convenient_service_config__.middlewares(...)
end

#new(*args, **kwargs, &block) ⇒ Object

Returns Can be any type.

Parameters:

  • args (Array<Object>)
  • kwargs (Hash{Symbol => Object})
  • block (Proc, nil)

Returns:

  • (Object)

    Can be any type.



71
72
73
# File 'lib/convenient_service/core/concern/class_methods.rb', line 71

def new(*args, **kwargs, &block)
  has_committed_config? ? super : method_missing(:new, *args, **kwargs, &block)
end