Class: Navigable::ImmutableObject
- Inherits:
-
Object
- Object
- Navigable::ImmutableObject
- Defined in:
- lib/navigable/immutable_object.rb
Direct Known Subclasses
Class Attribute Summary collapse
-
.attribute_names ⇒ Object
readonly
Returns the value of attribute attribute_names.
Class Method Summary collapse
Instance Method Summary collapse
- #attributes ⇒ Object
-
#initialize(**args) ⇒ ImmutableObject
constructor
A new instance of ImmutableObject.
Constructor Details
#initialize(**args) ⇒ ImmutableObject
Returns a new instance of ImmutableObject.
23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/navigable/immutable_object.rb', line 23 def initialize(**args) absent_attributes = args.keys - self.class.attribute_names if absent_attributes.any? raise ArgumentError, "Unknown attribute #{absent_attributes}" end args.each do |name, value| instance_variable_set("@#{name}", value) end freeze end |
Class Attribute Details
.attribute_names ⇒ Object (readonly)
Returns the value of attribute attribute_names.
8 9 10 |
# File 'lib/navigable/immutable_object.rb', line 8 def attribute_names @attribute_names end |
Class Method Details
.attribute(name) ⇒ Object
10 11 12 13 |
# File 'lib/navigable/immutable_object.rb', line 10 def attribute(name) @attribute_names << name attr_reader(name) end |
Instance Method Details
#attributes ⇒ Object
37 38 39 |
# File 'lib/navigable/immutable_object.rb', line 37 def attributes self.class.attribute_names.to_h { |name| [name, public_send(name)] } end |