Class: SleepingKingStudios::Tools::Toolbox::HeritableData::HeritableMethods

Inherits:
Module
  • Object
show all
Defined in:
lib/sleeping_king_studios/tools/toolbox/heritable_data.rb

Overview

Custom module used to propagate heritable methods across Data classes.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parent_class: nil) { ... } ⇒ HeritableMethods

Returns a new instance of HeritableMethods.

Parameters:

  • parent_class (Class, nil) (defaults to: nil)

    the parent data class, if any.

Yields:

  • additional methods to define on the Data class.



121
122
123
124
125
126
127
# File 'lib/sleeping_king_studios/tools/toolbox/heritable_data.rb', line 121

def initialize(parent_class: nil, &methods)
  super(&nil)

  @parent_class = parent_class

  class_exec(&methods) if methods
end

Instance Attribute Details

#parent_classClass? (readonly)

Returns the parent data class.

Returns:

  • (Class, nil)

    the parent data class.



130
131
132
# File 'lib/sleeping_king_studios/tools/toolbox/heritable_data.rb', line 130

def parent_class
  @parent_class
end

Instance Method Details

#each_heritable_moduleObject

Iterates over the ::HeritableMethods modules for parent classes.



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/sleeping_king_studios/tools/toolbox/heritable_data.rb', line 133

def each_heritable_module
  return enum_for(:each_heritable_module) unless block_given?

  ancestor = self

  while ancestor
    yield ancestor

    ancestor = ancestor.parent_class&.then { |mod| mod::HeritableMethods }
  end
end