Module: Validy::InstanceMethods
- Defined in:
- lib/validy.rb
Instance Method Summary collapse
-
#add_error(args = {}) ⇒ FalseClass
“add_error” adds an error and set valid state to false.
-
#condition(method, error = nil, &block) ⇒ Object
“condition” evaluates either passed block or instance method represented in the instance.
-
#errors ⇒ Hash
“errors” returns errors hash.
-
#invalid? ⇒ Boolean
“invalid?” returns opposite value of inner valid state.
-
#optional(attribute) ⇒ Object
“optional” starts void validation for the given attribute.
-
#required(attribute, error = nil, &block) ⇒ Object
“required” checks presence of the variable.
-
#type(clazz, error = nil, &block) ⇒ Object
“type” validates type of the instance variable.
-
#valid? ⇒ Boolean
“valid?” returns inner valid state.
Instance Method Details
#add_error(args = {}) ⇒ FalseClass
“add_error” adds an error and set valid state to false
102 103 104 105 |
# File 'lib/validy.rb', line 102 def add_error(args = {}) @errors.merge!(args) @valid = false end |
#condition(method, error = nil, &block) ⇒ Object
“condition” evaluates either passed block or instance method represented in the instance
129 130 131 132 133 134 135 136 |
# File 'lib/validy.rb', line 129 def condition(method, error = nil, &block) return self unless valid? return self if skip_optional? condition = method.respond_to?(:call) ? method.call : send(method) validate_condition(condition, error, &block) self end |
#errors ⇒ Hash
“errors” returns errors hash
121 122 123 |
# File 'lib/validy.rb', line 121 def errors @errors end |
#invalid? ⇒ Boolean
“invalid?” returns opposite value of inner valid state
115 116 117 |
# File 'lib/validy.rb', line 115 def invalid? !valid? end |
#optional(attribute) ⇒ Object
“optional” starts void validation for the given attribute
154 155 156 157 158 159 160 |
# File 'lib/validy.rb', line 154 def optional(attribute) return self unless valid? @optional = true @evaluating_attribute_value = instance_variable_get("@#{attribute}") self end |
#required(attribute, error = nil, &block) ⇒ Object
“required” checks presence of the variable
142 143 144 145 146 147 148 149 150 |
# File 'lib/validy.rb', line 142 def required(attribute, error = nil, &block) return self unless valid? @evaluating_attribute_value = instance_variable_get("@#{attribute}") unless @evaluating_attribute_value.instance_of?(FalseClass) validate_condition(@evaluating_attribute_value, error || "#{attribute} required!", &block) end self end |
#type(clazz, error = nil, &block) ⇒ Object
“type” validates type of the instance variable
166 167 168 169 170 171 172 173 174 175 |
# File 'lib/validy.rb', line 166 def type(clazz, error = nil, &block) return self unless valid? return self if skip_optional? validate_condition( @evaluating_attribute_value&.is_a?(clazz), error || "`#{@evaluating_attribute_value}` is not a type #{clazz}", &block ) self end |
#valid? ⇒ Boolean
“valid?” returns inner valid state
109 110 111 |
# File 'lib/validy.rb', line 109 def valid? @valid end |