Class: Navigable::ImmutableObject

Inherits:
Object
  • Object
show all
Defined in:
lib/navigable/immutable_object.rb

Direct Known Subclasses

Input

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_namesObject (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

#attributesObject



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