Class: Manioc::Struct

Inherits:
Object
  • Object
show all
Defined in:
lib/manioc/struct.rb

Instance Method Summary collapse

Constructor Details

#initialize(**fields) ⇒ Struct

Returns a new instance of Struct.



26
27
28
29
30
31
# File 'lib/manioc/struct.rb', line 26

def initialize **fields
  fields = self.class.defaults.merge fields
  _validate fields
  _assign   fields
  freeze if Manioc.frozen?
end

Instance Method Details

#==(other) ⇒ Object



33
34
35
# File 'lib/manioc/struct.rb', line 33

def == other
  super || to_h == other.to_h
end

#inspectObject



45
46
47
48
49
# File 'lib/manioc/struct.rb', line 45

def inspect
  # :nocov:
  %|<#{self.class.name}(#{self.class.fields.join(', ')})>|
  # :nocov:
end

#to_hObject



41
42
43
# File 'lib/manioc/struct.rb', line 41

def to_h
  self.class.fields.each_with_object({}) { |field,h| h[field] = public_send field }
end

#with(**fields) ⇒ Object



37
38
39
# File 'lib/manioc/struct.rb', line 37

def with **fields
  self.class.new to_h.merge fields
end