Class: Validate::BlockParsingContext
- Inherits:
-
Object
- Object
- Validate::BlockParsingContext
- Defined in:
- lib/validate/parser.rb
Instance Method Summary collapse
-
#allow_keys(keys) ⇒ Object
‘allow_keys` is another special case, which can be used like this:.
-
#initialize ⇒ BlockParsingContext
constructor
A new instance of BlockParsingContext.
- #method_missing(method, *args, &block) ⇒ Object
-
#run_when(condition, &block) ⇒ Object
‘when` is a special case, its syntax is as follows:.
-
#validations ⇒ Object
Returns a CompiledValidations struct.
Constructor Details
#initialize ⇒ BlockParsingContext
Returns a new instance of BlockParsingContext.
37 38 39 40 |
# File 'lib/validate/parser.rb', line 37 def initialize @allow_keys = :any @validations = [] end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &block) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/validate/parser.rb', line 48 def method_missing(method, *args, &block) raise NoMethodError.new("No method #{method} to call in the context of a validation block.") unless method.to_s =~ /^validates/ raise NoMethodError.new("Undefined validation method: #{method}...") unless ValidationMethods.new.respond_to?(method) opts = args.pop if args.last.is_a?(::Hash) children = if block Parser.parse(&block) end @validations << { name: method, fields: args, opts: opts, validations: children } end |
Instance Method Details
#allow_keys(keys) ⇒ Object
‘allow_keys` is another special case, which can be used like this:
allow_keys :any
allow_keys :valid
allow_keys %w(one two three)
Defaults to :any, if not specified.
87 88 89 |
# File 'lib/validate/parser.rb', line 87 def allow_keys(keys) @allow_keys = keys end |
#run_when(condition, &block) ⇒ Object
‘when` is a special case, its syntax is as follows:
run_when -> { ... } do
# validations go here
end
69 70 71 72 73 74 75 76 77 |
# File 'lib/validate/parser.rb', line 69 def run_when(condition, &block) validations = Parser.parse(&block).validations validations.map do |v| v[:opts] ||= {} v[:opts][:when] = condition v end @validations += validations end |
#validations ⇒ Object
Returns a CompiledValidations struct.
44 45 46 |
# File 'lib/validate/parser.rb', line 44 def validations CompiledValidations.new(@validations, @allow_keys) end |