Class: Attribeauty::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/attribeauty/validator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, original_val, type = nil, args = {}) ⇒ Validator

Returns a new instance of Validator.



12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/attribeauty/validator.rb', line 12

def initialize(name, original_val, type = nil, args = {})
  @name = name
  @type = type
  @original_val = original_val
  @default = args[:default]
  args.delete_if { |key, value| key == :required && value == false }
  @required = args[:required]
  @allows = args[:allow]
  @excludes = args[:exclude_if]

  @valid = true
  @errors = []
end

Instance Attribute Details

#allows ⇒ Object (readonly)

Returns the value of attribute allows.



9
10
11
# File 'lib/attribeauty/validator.rb', line 9

def allows
  @allows
end

#default ⇒ Object (readonly)

Returns the value of attribute default.



9
10
11
# File 'lib/attribeauty/validator.rb', line 9

def default
  @default
end

#errors ⇒ Object (readonly)

Returns the value of attribute errors.



9
10
11
# File 'lib/attribeauty/validator.rb', line 9

def errors
  @errors
end

#excludes ⇒ Object (readonly)

Returns the value of attribute excludes.



9
10
11
# File 'lib/attribeauty/validator.rb', line 9

def excludes
  @excludes
end

#name ⇒ Object (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/attribeauty/validator.rb', line 9

def name
  @name
end

#original_val ⇒ Object (readonly)

Returns the value of attribute original_val.



9
10
11
# File 'lib/attribeauty/validator.rb', line 9

def original_val
  @original_val
end

#required ⇒ Object (readonly)

Returns the value of attribute required.



9
10
11
# File 'lib/attribeauty/validator.rb', line 9

def required
  @required
end

#type ⇒ Object (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/attribeauty/validator.rb', line 9

def type
  @type
end

#valid ⇒ Object (readonly)

Returns the value of attribute valid.



9
10
11
# File 'lib/attribeauty/validator.rb', line 9

def valid
  @valid
end

#value ⇒ Object (readonly)

Returns the value of attribute value.



9
10
11
# File 'lib/attribeauty/validator.rb', line 9

def value
  @value
end

Class Method Details

.run(name, original_val, type = nil, args = {}) ⇒ Object



5
6
7
# File 'lib/attribeauty/validator.rb', line 5

def self.run(name, original_val, type = nil, args = {})
  new(name, original_val, type, args).run
end

Instance Method Details

#required? ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/attribeauty/validator.rb', line 45

def required?
  required
end

#run ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/attribeauty/validator.rb', line 26

def run
  handle_allows
  handle_missing_original_val!
  handle_missing_required!

  handle_missing_type
  set_default
  cast_value
  handle_excludes

  self
rescue ValueInvalidError
  self
end

#valid? ⇒ Boolean

Returns:

  • (Boolean)


41
42
43
# File 'lib/attribeauty/validator.rb', line 41

def valid?
  valid
end