Class: SimpleParams::ValidationBuilder
- Inherits:
-
Object
- Object
- SimpleParams::ValidationBuilder
- Defined in:
- lib/simple_params/validation_builder.rb
Instance Method Summary collapse
-
#initialize(name, opts = {}) ⇒ ValidationBuilder
constructor
A new instance of ValidationBuilder.
- #validation_string ⇒ Object
Constructor Details
#initialize(name, opts = {}) ⇒ ValidationBuilder
Returns a new instance of ValidationBuilder.
3 4 5 6 |
# File 'lib/simple_params/validation_builder.rb', line 3 def initialize(name, opts={}) @name = name @opts = opts end |
Instance Method Details
#validation_string ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/simple_params/validation_builder.rb', line 8 def validation_string validations = @opts[:validations] || {} has_default = @opts.has_key?(:default) # checking has_key? because :default may be nil optional = @opts[:optional] if !validations.empty? if optional || has_default validations.merge!(allow_nil: true) else validations.merge!(presence: true) end else if !optional && !has_default validations.merge!(presence: true) end end if validations.empty? return '' else return "validates :#{@name.to_sym}, #{validations}" end end |