Class: Algebrick::ProductConstructors::Abstract

Inherits:
Object
  • Object
show all
Extended by:
TypeCheck
Includes:
Value
Defined in:
lib/algebrick/product_constructors/abstract.rb

Direct Known Subclasses

Basic, Named

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TypeCheck

Child!, Child?, Match!, Match?, Type!, Type?

Methods included from Value

#inspect, #pretty_print, #to_s

Methods included from Matching

#any, #match, #match?, #on

Constructor Details

#initialize(*fields) ⇒ Abstract

Returns a new instance of Abstract.



22
23
24
25
26
27
# File 'lib/algebrick/product_constructors/abstract.rb', line 22

def initialize(*fields)
  if fields.size == 1 && fields.first.is_a?(Hash)
    fields = type.field_names.map { |k| fields.first[k] }
  end
  @fields = fields.zip(self.class.type.fields).map { |field, type| Type! field, type }.freeze
end

Instance Attribute Details

#fieldsObject (readonly)

Returns the value of attribute fields.



20
21
22
# File 'lib/algebrick/product_constructors/abstract.rb', line 20

def fields
  @fields
end

Class Method Details

.nameObject



56
57
58
# File 'lib/algebrick/product_constructors/abstract.rb', line 56

def self.name
  @type.to_s
end

.to_sObject



60
61
62
# File 'lib/algebrick/product_constructors/abstract.rb', line 60

def self.to_s
  name
end

.typeObject



48
49
50
# File 'lib/algebrick/product_constructors/abstract.rb', line 48

def self.type
  @type || raise
end

.type=(type) ⇒ Object



64
65
66
67
68
69
# File 'lib/algebrick/product_constructors/abstract.rb', line 64

def self.type=(type)
  Type! type, ProductVariant
  raise if @type
  @type = type
  include type
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



37
38
39
40
# File 'lib/algebrick/product_constructors/abstract.rb', line 37

def ==(other)
  return false unless other.kind_of? self.class
  @fields == other.fields
end

#hashObject



44
45
46
# File 'lib/algebrick/product_constructors/abstract.rb', line 44

def hash
  [self.class, @fields].hash
end

#to_aObject



33
34
35
# File 'lib/algebrick/product_constructors/abstract.rb', line 33

def to_a
  @fields
end

#to_aryObject



29
30
31
# File 'lib/algebrick/product_constructors/abstract.rb', line 29

def to_ary
  @fields
end

#typeObject



52
53
54
# File 'lib/algebrick/product_constructors/abstract.rb', line 52

def type
  self.class.type
end

#update(*fields) ⇒ Object

Raises:

  • (NotImplementedError)


71
72
73
# File 'lib/algebrick/product_constructors/abstract.rb', line 71

def update(*fields)
  raise NotImplementedError
end