Class: PoroValidator::Validators::BaseClass

Inherits:
Object
  • Object
show all
Defined in:
lib/poro_validator/validators/base_class.rb

Overview

Since:

  • 0.0.1

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attribute, options = {}) ⇒ BaseClass

Returns a new instance of BaseClass.

Since:

  • 0.0.1



6
7
8
9
# File 'lib/poro_validator/validators/base_class.rb', line 6

def initialize(attribute, options = {})
  @attribute = attribute
  @options   = options
end

Instance Attribute Details

#attributeObject

Since:

  • 0.0.1



15
16
17
# File 'lib/poro_validator/validators/base_class.rb', line 15

def attribute
  @attribute
end

Instance Method Details

#__validate__(validator_context) ⇒ Object

Since:

  • 0.0.1



62
63
64
65
66
# File 'lib/poro_validator/validators/base_class.rb', line 62

def __validate__(validator_context)
  @context = validator_context
  @errors  = validator_context.errors
  validate(attribute, value, options)
end

#contextObject

Since:

  • 0.0.1



23
24
25
# File 'lib/poro_validator/validators/base_class.rb', line 23

def context
  @context
end

#errorsObject

Since:

  • 0.0.1



19
20
21
# File 'lib/poro_validator/validators/base_class.rb', line 19

def errors
  @errors
end

#nested?Boolean

Returns:

  • (Boolean)

Since:

  • 0.0.1



27
28
29
# File 'lib/poro_validator/validators/base_class.rb', line 27

def nested?
  attribute.is_a?(::Array)
end

#optionsObject

Since:

  • 0.0.1



11
12
13
# File 'lib/poro_validator/validators/base_class.rb', line 11

def options
  @options
end

#validate(attribute, value, options) ⇒ Object



31
32
33
34
35
# File 'lib/poro_validator/validators/base_class.rb', line 31

def validate(attribute, value, options)
  raise ::PoroValidator::OverloadriddenRequired.new(
    "This method needs to be overloaded/overriden."
  )
end

#valueObject

Since:

  • 0.0.1



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/poro_validator/validators/base_class.rb', line 37

def value
  if entity_is_hash = context.entity.is_a?(::Hash)
    context.entity.extend(::PoroValidator::Utils::DeepFetch)
  end

  if nested?
    if entity_is_hash
      @value = context.entity.deep_fetch(*attribute.flatten) do
        nil
      end
    else
      @value = attribute.flatten.inject(context.entity, :public_send)
    end
  else
    if entity_is_hash
      @value = context.entity.deep_fetch(attribute) do
        nil
      end
    else
      @value = context.entity.public_send(attribute)
    end
  end
end