Module: Validy::InstanceMethods

Defined in:
lib/validy.rb

Instance Method Summary collapse

Instance Method Details

#add_error(args = {}) ⇒ FalseClass

“add_error” adds an error and set valid state to false

Returns:

  • (FalseClass)


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

Parameters:

  • method (String|block)

    or callable object which will be triggered for validation

  • error (String|Hash) (defaults to: nil)

    definition

  • block (Proc)


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

#errorsHash

“errors” returns errors hash

Returns:

  • (Hash)


121
122
123
# File 'lib/validy.rb', line 121

def errors
  @errors
end

#invalid?Boolean

“invalid?” returns opposite value of inner valid state

Returns:

  • (Boolean)


115
116
117
# File 'lib/validy.rb', line 115

def invalid?
  !valid?
end

#optional(attribute) ⇒ Object

“optional” starts void validation for the given attribute

Parameters:

  • attribute (String)

    a target one



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

Parameters:

  • attribute (String)

    a target one

  • error (String|Hash) (defaults to: nil)

    custom defined error message

  • block (Proc)


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

Parameters:

  • clazz (Object)

    for checking type of the target attribute

  • error (nil) (defaults to: nil)

    custom defined error message

  • block (Proc)


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

Returns:

  • (Boolean)


109
110
111
# File 'lib/validy.rb', line 109

def valid?
  @valid
end