Module: Stannum::Entities::Constraints::AttributesMethods

Defined in:
lib/stannum/entities/constraints.rb

Overview

Class methods to extend the class when including Attributes.

Instance Method Summary collapse

Instance Method Details

#attribute(attr_name, attr_type, **options) ⇒ Object

Defines an attribute on the entity.

Delegates to the superclass method, and then adds a type constraint to ::Contract.

See Also:

  • Attributes::ClassMethods#attribute.


16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/stannum/entities/constraints.rb', line 16

def attribute(attr_name, attr_type, **options) # rubocop:disable Metrics/MethodLength
  returned = super

  attribute  = attributes[attr_name.to_s]
  constraint = Stannum::Constraints::Type.new(
    attribute.type,
    required: attribute.required?
  )

  self::Contract.add_constraint(
    constraint,
    property: attribute.reader_name
  )

  returned
end