Class: Algebrick::ProductConstructors::Named

Inherits:
Abstract show all
Defined in:
lib/algebrick/product_constructors/named.rb

Instance Attribute Summary

Attributes inherited from Abstract

#fields

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods inherited from Abstract

#==, #initialize, name, #to_a, #to_ary, to_s, #type, type

Methods included from TypeCheck

#Child!, #Child?, #Match!, #Match?, #Type!, #Type?, error

Methods included from Value

#==, #inspect, #type

Methods included from Matching

#any, #match, match_value, #on

Constructor Details

This class inherits a constructor from Algebrick::ProductConstructors::Abstract

Class Method Details

+ (Object) type=(type)



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

def self.type=(type)
  super(type)
  raise unless type.field_names?
end

Instance Method Details

- (Object) pretty_print(q)



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/algebrick/product_constructors/named.rb', line 23

def pretty_print(q)
  q.group(1, "#{self.class.type.name}[", ']') do
    type.field_names.each_with_index do |name, i|
      if i == 0
        q.breakable ''
      else
        q.text ','
        q.breakable ' '
      end
      q.text name.to_s
      q.text ':'
      q.group(1) do
        q.breakable ' '
        q.pp self[name]
      end
    end
  end
end

- (Object) to_hash Also known as: to_h



42
43
44
# File 'lib/algebrick/product_constructors/named.rb', line 42

def to_hash
  type.field_names.inject({}) { |h, name| h.update name => self[name] }
end

- (Object) to_s



18
19
20
21
# File 'lib/algebrick/product_constructors/named.rb', line 18

def to_s
  "#{self.class.type.name}[" +
      type.field_names.map { |name| "#{name}: #{self[name].to_s}" }.join(', ') +']'
end

- (Object) update(fields)



53
54
55
# File 'lib/algebrick/product_constructors/named.rb', line 53

def update(fields)
  type[to_hash.merge fields]
end