Module: Serviced

Defined in:
lib/serviced.rb,
lib/serviced/flow.rb,
lib/serviced/query.rb,
lib/serviced/typed.rb,
lib/serviced/errors.rb,
lib/serviced/result.rb,
lib/serviced/service.rb,
lib/serviced/version.rb,
lib/serviced/configuration.rb,
lib/serviced/result_helpers.rb

Overview

Serviced provides small, explicit service objects: typed and immutable inputs, a mandatory Success/Failure return value, and composable flows with optional transactions.

See Service and Flow.

Defined Under Namespace

Modules: ResultHelpers, Typed Classes: Configuration, Error, Failure, Flow, InvalidQuery, InvalidResultAccess, MissingTransactionHandler, Query, Result, ResultTypeError, Rollback, Service, Success

Constant Summary collapse

VERSION =
"0.1.1"

Class Method Summary collapse

Class Method Details

.configurationServiced::Configuration

Returns the current configuration.

Returns:



23
24
25
# File 'lib/serviced.rb', line 23

def configuration
  @configuration ||= Configuration.new
end

.configure {|config| ... } ⇒ Object

Yields the configuration for mutation.

Yield Parameters:



29
30
31
# File 'lib/serviced.rb', line 29

def configure
  yield(configuration)
end

.error_summary(errors) ⇒ String

Renders an ActiveModel::Errors into a short human string. Falls back to the attribute names when full messages cannot be built: an anonymous class has no model_name, which ActiveModel needs to humanize messages. Building an error message must never itself raise.

Parameters:

  • errors (ActiveModel::Errors)

Returns:

  • (String)


45
46
47
48
49
# File 'lib/serviced.rb', line 45

def error_summary(errors)
  errors.full_messages.join(", ")
rescue StandardError
  errors.attribute_names.join(", ")
end

.reset_configuration!Serviced::Configuration

Resets configuration to defaults. Primarily useful in test suites.



35
36
37
# File 'lib/serviced.rb', line 35

def reset_configuration!
  @configuration = Configuration.new
end

.snapshot(value) ⇒ Object

Returns an immutable snapshot of a value-like input. Arrays, hashes, sets and strings are deep-copied and frozen, so the result is isolated from the caller and cannot be mutated. Objects with identity (ActiveRecord records and other non-data objects) are returned by reference, unfrozen: a deep copy of a record is a different, non-persisted object, and freezing one in place would corrupt the caller's copy. Scalars are already immutable.

Parameters:

  • value (Object)

Returns:

  • (Object)

    a frozen snapshot for value-like data, else the value itself



59
60
61
62
63
64
65
66
67
# File 'lib/serviced.rb', line 59

def snapshot(value)
  case value
  when Array then value.map { |element| snapshot(element) }.freeze
  when Set then Set.new(value.map { |element| snapshot(element) }).freeze
  when Hash then snapshot_hash(value)
  when String then snapshot_string(value)
  else value
  end
end