Class: Toolchain::Validations::Validators::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/toolchain/validations/validators/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(object, key_path, data) ⇒ Base

Instantiates a new Toolchain::Validations::Validators object which’ll be used to validate the target object.

Parameters:

  • object (Object)
  • key_path (Object)
  • data (Object)


35
36
37
38
39
40
41
# File 'lib/toolchain/validations/validators/base.rb', line 35

def initialize(object, key_path, data)
  @object = object
  @errors = object.errors
  @key_path = key_path
  @data = data
  @message = data[:message] if data.is_a?(Hash)
end

Instance Attribute Details

#dataHash (readonly)

Returns The value that was passed in to the validator at definition time. i.e. a Hash containing the regular expression for Toolchain::Validations::Validators::Formatter along with a message.

Returns:

  • (Hash)

    The value that was passed in to the validator at definition time. i.e. a Hash containing the regular expression for Toolchain::Validations::Validators::Formatter along with a message.



21
22
23
# File 'lib/toolchain/validations/validators/base.rb', line 21

def data
  @data
end

#errorsToolchain::Validations::ValidationErrors (readonly)

Returns the errors object of the object that’s being validated.

Returns:



11
12
13
# File 'lib/toolchain/validations/validators/base.rb', line 11

def errors
  @errors
end

#key_pathSymbol (readonly)

Returns The key_path of the attribute that’s being validated.

Returns:

  • (Symbol)

    The key_path of the attribute that’s being validated.



15
16
17
# File 'lib/toolchain/validations/validators/base.rb', line 15

def key_path
  @key_path
end

#messageString (readonly)

Returns A custom message to override the default message with in case the attribute is invalid.

Returns:

  • (String)

    A custom message to override the default message with in case the attribute is invalid.



26
27
28
# File 'lib/toolchain/validations/validators/base.rb', line 26

def message
  @message
end

#objectObject (readonly)

Returns the object that contains the attribute that’ll be validated.

Returns:

  • (Object)

    the object that contains the attribute that’ll be validated.



6
7
8
# File 'lib/toolchain/validations/validators/base.rb', line 6

def object
  @object
end

Instance Method Details

#valueObject

Returns The value that’s stored in the attribute of the object that’s being validated.

Returns:

  • (Object)

    The value that’s stored in the attribute of the object that’s being validated.



46
47
48
49
50
51
52
53
# File 'lib/toolchain/validations/validators/base.rb', line 46

def value
  @value ||= (
    keys = key_path.dup
    keys.inject(object.send(keys.shift)) do |memo, key|
      memo[key] if memo.kind_of?(Hash)
    end
  )
end