Class: Hyraft::Engine

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

Overview

Engine is the base class for all domain entities in Hyraft applications. It provides common functionality like ID management, timestamps, persistence state tracking, and hash conversion.

Examples:

Basic Usage

class Article < Hyraft::Engine
  attr_reader :title, :content

  def initialize(id: nil, title:, content:, created_at: nil, updated_at: nil)
    super(id: id, created_at: created_at, updated_at: updated_at)
    @title = title
    @content = content
  end
end

Creating a new record

article = Article.new(title: "Ruby 4", content: "Hexagonal Architecture")
article.new_record?  # => true
article.persisted?   # => false

Working with persisted records

saved = Article.new(id: 1, title: "Saved", content: "Content")
saved.persisted?     # => true
saved.to_hash        # => { id: 1, created_at: "...", updated_at: "..." }

Updating timestamp

article.touch        # Updates updated_at and returns self

Direct Known Subclasses

Circuit::Engine

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id: nil, created_at: nil, updated_at: nil) ⇒ Engine

Initialize a new Engine instance

Examples:

Creating a new record

Engine.new(id: nil, created_at: Time.now, updated_at: Time.now)

Loading existing record

Engine.new(id: 1, created_at: Time.parse("2024-01-01"), updated_at: Time.now)

Parameters:

  • id (Integer, nil) (defaults to: nil)

    The record identifier (nil for new records)

  • created_at (Time, nil) (defaults to: nil)

    Creation timestamp (defaults to current time)

  • updated_at (Time, nil) (defaults to: nil)

    Last update timestamp (defaults to current time)



53
54
55
56
57
# File 'lib/hyraft/circuit/origin.rb', line 53

def initialize(id: nil, created_at: nil, updated_at: nil)
  @id = id
  @created_at = created_at || Time.now
  @updated_at = updated_at || Time.now
end

Instance Attribute Details

#created_atTime (readonly)

Returns The timestamp when the record was created.

Returns:

  • (Time)

    The timestamp when the record was created



36
37
38
# File 'lib/hyraft/circuit/origin.rb', line 36

def created_at
  @created_at
end

#idInteger? (readonly)

Returns The unique identifier of the record.

Returns:

  • (Integer, nil)

    The unique identifier of the record



33
34
35
# File 'lib/hyraft/circuit/origin.rb', line 33

def id
  @id
end

#updated_atTime (readonly)

Returns The timestamp when the record was last updated.

Returns:

  • (Time)

    The timestamp when the record was last updated



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

def updated_at
  @updated_at
end

Instance Method Details

#new_record?Boolean

Check if the record is new and not yet persisted

Examples:

article = Article.new(title: "New")
article.new_record?  # => true

saved = Article.new(id: 1, title: "Saved", content: "Content")
saved.new_record?    # => false

Returns:

  • (Boolean)

    true if record has no ID, false otherwise



85
86
87
# File 'lib/hyraft/circuit/origin.rb', line 85

def new_record?
  @id.nil?
end

#persisted?Boolean

Check if the record has been persisted to the database

Examples:

article = Article.new(title: "New")
article.persisted?  # => false

saved = Article.new(id: 1, title: "Saved", content: "Content")
saved.persisted?    # => true

Returns:

  • (Boolean)

    true if record has an ID, false otherwise



70
71
72
# File 'lib/hyraft/circuit/origin.rb', line 70

def persisted?
  !@id.nil?
end

#to_hHash

Convert the Engine instance to a hash representation

Examples:

article.to_hash
# => {
#   id: 1,
#   created_at: "2024-01-15T10:30:00Z",
#   updated_at: "2024-01-15T10:30:00Z"
# }

Returns:

  • (Hash)

    Hash with id, created_at, and updated_at in ISO8601 format



115
116
117
118
119
120
121
# File 'lib/hyraft/circuit/origin.rb', line 115

def to_h
  {
    id: @id,
    created_at: @created_at.iso8601,
    updated_at: @updated_at.iso8601
  }
end

#touchself

Update the updated_at timestamp to the current time

Examples:

article = Article.find(1)
article.touch
article.updated_at  # => current time

Returns:

  • (self)

    Returns the instance for method chaining



98
99
100
101
# File 'lib/hyraft/circuit/origin.rb', line 98

def touch
  @updated_at = Time.now
  self
end