Module: StructuredPolymorphic

Defined in:
lib/structured-poly.rb

Overview

Creates a Structured class that can turn into multiple kinds of Structured objects.

To use, include StructuredPolymorphic in a relevant class, and then within the class body use ClassMethods#type or ClassMethods#types to specify the different types of Structured objects that this class can produce.

When a StructuredPolymorphic object is initialized based on a hash, the hash is checked for a key called type. (The key can be changed using ClassMethods#set_type_key.) The value of that type key is used to determine what type of Structured object to create.

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

Extends ClassMethods to the including class’s class methods.



171
172
173
174
175
176
# File 'lib/structured-poly.rb', line 171

def self.included(base)
  if base.is_a?(Class)
    base.extend(ClassMethods)
    base.reset
  end
end

Instance Method Details

#initialize(*args, **params) ⇒ Object

This should never be called because the new method is overridden.

Raises:

  • (TypeError)


19
20
21
# File 'lib/structured-poly.rb', line 19

def initialize(*args, **params)
  raise TypeError, "Abstract StructuredPolymorphic class"
end