Class: WorkflowTemplate::Validation::Builder::Proxy

Inherits:
Object
  • Object
show all
Defined in:
lib/workflow_template/validation/builder/proxy.rb

Defined Under Namespace

Classes: Keyed

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(adapter, collection, name, context) ⇒ Proxy

Returns a new instance of Proxy.



32
33
34
35
36
37
38
# File 'lib/workflow_template/validation/builder/proxy.rb', line 32

def initialize(adapter, collection, name, context)
  @adapter = adapter
  @collection = collection
  @name = name&.to_sym
  @context = context.freeze
  freeze
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



40
41
42
# File 'lib/workflow_template/validation/builder/proxy.rb', line 40

def context
  @context
end

#nameObject (readonly)

Returns the value of attribute name.



40
41
42
# File 'lib/workflow_template/validation/builder/proxy.rb', line 40

def name
  @name
end

Class Method Details

.instance(adapter, collection, name: nil, context: nil) ⇒ Object



10
11
12
# File 'lib/workflow_template/validation/builder/proxy.rb', line 10

def self.instance(adapter, collection, name: nil, context: nil)
  new adapter, collection, normalize_name(name), normalize_context(context)
end

.normalize_context(context) ⇒ Object

Raises:



22
23
24
25
26
27
28
# File 'lib/workflow_template/validation/builder/proxy.rb', line 22

def self.normalize_context(context)
  return if context.nil? || context.empty?

  raise Error, "Context contains invalid keys: #{context}" if context.any?(&:empty?)

  context.map(&:to_sym)
end

.normalize_name(name) ⇒ Object

Raises:



14
15
16
17
18
19
20
# File 'lib/workflow_template/validation/builder/proxy.rb', line 14

def self.normalize_name(name)
  return if name.nil?

  raise Error, 'Empty name' if name.empty?

  name.to_sym
end

Instance Method Details

#declare(validation) ⇒ Object



77
78
79
# File 'lib/workflow_template/validation/builder/proxy.rb', line 77

def declare(validation)
  collection.add(evaluation(validation))
end

#keyObject



46
47
48
# File 'lib/workflow_template/validation/builder/proxy.rb', line 46

def key
  nil
end

#key?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/workflow_template/validation/builder/proxy.rb', line 42

def key?
  false
end

#named(name) ⇒ Object

Raises:



50
51
52
53
54
# File 'lib/workflow_template/validation/builder/proxy.rb', line 50

def named(name)
  raise Error, "Name already defined: #{self.name}" if self.name

  self.class.instance(adapter, collection, name: name, context: context)
end

#validate(key = nil, **opts, &block) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/workflow_template/validation/builder/proxy.rb', line 62

def validate(key = nil, **opts, &block)
  proxy = if key
    Keyed.instance(key, adapter, collection, name: name, context: context)
  else
    raise Error, 'Key must be supplied if name is missing' if name.nil?
    raise Error, 'Context is not available if key is not specified' unless context.nil?

    self
  end

  adapter.builder_class
         .new(proxy)
         .validate(**opts, &block)
end

#with_context(*context) ⇒ Object

Raises:



56
57
58
59
60
# File 'lib/workflow_template/validation/builder/proxy.rb', line 56

def with_context(*context)
  raise Error, "Context already defined: #{self.context}" if self.context

  self.class.instance(adapter, collection, name: name, context: context)
end