Class: GreatGuardian::Attribute::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/great_guardian/attribute/base.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_value: nil, medium: :body, required: true, **custom_constraints) ⇒ Base

Returns a new instance of Base.

Raises:

  • (::ArgumentError)

18
19
20
21
22
23
24
25
26
# File 'lib/great_guardian/attribute/base.rb', line 18

def initialize(default_value: nil, medium: :body, required: true, **custom_constraints)
  raise ::ArgumentError, required.inspect unless [false, true].include?(required)
  raise ::ArgumentError, medium.inspect    unless %i[body header querystring].include?(medium)

  @custom_constraints = custom_constraints
  @default_value      = default_value
  @medium             = medium
  @required           = required
end

Instance Attribute Details

#custom_constraintsObject (readonly)

Returns the value of attribute custom_constraints


6
7
8
# File 'lib/great_guardian/attribute/base.rb', line 6

def custom_constraints
  @custom_constraints
end

#default_valueObject (readonly)

Returns the value of attribute default_value


6
7
8
# File 'lib/great_guardian/attribute/base.rb', line 6

def default_value
  @default_value
end

#mediumObject (readonly)

Returns the value of attribute medium


6
7
8
# File 'lib/great_guardian/attribute/base.rb', line 6

def medium
  @medium
end

Class Method Details

.default_constraintsObject


8
9
10
# File 'lib/great_guardian/attribute/base.rb', line 8

def self.default_constraints
  {}
end

.default_valueObject


12
# File 'lib/great_guardian/attribute/base.rb', line 12

def self.default_value; end

.first_matched_error_on_complex_validation_logic_against(_actual_value) ⇒ Object


14
# File 'lib/great_guardian/attribute/base.rb', line 14

def self.first_matched_error_on_complex_validation_logic_against(_actual_value); end

.possible_valuesObject


16
# File 'lib/great_guardian/attribute/base.rb', line 16

def self.possible_values; end

Instance Method Details

#body?Boolean

Returns:

  • (Boolean)

62
63
64
# File 'lib/great_guardian/attribute/base.rb', line 62

def body?
  medium.equal?(:body)
end

#call(actual_value) ⇒ Object


28
29
30
31
32
33
# File 'lib/great_guardian/attribute/base.rb', line 28

def call(actual_value)
  actual_value  = actual_value_or_default_value(actual_value)
  error_message = compute_error_message(actual_value)

  Verdict.new(to_s, actual_value, error_message, medium)
end

#constraintsObject


35
36
37
# File 'lib/great_guardian/attribute/base.rb', line 35

def constraints
  self.class.default_constraints.merge(**custom_constraints)
end

#expected_valueObject


39
40
41
# File 'lib/great_guardian/attribute/base.rb', line 39

def expected_value
  self.class.expected_value_type.new(**constraints)
end

#header_or_querystring?Boolean

Returns:

  • (Boolean)

66
67
68
# File 'lib/great_guardian/attribute/base.rb', line 66

def header_or_querystring?
  !body?
end

#required?Boolean

Returns:

  • (Boolean)

43
44
45
# File 'lib/great_guardian/attribute/base.rb', line 43

def required?
  @required
end

#to_sObject


47
48
49
50
51
52
53
54
55
56
# File 'lib/great_guardian/attribute/base.rb', line 47

def to_s
  self.class
      .name
      .split("::")
      .fetch(-1)
      .gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2')
      .gsub(/([a-z\d])([A-Z])/,'\1_\2')
      .tr("-", "_")
      .downcase
end

#to_symObject


58
59
60
# File 'lib/great_guardian/attribute/base.rb', line 58

def to_sym
  to_s.to_sym
end