Module: Strict::Value

Includes:
_AttributesInstance
Defined in:
lib/strict/value.rb,
sig/strict.rbs

Class Method Summary collapse

Instance Method Summary collapse

Methods included from _AttributesInstance

#inspect, #pretty_print, #to_h

Class Method Details

.included(mod) ⇒ Object



5
6
7
# File 'lib/strict/value.rb', line 5

def self.included(mod)
  Attributes::GeneratedMethods.install_on(mod, writable: false)
end

Instance Method Details

#deconstruct_keys(keys) ⇒ Hash[Symbol, untyped]

Parameters:

  • keys (Array[Symbol], nil)

Returns:

  • (Hash[Symbol, untyped])


13
14
15
16
# File 'lib/strict/value.rb', line 13

def deconstruct_keys(keys)
  attributes = Attributes::Instance.attribute_values(self)
  keys ? attributes.slice(*keys) : attributes
end

#eql?(other) ⇒ Boolean Also known as: ==

Parameters:

Returns:

  • (Boolean)


18
19
20
21
22
23
24
# File 'lib/strict/value.rb', line 18

def eql?(other)
  return false unless self.class.equal?(other.class)

  self.class.strict_attributes.all? do |attribute|
    public_send(attribute.name).eql?(other.public_send(attribute.name))
  end
end

#hashInteger

Returns:

  • (Integer)


27
28
29
30
31
32
33
34
# File 'lib/strict/value.rb', line 27

def hash
  value_class = self.class
  components = [value_class]
  value_class.strict_attributes.each do |attribute|
    components << public_send(attribute.name)
  end
  components.hash
end

#with(**attributes) ⇒ self

Parameters:

Returns:

  • (self)


9
10
11
# File 'lib/strict/value.rb', line 9

def with(**attributes)
  self.class.new(**Attributes::Instance.attribute_values(self), **attributes)
end