Class: Plumb::Policy

Inherits:
Object
  • Object
show all
Includes:
Composable
Defined in:
lib/plumb/policy.rb

Overview

Wrap a policy composition ("step") in a Policy object. So that visitors such as JSONSchema and Metadata visitors can define dedicated handlers for policies, if they need to.

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Composable

#&, #/, #>>, #[], #absorb_input, #absorb_output, #accepted_type, #as_node, #build, #check, #defer, #fusable_step?, #fuse_with, #generate, #idempotent?, included, #invalid, #invoke, #match, #metadata, #not, #pipeline, #policy, resolve_operand, #static, #subtype_identity, #to_json_schema, #to_mermaid, #to_plumb_type, #to_s, #transform, #value, #value_preserving?, #where, #with, wrap, #|

Methods included from Callable

#parse, #resolve

Constructor Details

#initialize(policy_name, arg, step) ⇒ Policy

Returns a new instance of Policy.



20
21
22
23
24
25
26
# File 'lib/plumb/policy.rb', line 20

def initialize(policy_name, arg, step)
  @policy_name = policy_name
  @arg = arg
  @step = step
  @children = [step].freeze
  freeze
end

Instance Attribute Details

#argObject (readonly)

Returns the value of attribute arg.



12
13
14
# File 'lib/plumb/policy.rb', line 12

def arg
  @arg
end

#childrenObject (readonly)

Returns the value of attribute children.



12
13
14
# File 'lib/plumb/policy.rb', line 12

def children
  @children
end

#policy_nameObject (readonly)

Returns the value of attribute policy_name.



12
13
14
# File 'lib/plumb/policy.rb', line 12

def policy_name
  @policy_name
end

Instance Method Details

#==(other) ⇒ Boolean

Policy equality includes the wrapped step; otherwise same-named policies on disjoint types would compare equal and one could be reduced away.

Parameters:

  • other (Object)

Returns:

  • (Boolean)


32
33
34
35
36
37
# File 'lib/plumb/policy.rb', line 32

def ==(other)
  other.instance_of?(self.class) &&
    policy_name == other.policy_name &&
    arg == other.arg &&
    children == other.children
end

#call(result) ⇒ Result

The standard Step interface.

Parameters:

Returns:



47
# File 'lib/plumb/policy.rb', line 47

def call(result) = @step.call(result)

#input_typeObject

A Policy is a transparent wrapper: it delegates type-flow to the wrapped step.



41
# File 'lib/plumb/policy.rb', line 41

def input_type = @step.input_type

#output_typeObject



42
# File 'lib/plumb/policy.rb', line 42

def output_type = @step.output_type

#with_children(children) ⇒ Object

Parameters:

  • policy_name (Symbol)
  • arg (Object, nil)

    the argument to the policy, if any.

  • step (Step)

    the step composition wrapped by this policy.

See Also:



18
# File 'lib/plumb/policy.rb', line 18

def with_children(children) = self.class.new(policy_name, arg, children.first)