Class: Framecurve::Validator

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

Overview

Validates a Curve object for well-formedness and completeness.

v = Validator.new
v.parse(io_handle)
v.errors => []
v.warnings => ["Do not put cusswords in your framecurves"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeValidator

Returns a new instance of Validator.



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

def initialize
  @warnings, @errors, @performed = [], [], false
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



7
8
9
# File 'lib/framecurve/validator.rb', line 7

def errors
  @errors
end

#warningsObject (readonly)

Returns the value of attribute warnings.



7
8
9
# File 'lib/framecurve/validator.rb', line 7

def warnings
  @warnings
end

Instance Method Details

#any_errors?Boolean

Tells whether this validator instance has any errors

Returns:

  • (Boolean)


14
15
16
# File 'lib/framecurve/validator.rb', line 14

def any_errors?
  @errors.any?
end

#any_warnings?Boolean

Tells whether this validator instance has any warnings

Returns:

  • (Boolean)


19
20
21
# File 'lib/framecurve/validator.rb', line 19

def any_warnings?
  @warnings.any?
end

#ok?Boolean

Returns true if validation has been performed and there are no warnings and no errors

Returns:

  • (Boolean)


42
43
44
# File 'lib/framecurve/validator.rb', line 42

def ok?
  @performed && !any_errors? && !any_warnings?
end

#parse_and_validate(path_or_io) ⇒ Object

Parse and validate a file (API similar to Parser#parse)



24
25
26
27
28
29
30
# File 'lib/framecurve/validator.rb', line 24

def parse_and_validate(path_or_io)
  begin
    validate(Framecurve::Parser.new.parse(path_or_io))
  rescue Framecurve::Malformed => e
    @errors.push(e.message)
  end
end

#validate(curve) ⇒ Object

Validate a passed Curve object



33
34
35
36
37
38
39
# File 'lib/framecurve/validator.rb', line 33

def validate(curve)
  initialize # reset
  methods_matching(/^(verify|recommend)/).each do | method_name |
    method(method_name).call(curve)
  end
  @performed = true
end