Class: WorkflowTemplate::Definer::HasValidations::Validations::Prepared

Inherits:
Object
  • Object
show all
Defined in:
lib/workflow_template/definer/has_validations/validations.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(declarations, input, output) ⇒ Prepared

Returns a new instance of Prepared.



101
102
103
104
105
106
# File 'lib/workflow_template/definer/has_validations/validations.rb', line 101

def initialize(declarations, input, output)
  @declarations = declarations.freeze
  @input = input.freeze
  @output = output.freeze
  freeze
end

Instance Attribute Details

#declarationsObject (readonly)

Returns the value of attribute declarations.



85
86
87
# File 'lib/workflow_template/definer/has_validations/validations.rb', line 85

def declarations
  @declarations
end

#inputObject (readonly)

Returns the value of attribute input.



85
86
87
# File 'lib/workflow_template/definer/has_validations/validations.rb', line 85

def input
  @input
end

#outputObject (readonly)

Returns the value of attribute output.



85
86
87
# File 'lib/workflow_template/definer/has_validations/validations.rb', line 85

def output
  @output
end

Class Method Details

.instance(declarations:, input:, output:) ⇒ Object



87
88
89
# File 'lib/workflow_template/definer/has_validations/validations.rb', line 87

def self.instance(declarations:, input:, output:)
  new(declarations, normalize_entry(input), normalize_entry(output))
end

.normalize_entry(entry) ⇒ Object



91
92
93
94
95
96
97
# File 'lib/workflow_template/definer/has_validations/validations.rb', line 91

def self.normalize_entry(entry)
  case entry
  when nil then nil
  when Array, Set then entry.to_a.map(&:to_sym)
  else entry.to_sym
  end
end

Instance Method Details

#merge(other) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
# File 'lib/workflow_template/definer/has_validations/validations.rb', line 108

def merge(other)
  merged_validations = declarations.merge other.declarations
  merged_input = Validations.merge_entries(input, other.send(:input_set))
  merged_output = Validations.merge_entries(output, other.send(:output_set))

  Prepared.instance(
    declarations: merged_validations,
    input: merged_input,
    output: merged_output
  )
end

#resolve_validation(name) ⇒ Object

Raises:



128
129
130
131
132
133
# File 'lib/workflow_template/definer/has_validations/validations.rb', line 128

def resolve_validation(name)
  name = name.to_sym
  raise Error, "Validation not declared: #{name}" unless declarations.key? name

  declarations[name]
end

#resolve_validations(option) ⇒ Object



120
121
122
123
124
125
126
# File 'lib/workflow_template/definer/has_validations/validations.rb', line 120

def resolve_validations(option)
  case option
  when nil then nil
  when Array then option.map { |element| resolve_validation(element) }.freeze
  else resolve_validation(option).freeze
  end
end

#resolvedObject



135
136
137
# File 'lib/workflow_template/definer/has_validations/validations.rb', line 135

def resolved
  Resolved.new(self)
end