Class: Wayfinding::Kind

Inherits:
Object
  • Object
show all
Defined in:
lib/wayfinding/kind.rb

Overview

A named field contract. Applications define kinds; Wayfinding only enforces them.

Wayfinding.define_kind(:report, requires: %i[label description action subject])

requires may name any field, whether or not Wayfinding gives it behavior. Validation asserts presence only and never resolves callables, so a lazily registered subject: -> { ProjectTask } passes without autoloading.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name:, requires: []) ⇒ Kind

Returns a new instance of Kind.



14
15
16
17
18
# File 'lib/wayfinding/kind.rb', line 14

def initialize(name:, requires: [])
  @name = name.to_sym
  @requires = Array(requires).map(&:to_sym).freeze
  freeze
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



12
13
14
# File 'lib/wayfinding/kind.rb', line 12

def name
  @name
end

#requiresObject (readonly)

Returns the value of attribute requires.



12
13
14
# File 'lib/wayfinding/kind.rb', line 12

def requires
  @requires
end

Instance Method Details

#validate!(destination_name, given) ⇒ Object

Raises:



20
21
22
23
24
25
26
27
# File 'lib/wayfinding/kind.rb', line 20

def validate!(destination_name, given)
  missing = requires.reject { |field| given.key?(field) && !given[field].nil? }
  return if missing.empty?

  raise InvalidDestination,
        "Destination #{destination_name.inspect} of kind #{name.inspect} " \
        "is missing required #{missing.length == 1 ? 'field' : 'fields'}: #{missing.join(', ')}"
end