Module: StructuredPolymorphic::ClassMethods
- Defined in:
- lib/structured-poly.rb
Instance Method Summary collapse
-
#description(len = nil) ⇒ Object
Returns the class’s description.
-
#each ⇒ Object
Iterates through all the types.
-
#explain(io = STDOUT) ⇒ Object
Prints out documentation for this class.
- #inherited(base) ⇒ Object
- #input_err(text) ⇒ Object
-
#new(hash, parent = nil) ⇒ Object
Constructs a new object of this StructuredPolymorphic type, by inspecting the hash’s type identifier and calling the corresponding class’s constructor.
- #reset ⇒ Object
-
#set_description(desc) ⇒ Object
Provides a description of this class, for use with the #explain method.
-
#set_type_key(key) ⇒ Object
Sets the hash key in which the polymorphic subtype is identified.
- #template(indent: '') ⇒ Object
-
#type(name, subclass) ⇒ Object
Adds a new subtype to this polymorphic superclass.
-
#type_for(name) ⇒ Object
Returns the class corresponding to the given type.
-
#types(**params) ⇒ Object
Adds multiple subtypes by repeatedly calling #type for all key-value pairs.
Instance Method Details
#description(len = nil) ⇒ Object
Returns the class’s description. The given number can be used to limit the length of the description.
42 43 44 45 46 47 48 49 |
# File 'lib/structured-poly.rb', line 42 def description(len = nil) desc = @class_description || '' if len && desc.length > len return desc[0, len] if len <= 5 return desc[0, len - 3] + '...' end return desc end |
#each ⇒ Object
Iterates through all the types.
92 93 94 |
# File 'lib/structured-poly.rb', line 92 def each @subclasses.sort.each do |type, c| yield(type, c) end end |
#explain(io = STDOUT) ⇒ Object
Prints out documentation for this class.
99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/structured-poly.rb', line 99 def explain(io = STDOUT) io.puts("Polymorphic Structured Class #{self}:") if @class_description io.puts("\n" + TextTools.line_break(@class_description, prefix: ' ')) end io.puts io.puts "Available subtypes:" max_type_len = @subclasses.keys.map(&:to_s).map(&:length).max @subclasses.sort.each do |type, c| desc = c.description(80 - max_type_len - 5) desc = c.name if desc == '' io.puts " #{type.to_s.ljust(max_type_len)} #{desc}" end end |
#inherited(base) ⇒ Object
158 159 160 |
# File 'lib/structured-poly.rb', line 158 def inherited(base) base.include(Structured) end |
#input_err(text) ⇒ Object
162 163 164 |
# File 'lib/structured-poly.rb', line 162 def input_err(text) raise Structured::InputError, text end |
#new(hash, parent = nil) ⇒ Object
Constructs a new object of this StructuredPolymorphic type, by inspecting the hash’s type identifier and calling the corresponding class’s constructor.
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/structured-poly.rb', line 131 def new(hash, parent = nil) # For subclasses, don't use this overridden new method. if self.include?(Structured) return super(hash, parent) end Structured.trace(self) do type = hash[@type_key] || hash[@type_key.to_s] input_err("no type") unless type type_class = @subclasses[type.to_sym] input_err("Unknown #{name} type #{type}") unless type_class # Remove the type key when initializing the subclass new_hash = hash.dup new_hash.delete(@type_key) new_hash.delete(@type_key.to_s) o = type_class.new(new_hash, parent) # Set the type value o.instance_variable_set(:@type, type) return o end end |
#reset ⇒ Object
25 26 27 28 29 |
# File 'lib/structured-poly.rb', line 25 def reset @subclasses = {} @class_description = nil @type_key = :type end |
#set_description(desc) ⇒ Object
Provides a description of this class, for use with the #explain method.
34 35 36 |
# File 'lib/structured-poly.rb', line 34 def set_description(desc) @class_description = desc end |
#set_type_key(key) ⇒ Object
Sets the hash key in which the polymorphic subtype is identified. By default the key is :type
.
55 56 57 |
# File 'lib/structured-poly.rb', line 55 def set_type_key(key) @type_key = key.to_sym end |
#template(indent: '') ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 |
# File 'lib/structured-poly.rb', line 114 def template(indent: '') res = "#{indent}# #{name}\n" if @class_description res << indent res << TextTools.line_break(@class_description, prefix: "#{indent}# ") res << "\n" end res << indent << "type: \n" res << indent << "...\n" return res end |
#type(name, subclass) ⇒ Object
Adds a new subtype to this polymorphic superclass.
65 66 67 68 69 70 |
# File 'lib/structured-poly.rb', line 65 def type(name, subclass) unless subclass.include?(Structured) raise ArgumentError, "#{subclass} is not Structured" end @subclasses[name.to_sym] = subclass end |
#type_for(name) ⇒ Object
Returns the class corresponding to the given type.
85 86 87 |
# File 'lib/structured-poly.rb', line 85 def type_for(name) return @subclasses[name.to_sym] end |
#types(**params) ⇒ Object
Adds multiple subtypes by repeatedly calling #type for all key-value pairs.
76 77 78 79 80 |
# File 'lib/structured-poly.rb', line 76 def types(**params) params.each do |name, subclass| type(name, subclass) end end |