Class: Waw::Validation::Signature
- Defined in:
- lib/waw/validation/signature.rb
Overview
A service or data signature, as a list of validations to be respected.
Defined Under Namespace
Classes: DSL, ValidationRule
Instance Attribute Summary collapse
-
#rules ⇒ Object
readonly
Rules.
Instance Method Summary collapse
-
#add_validation(args, validator, onfailure) ⇒ Object
Adds a validation rule.
-
#allows?(hash = {}) ⇒ Boolean
Checks if the signature allows passing with some values.
-
#apply(hash) ⇒ Object
Validates argument values given through a hash and returns a series of onfailure flags.
-
#blocks?(hash = {}) ⇒ Boolean
Checks if the signature blocks with some values.
-
#default ⇒ Object
Computed default data for this signature.
-
#dup ⇒ Object
Duplicates this validation.
-
#first_converted(hash = {}) ⇒ Object
Calls first_converted(hash) on the first rule.
-
#initialize(&block) ⇒ Signature
constructor
Creates an empty signature.
-
#merge(&block) ⇒ Object
Merges this signature with additional definition.
-
#validate(hash) ⇒ Object
Validates argument values given through a hash and returns a series of onfailure flags.
Constructor Details
Instance Attribute Details
#rules ⇒ Object
Rules
85 86 87 |
# File 'lib/waw/validation/signature.rb', line 85 def rules @rules end |
Instance Method Details
#add_validation(args, validator, onfailure) ⇒ Object
Adds a validation rule
101 102 103 104 |
# File 'lib/waw/validation/signature.rb', line 101 def add_validation(args, validator, onfailure) args = [args] if Symbol===args @rules << ValidationRule.new(args, validator, onfailure) end |
#allows?(hash = {}) ⇒ Boolean
Checks if the signature allows passing with some values
131 132 133 |
# File 'lib/waw/validation/signature.rb', line 131 def allows?(hash={}) apply(hash)[0] end |
#apply(hash) ⇒ Object
Validates argument values given through a hash and returns a series of onfailure flags.
116 117 118 119 120 121 |
# File 'lib/waw/validation/signature.rb', line 116 def apply(hash) raise ArgumentError, "Hash expected (did you invoked an action without a Hash?)" unless Hash===hash converted, failures = hash.dup, [] failures = @rules.collect{|rule| rule.convert_and_validate(converted)}.compact failures.empty? ? [true, converted] : [false, failures] end |
#blocks?(hash = {}) ⇒ Boolean
Checks if the signature blocks with some values
136 137 138 |
# File 'lib/waw/validation/signature.rb', line 136 def blocks?(hash={}) not(allows?(hash)) end |
#default ⇒ Object
Computed default data for this signature
141 142 143 144 145 |
# File 'lib/waw/validation/signature.rb', line 141 def default defaults = {} @rules.each {|rule| rule.complete_defaults(defaults)} defaults end |
#dup ⇒ Object
Duplicates this validation
148 149 150 151 152 |
# File 'lib/waw/validation/signature.rb', line 148 def dup copy = Signature.new copy.rules = self.rules.dup copy end |
#first_converted(hash = {}) ⇒ Object
Calls first_converted(hash) on the first rule. Returns nil if no rule has been installed. The hash is never modified by this method.
125 126 127 128 |
# File 'lib/waw/validation/signature.rb', line 125 def first_converted(hash = {}) return nil if @rules.nil? or @rules.empty? @rules[0].first_converted(hash) end |
#merge(&block) ⇒ Object
Merges this signature with additional definition
94 95 96 97 98 |
# File 'lib/waw/validation/signature.rb', line 94 def merge(&block) signature = self.dup DSL.new(signature).execute(&block) unless block.nil? signature end |
#validate(hash) ⇒ Object
Validates argument values given through a hash and returns a series of onfailure flags.
108 109 110 111 112 |
# File 'lib/waw/validation/signature.rb', line 108 def validate(hash) converted, failures = hash.dup, [] failures = @rules.collect{|rule| rule.validate(converted)}.compact failures.empty? ? [true, converted] : [false, failures] end |