Class: Service::Result

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::API, ActiveModel::Attributes
Defined in:
app/concerns/service.rb

Overview

ActiveSupport::Concern mixin: result.

Value object returned by services. Carries the service output plus
error details and HTTP-cache metadata (last_modified, etag).

Instance Method Summary collapse

Instance Method Details

#[](key) ⇒ Object?

Fetches a single attribute by name (Hash compatibility).

Parameters:

  • key (Symbol, String)

    attribute name

Returns:

  • (Object, nil)

    the attribute value, or nil when absent



72
73
74
# File 'app/concerns/service.rb', line 72

def [](key)
  to_hash[key.to_sym]
end

#to_hashHash{Symbol => Object} Also known as: to_h

Hash compatibility — the old Virtus struct responded to [] and to_hash.
Callers that do result[:output], result.to_hash, or **result still work.
:params is excluded because it holds the internal attributes_used snapshot
and was never part of the public contract.

Returns:

  • (Hash{Symbol => Object})

    public attributes with symbol keys



63
64
65
# File 'app/concerns/service.rb', line 63

def to_hash
  attributes.transform_keys(&:to_sym).except(:params)
end

#to_hash_with_paramsHash{Symbol => Object}

Full attribute hash, including the internal :params snapshot.

Returns:

  • (Hash{Symbol => Object})

    all attributes with symbol keys



79
80
81
# File 'app/concerns/service.rb', line 79

def to_hash_with_params
  attributes.transform_keys(&:to_sym)
end