Class: ADT::Constructor
- Inherits:
-
Object
- Object
- ADT::Constructor
- Defined in:
- lib/adt/constructor.rb
Instance Attribute Summary collapse
-
#klass ⇒ Object
readonly
Returns the value of attribute klass.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#others ⇒ Object
readonly
Returns the value of attribute others.
Instance Method Summary collapse
-
#initialize(parent, name, parameters, &block) ⇒ Constructor
constructor
A new instance of Constructor.
- #inspect ⇒ Object
- #to_s ⇒ Object
- #|(other) ⇒ Object
Constructor Details
#initialize(parent, name, parameters, &block) ⇒ Constructor
Returns a new instance of Constructor.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/adt/constructor.rb', line 5 def initialize(parent, name, parameters, &block) @name = name @klass = Class.new(parent, &block) keys = parameters.keys types = parameters.values @klass.class_eval """ attr_reader #{keys.map { |n| ":#{n}" }.join(', ')} def initialize(#{keys.join(", ")}) types = [#{keys.join(", ")}].map(&:class) raise TypeError, 'Types mismatch: given ' + types.join(', ') + ', expected #{types.join(", ")}' unless types == [#{types.join(", ")}] #{keys.empty? ? "" : keys.map{ |n| "@#{n}" }.join(',') + " = " + keys.join(", ")} end def ==(other) other.is_a?(self.class) && #{keys.empty? ? "true" : keys.map { |key| "#{key} == other.#{key}"}.join(" && ")} end " @parameters = parameters @others = [] end |
Instance Attribute Details
#klass ⇒ Object (readonly)
Returns the value of attribute klass.
3 4 5 |
# File 'lib/adt/constructor.rb', line 3 def klass @klass end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/adt/constructor.rb', line 3 def name @name end |
#others ⇒ Object (readonly)
Returns the value of attribute others.
3 4 5 |
# File 'lib/adt/constructor.rb', line 3 def others @others end |
Instance Method Details
#inspect ⇒ Object
31 32 33 |
# File 'lib/adt/constructor.rb', line 31 def inspect to_s end |
#to_s ⇒ Object
27 28 29 |
# File 'lib/adt/constructor.rb', line 27 def to_s "#{@name}(#{@parameters})" end |
#|(other) ⇒ Object
35 36 37 38 |
# File 'lib/adt/constructor.rb', line 35 def |(other) @others << other self end |