Class: Pvectl::Models::Base Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/pvectl/models/base.rb

Overview

This class is abstract.

Subclass and add attr_readers for domain attributes.

Abstract base class for domain models.

Provides common initialization from hash attributes. All models are immutable - only getters, no setters.

Subclasses store domain data and contain domain logic, but do not fetch data themselves (that’s the Repository’s job).

Examples:

Implementing a model

class Vm < Base
  attr_reader :vmid, :name, :status

  def initialize(attributes = {})
    super
    @vmid = attributes[:vmid]
    @name = attributes[:name]
    @status = attributes[:status]
  end

  def running?
    status == "running"
  end
end

See Also:

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Creates model from attributes hash.

Converts string keys to symbols for consistent access.

Parameters:

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

    attribute key-value pairs



39
40
41
# File 'lib/pvectl/models/base.rb', line 39

def initialize(attributes = {})
  @attributes = (attributes || {}).transform_keys(&:to_sym)
end