Module: Phronomy::WorkflowContext::ClassMethods

Defined in:
lib/phronomy/workflow_context.rb

Instance Method Summary collapse

Instance Method Details

#field(name, type: :replace, default: nil) ⇒ Object

Defines a context field.

Parameters:

  • name (Symbol)
  • type (Symbol) (defaults to: :replace)

    :replace / :append / :merge

  • default (Object, Proc, nil) (defaults to: nil)

Raises:

  • (ArgumentError)

    if +default+ is a plain Array or Hash (use a Proc instead)



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/phronomy/workflow_context.rb', line 36

def field(name, type: :replace, default: nil)
  if default.is_a?(Array) || default.is_a?(Hash)
    raise ArgumentError,
      "Mutable default for field #{name.inspect} must be wrapped in a Proc " \
      "to avoid shared state across instances. " \
      "Use `default: -> { #{default.inspect} }` instead."
  end

  @fields[name] = {type: type, default: default}

  # Define getter.
  attr_reader name

  # Define write-guarded setter.  Mutation from outside the EventLoop
  # dispatch thread raises WorkflowContextOwnershipError in EventLoop mode.
  define_method(:"#{name}=") do |value|
    _assert_write_permitted!
    instance_variable_set(:"@#{name}", value)
  end
end

#fieldsObject



57
58
59
# File 'lib/phronomy/workflow_context.rb', line 57

def fields
  @fields
end