Module: NliPipeline::AbstractUtil::ClassMethods

Defined in:
lib/nli_pipeline/abstract_util/init_attrs.rb

Overview

methods to be loaded as static/class methods in class that extends AbstractUtil

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args) ⇒ Object

:nocov: use method_missing to force classes that use AbstractUtil to implement

supported_args and required_args.

Parameters:

  • method (String)
  • args (Array)


76
77
78
79
80
81
82
83
84
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 76

def method_missing(method, *args)
  # cast methods to array of symbols, compare to current missing method
  if %i[supported_args required_args].include?(method.to_sym)
    message = "Method #{method} must be implemented in #{self.class} "\
      'in order to use AbstractUtil'
    raise ArgumentError, message
  end
  super
end

Instance Method Details

#get_allowed_kwargs(**kwargs) ⇒ Hash

Returns args that can be converted into instance vars for class.

Returns:

  • (Hash)

    args that can be converted into instance vars for class



95
96
97
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 95

def get_allowed_kwargs(**kwargs)
  kwargs.select { |k, _v| supported_args.key?(k) }
end

#get_forbidden_kwargs(**kwargs) ⇒ Hash

Returns args that can’t be converted into instance vars for class.

Returns:

  • (Hash)

    args that can’t be converted into instance vars for class



100
101
102
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 100

def get_forbidden_kwargs(**kwargs)
  kwargs.reject { |k, _v| supported_args.key?(k) }
end

#required_args?(**kwargs) ⇒ Boolean

Returns have all required args been passed?.

Returns:

  • (Boolean)

    have all required args been passed?



105
106
107
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 105

def required_args?(**kwargs)
  required_args.all? { |key| kwargs.key?(key) }
end

#respond_to_missing?(method, *args) ⇒ Boolean

Returns:

  • (Boolean)


90
91
92
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 90

def respond_to_missing?(method, *args)
  %i[supported_args required_args].include?(method.to_sym) || super
end