class Upgrow::ImmutableObject

A read-only Object. An Immutable Object is initialized with its attributes and subsequent state changes are not permitted.

Attributes

schema[RW]
attributes[R]

Public Class Methods

attribute(name) click to toggle source

Defines an attribute in the Immutable Object Schema.

@param name [Symbol] the name of the attribute.

# File lib/upgrow/immutable_object.rb, line 17
def attribute(name)
  schema.attribute(name)
end
new(**attributes) click to toggle source

Initializes a new Immutable Object with the given member values.

@param attributes [Hash<Symbol, Object>] the list of values for each

attribute of the Immutable Object.

@raise [ArgumentError] if the given argument is not an attribute.

# File lib/upgrow/immutable_object.rb, line 37
def initialize(**attributes)
  absent_attributes = attributes.keys - self.class.schema.attribute_names

  if absent_attributes.any?
    raise ArgumentError, "Unknown attribute #{absent_attributes}"
  end

  @attributes = self.class.schema.attribute_names.to_h do |name|
    [name, attributes[name]]
  end.freeze

  freeze
end

Private Class Methods

inherited(subclass) click to toggle source
Calls superclass method
# File lib/upgrow/immutable_object.rb, line 23
def inherited(subclass)
  super
  subclass.schema = @schema.dup
end

Private Instance Methods

method_missing(name, *args, &block) click to toggle source
Calls superclass method
# File lib/upgrow/immutable_object.rb, line 53
def method_missing(name, *args, &block)
  super unless attributes.include?(name)
  attributes.fetch(name)
end
respond_to_missing?(name, _include_private = false) click to toggle source
Calls superclass method
# File lib/upgrow/immutable_object.rb, line 58
def respond_to_missing?(name, _include_private = false)
  attributes.include?(name) || super
end