Module: TinyValidations
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/tiny_validations.rb,
lib/tiny_validations/version.rb
Constant Summary collapse
- VERSION =
0.2
Instance Method Summary collapse
- #context=(key) ⇒ Object
- #validations_base(rule, condition = :if, &block) ⇒ Object
- #validations_when(key, &block) ⇒ Object
- #validations_when_not(key, &block) ⇒ Object
Instance Method Details
#context=(key) ⇒ Object
32 33 34 35 36 37 38 |
# File 'lib/tiny_validations.rb', line 32 def context=(key) if self.respond_to?(context_key(key)) self.validation_context = key else raise 'Context not found' end end |
#validations_base(rule, condition = :if, &block) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/tiny_validations.rb', line 16 def validations_base(rule, condition = :if, &block) if rule.is_a?(Array) keys = rule.map { |r| context_key(r) } else keys = Array(context_key(rule)) end = {} [condition] = -> { keys.include?(context_key(validation_context)) } instance_eval do keys.each { |k| attr_accessor k } () { |validations| yield validations } end end |
#validations_when(key, &block) ⇒ Object
8 9 10 |
# File 'lib/tiny_validations.rb', line 8 def validations_when(key , &block) validations_base(key, &block) end |
#validations_when_not(key, &block) ⇒ Object
12 13 14 |
# File 'lib/tiny_validations.rb', line 12 def validations_when_not(key, &block) validations_base(key, :unless, &block) end |