Class: SolidObjects::StateDefinition

Inherits:
Object
  • Object
show all
Defined in:
lib/solid_objects/state.rb,
sig/generated/lib/solid_objects/state.rbs

Defined Under Namespace

Classes: Attribute

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStateDefinition

Returns a new instance of StateDefinition.

RBS:

  • () -> void



10
11
12
# File 'lib/solid_objects/state.rb', line 10

def initialize
  @attributes = {}
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.

Returns:

  • (Object)


36
37
38
# File 'lib/solid_objects/state.rb', line 36

def attributes
  @attributes
end

Instance Method Details

#attribute(name, default: nil) ⇒ Attribute

RBS:

  • (Symbol | String, ?default: untyped) -> Attribute

Parameters:

  • (Symbol, String)
  • default: (Object) (defaults to: nil)

Returns:



15
16
17
18
19
20
# File 'lib/solid_objects/state.rb', line 15

def attribute(name, default: nil)
  attribute_name = name.to_sym
  raise InvalidActor, "state attribute #{attribute_name.inspect} is already defined" if attributes.key?(attribute_name)

  attributes[attribute_name] = Attribute.new(name: attribute_name, default:)
end

#duplicateStateDefinition

RBS:

  • () -> StateDefinition

Returns:



28
29
30
31
32
# File 'lib/solid_objects/state.rb', line 28

def duplicate
  self.class.new.tap do |definition|
    attributes.each_value { |attribute| definition.attribute(attribute.name, default: attribute.default) }
  end
end

#to_hHash[Symbol, Attribute]

RBS:

  • () -> Hash[Symbol, Attribute]

Returns:



23
24
25
# File 'lib/solid_objects/state.rb', line 23

def to_h
  attributes.dup.freeze
end