Class: Hexx::Entities::Base

Inherits:
Part
  • Object
show all
Defined in:
lib/hexx-entities/base.rb

Overview

Class Base provides a common interface for entities, that have identity

Examples:

require "hexx-entities"

class Item < Hexx::Entities::Base

  # Defines attributes with their coercers
  attribute :foo, coerce: ->(value) { value.to_s }

  # Defines validation for the entity
  validate { invalid :blank_foo unless foo }

end # class Item

Class Attribute Summary collapse

Class Method Summary collapse

Methods inherited from Part

serialize

Class Attribute Details

.uuidsArray<String>

Returns The list of UUIDs that identify the entity.

Returns:

  • (Array<String>)

    The list of UUIDs that identify the entity



42
# File 'lib/hexx-entities/base.rb', line 42

attribute_reader :uuids

Class Method Details

.==(other) ⇒ Boolean

Checks the equality of the entity to another object

The entity is equal to any other entity that has at least one of its own uuids.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/hexx-entities/base.rb', line 53

def ==(other)
  return false unless other.is_a? Base
  (other.uuids & uuids).any?
end

.initialize(uuids: nil, **attributes) ⇒ Object



34
35
36
37
# File 'lib/hexx-entities/base.rb', line 34

def initialize(uuids: nil, **attributes)
  @uuids = UUIDs.build Array(uuids)
  super attributes
end

.new(attributes) ⇒ Hexx::Entities::Base

Creates the entity and initializes its attributes

Parameters:

  • attributes (Hash)

Returns:



# File 'lib/hexx-entities/base.rb', line 25