Module: NliPipeline::AbstractUtil

Included in:
DockerManager, FileManager, SystemWrapper::CallWrapper, SystemWrapper::CallWrapperError, SystemWrapper::ConfigError
Defined in:
lib/nli_pipeline/abstract_util/init_attrs.rb

Overview

Abstract Utilities for handling class instantiation similar to open struct, but enforces some arguments

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(parent) ⇒ Object

override include to automatically extend classmethods i.e. functions in the module ClassMethods becom class methods when the AbstractUtil is included



11
12
13
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 11

def self.included(parent)
  parent.extend(ClassMethods)
end

Instance Method Details

#add_attrs(**kwargs) ⇒ Object

keys become instance variable names value is set using corresponding value in kwargs all instance variables have getters, but not setters add attributes to class



28
29
30
31
32
33
34
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 28

def add_attrs(**kwargs)
  kwargs.each do |k, v|
    instance_variable_set("@#{k}", v)
    self.class.class_eval { attr_reader k.to_sym }
  end
  puts(to_s.yellow) if @debug
end

#drop_forbidden_args_message(**kwargs) ⇒ Object

support method for init_with_attrs to warn user about unused kwargs



37
38
39
40
41
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 37

def drop_forbidden_args_message(**kwargs)
  forbidden_args = self.class.get_forbidden_kwargs(**kwargs)
  show_warning = !forbidden_args.empty? && kwargs[:debug]
  puts("Warning, dropping args #{forbidden_args} to #{self.class}".red) if show_warning
end

#init_with_attrs(**kwargs) ⇒ Object

similar to openstruct initialize assigns all keyword arguments as instance variables but uses attr_reader, not attr_accessor



46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 46

def init_with_attrs(**kwargs)
  sanitized_args = self.class.get_allowed_kwargs(**kwargs)
  drop_forbidden_args_message(**kwargs)
  unless self.class.required_args?(**kwargs)
    # raise SystemWrapper::ConfigError.new()
    msg = "#{self.class}.#{caller_locations(1, 1)[0].label}"\
      " requires arguments: #{self.class.required_args}"
    raise ArgumentError, msg
  end
  # merge passed args with default values for args
  merged_args = self.class.supported_args.merge(sanitized_args)
  add_attrs(**merged_args)
end

#raise_unless_all(**kwargs) ⇒ Boolean

Returns:

  • (Boolean)

Raises:

  • (ArgumentError)

    unless all values in kwargs are truthy



17
18
19
20
21
22
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 17

def raise_unless_all(**kwargs)
  message = "All #{kwargs} must be truthy to use"\
    " #{self.class}.#{caller_locations(1, 1)[0].label}"
  raise ArgumentError, message unless kwargs.values.all?
  true
end

#to_sObject

override default to_s for nice formatting of instance vars



61
62
63
64
65
66
67
# File 'lib/nli_pipeline/abstract_util/init_attrs.rb', line 61

def to_s
  format_instance_vars = lambda do |x, y|
    x + format("\n%<key>-30s: %<value>s", key: y, value: instance_variable_get(y.to_sym))
  end
  vars = instance_variables.reduce('', &format_instance_vars)
  "\n#{self.class} config#{vars}\n"
end