Module: Plumb

Defined in:
lib/plumb.rb,
lib/plumb/or.rb,
lib/plumb/and.rb,
lib/plumb/key.rb,
lib/plumb/not.rb,
lib/plumb/step.rb,
lib/plumb/build.rb,
lib/plumb/types.rb,
lib/plumb/policy.rb,
lib/plumb/result.rb,
lib/plumb/schema.rb,
lib/plumb/version.rb,
lib/plumb/deferred.rb,
lib/plumb/hash_map.rb,
lib/plumb/metadata.rb,
lib/plumb/pipeline.rb,
lib/plumb/policies.rb,
lib/plumb/any_class.rb,
lib/plumb/decorator.rb,
lib/plumb/transform.rb,
lib/plumb/attributes.rb,
lib/plumb/composable.rb,
lib/plumb/hash_class.rb,
lib/plumb/array_class.rb,
lib/plumb/match_class.rb,
lib/plumb/tagged_hash.rb,
lib/plumb/tuple_class.rb,
lib/plumb/value_class.rb,
lib/plumb/static_class.rb,
lib/plumb/stream_class.rb,
lib/plumb/type_registry.rb,
lib/plumb/interface_class.rb,
lib/plumb/metadata_visitor.rb,
lib/plumb/visitor_handlers.rb,
lib/plumb/json_schema_visitor.rb

Defined Under Namespace

Modules: Attributes, Callable, Composable, Equality, Naming, SplitPolicy, TypeRegistry, Types, VisitorHandlers Classes: And, AnyClass, ArrayClass, Build, Decorator, Deferred, HashClass, HashMap, InterfaceClass, JSONSchemaVisitor, Key, MatchClass, Metadata, MetadataVisitor, Not, Or, Pipeline, Policies, Policy, Result, Schema, StaticClass, Step, StreamClass, TaggedHash, Transform, TupleClass, UndefinedClass, ValueClass

Constant Summary collapse

VERSION =
'0.0.8'
ParseError =
Class.new(::TypeError)
Undefined =
UndefinedClass.new.freeze
BLANK_STRING =
''
BLANK_ARRAY =
[].freeze
BLANK_HASH =
{}.freeze
BLANK_RESULT =
Result.wrap(Undefined)
NOOP =
->(result) { result }

Class Method Summary collapse

Class Method Details

.decorate(type, &block) ⇒ Object



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

def self.decorate(type, &block)
  Decorator.call(type, &block)
end

.policiesObject



8
9
10
# File 'lib/plumb.rb', line 8

def self.policies
  @policies
end

.policy(name, opts = {}) {|Step, Object, &block| ... } ⇒ Object

Register a policy with the given name and block. Optionally define a method on the Composable method to call the policy. Example:

 Plumb.policy(:multiply_by, for_type: Integer, helper: true) do |step, factor, &block|
   step.transform(Integer) { |number| number * factor }
 end

type = Types::Integer.multiply_by(2)
type.parse(10) # => 20

Parameters:

  • name (Symbol)

    the name of the policy

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

    options for the policy

Yields:

  • (Step, Object, &block)

    the step (type), policy argument, and policy block, if any.



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/plumb.rb', line 25

def self.policy(name, opts = {}, &block)
  name = name.to_sym
  if opts.is_a?(Hash) && block_given?
    for_type = opts[:for_type] || Object
    helper = opts[:helper] || false
  elsif opts.respond_to?(:call) && opts.respond_to?(:for_type) && opts.respond_to?(:helper)
    for_type = opts.for_type
    helper = opts.helper
    block = opts.method(:call)
  else
    raise ArgumentError, 'Expected a block or a hash with :for_type and :helper keys'
  end

  policies.register(for_type, name, block)

  return self unless helper

  if Composable.instance_methods.include?(name)
    raise Policies::MethodAlreadyDefinedError, "Method #{name} is already defined on Composable"
  end

  Composable.define_method(name) do |arg = Undefined, &bl|
    if arg == Undefined
      policy(name, &bl)
    else
      policy(name, arg, &bl)
    end
  end

  self
end