Class: Hyraft::Port

Inherits:
Object
  • Object
show all
Defined in:
lib/hyraft/circuit/port.rb

Overview

Port is the base class for all ports in Hexagonal Architecture. It provides contract validation and method introspection for ports.

Examples:

Define an input port

class ArticlesInput < Hyraft::Port
  def create(title, content) = raise NotImplementedError
  def get(id) = raise NotImplementedError
  def list = raise NotImplementedError
end

Define an output port

class ArticlesOutput < Hyraft::Port
  def save(article) = raise NotImplementedError
  def find(id) = raise NotImplementedError
  def all = raise NotImplementedError
  def delete(id) = raise NotImplementedError
end

Validate implementation

class CreateArticle < ArticlesInput
  def create(title, content); end
  def get(id); end
  def list; end
end

ArticlesInput.validate_implementation(CreateArticle) # => true

Direct Known Subclasses

Circuit::Port

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.allArray<Class>

Get all defined ports

Returns:

  • (Array<Class>)

    List of all port classes



47
48
49
# File 'lib/hyraft/circuit/port.rb', line 47

def all
  @ports ||= []
end

.inherited(klass) ⇒ Array

Track all ports that inherit from this class

Parameters:

  • klass (Class)

    The class that inherits from Port

Returns:

  • (Array)

    Updated list of all ports



39
40
41
42
# File 'lib/hyraft/circuit/port.rb', line 39

def inherited(klass)
  @ports ||= []
  @ports << klass
end

.validate_implementation(klass) ⇒ Boolean

Validate that a class implements all port methods

Examples:

class CreateArticle < ArticlesInput
  def create(title, content); end
  def get(id); end
  def list; end
end

ArticlesInput.validate_implementation(CreateArticle) # => true

Parameters:

  • klass (Class)

    The class to validate

Returns:

  • (Boolean)

    true if all methods are implemented

Raises:

  • (RuntimeError)

    if methods are missing



90
91
92
93
94
95
96
# File 'lib/hyraft/circuit/port.rb', line 90

def self.validate_implementation(klass)
  missing = instance_methods(false) - klass.instance_methods(false)
  if missing.any?
    raise "Class #{klass} must implement: #{missing.join(', ')}"
  end
  true
end

Instance Method Details

#implements?(method_name) ⇒ Boolean

Check if a method is implemented in the port

Examples:

port = ArticlesInput.new
port.implements?(:create)  # => true
port.implements?(:delete)  # => false

Parameters:

  • method_name (Symbol)

    The method name to check

Returns:

  • (Boolean)

    true if method exists, false otherwise



61
62
63
# File 'lib/hyraft/circuit/port.rb', line 61

def implements?(method_name)
  self.class.instance_methods(false).include?(method_name)
end

#inspectString

Inspect port with its required methods

Examples:

port.inspect  # => "#<ArticlesInput methods=[:create, :get, :list]>"

Returns:

  • (String)

    Human-readable representation



114
# File 'lib/hyraft/circuit/port.rb', line 114

def inspect = "#<#{self.class.name} methods=#{required_methods}>"

#required_methodsArray<Symbol>

Get all required methods from this port

Examples:

port = ArticlesInput.new
port.required_methods  # => [:create, :get, :list]

Returns:

  • (Array<Symbol>)

    List of method names defined in the port



72
73
74
# File 'lib/hyraft/circuit/port.rb', line 72

def required_methods
  self.class.instance_methods(false)
end

#validate!Boolean

Ruby 4 - Pattern matching for port validation

Returns:

  • (Boolean)

    true if valid



101
102
103
104
105
106
# File 'lib/hyraft/circuit/port.rb', line 101

def validate!
  case self
  in {required_methods: []} then true
  else true
  end
end