Class: Tensai::Util::Initializer
- Inherits:
-
Module
- Object
- Module
- Tensai::Util::Initializer
- Defined in:
- lib/tensai/util/initializer.rb
Overview
Include in classes to insert boilerplate constructor, dry-types typecheck and getters
Defined Under Namespace
Classes: InitializeMethod
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
Instance Method Summary collapse
-
#included(klass) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize.
-
#initialize(**args) ⇒ Initializer
constructor
A new instance of Initializer.
Constructor Details
#initialize(**args) ⇒ Initializer
Returns a new instance of Initializer.
13 14 15 |
# File 'lib/tensai/util/initializer.rb', line 13 def initialize(**args) @args = args end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
11 12 13 |
# File 'lib/tensai/util/initializer.rb', line 11 def args @args end |
Instance Method Details
#included(klass) ⇒ Object
rubocop:disable Metrics/MethodLength, Metrics/AbcSize
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tensai/util/initializer.rb', line 17 def included(klass) # rubocop:disable Metrics/MethodLength, Metrics/AbcSize this_class_args = args all_args = all_args_for(klass) klass.instance_eval do define_method :__check_argument_type do |argument, value| type = all_args[argument] if type.default? && value.nil? instance_variable_set("@#{argument}".to_sym, type[]) else type[value] end end define_method :after_initialize, -> {} attr_reader(*this_class_args.keys) end klass.class_eval InitializeMethod.new(all_args).code, __FILE__, __LINE__ + 11 end |