Class: EcsRails::Registry::Declaration

Inherits:
Object
  • Object
show all
Defined in:
lib/ecs_rails/registry.rb

Overview

One component Foo declaration on one entity class.

A value object over names. #entity_class / #component_class resolve on every call, so a Declaration handed out before a reload still resolves to the post-reload constants.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entity_class_name:, component_class_name:, options: {}) ⇒ Declaration

Returns a new instance of Declaration.

Parameters:

  • entity_class_name (String)

    the declaring entity’s class name

  • component_class_name (String)

    the declared component’s class name

  • options (Hash) (defaults to: {})

    only:/except: delegation options



40
41
42
43
44
45
# File 'lib/ecs_rails/registry.rb', line 40

def initialize(entity_class_name:, component_class_name:, options: {})
  @entity_class_name = entity_class_name
  @component_class_name = component_class_name
  @options = options.dup.freeze
  freeze
end

Instance Attribute Details

#component_class_nameString (readonly)

Returns the declared component’s class name.

Returns:

  • (String)

    the declared component’s class name



35
# File 'lib/ecs_rails/registry.rb', line 35

attr_reader :entity_class_name, :component_class_name, :options

#entity_class_nameString (readonly)

Returns the declaring entity’s class name.

Returns:

  • (String)

    the declaring entity’s class name



35
36
37
# File 'lib/ecs_rails/registry.rb', line 35

def entity_class_name
  @entity_class_name
end

#optionsObject (readonly)

Returns the value of attribute options.



35
# File 'lib/ecs_rails/registry.rb', line 35

attr_reader :entity_class_name, :component_class_name, :options

Instance Method Details

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

Returns true if both declare the same component on the same entity with the same options.

Parameters:

  • other (Object)

Returns:

  • (Boolean)

    true if both declare the same component on the same entity with the same options



66
67
68
69
70
71
# File 'lib/ecs_rails/registry.rb', line 66

def ==(other)
  other.is_a?(Declaration) &&
    entity_class_name == other.entity_class_name &&
    component_class_name == other.component_class_name &&
    options == other.options
end

#component_classClass<EcsRails::Component>

Resolved live, so a reloaded constant is picked up.

Returns:

Raises:

  • (NameError)

    if the constant has gone away



59
60
61
# File 'lib/ecs_rails/registry.rb', line 59

def component_class
  component_class_name.constantize
end

#entity_classClass<EcsRails::Entity>

Resolved live, so a reloaded constant is picked up.

Returns:

Raises:



51
52
53
# File 'lib/ecs_rails/registry.rb', line 51

def entity_class
  entity_class_name.constantize
end

#hashInteger

Returns a hash consistent with #==, so Declarations work as Hash keys and in Sets.

Returns:

  • (Integer)

    a hash consistent with #==, so Declarations work as Hash keys and in Sets



76
77
78
# File 'lib/ecs_rails/registry.rb', line 76

def hash
  [self.class, entity_class_name, component_class_name, options].hash
end

#inspectString

Returns e.g. #<...Declaration User => Email {}>.

Returns:

  • (String)

    e.g. #<...Declaration User => Email {}>



81
82
83
# File 'lib/ecs_rails/registry.rb', line 81

def inspect
  "#<#{self.class} #{entity_class_name} => #{component_class_name} #{options.inspect}>"
end