Class: Zizia::Parser
- Inherits:
-
Object
- Object
- Zizia::Parser
- Defined in:
- lib/zizia/parser.rb
Overview
A generic parser.
‘Parser` implementations provide a stream of `InputRecord`s, derived from an input object (`file`), through `Parser#records`. This method should be implemented efficiently for repeated access, generating records lazily if possible, and caching if appropriate.
Input validation is handled by an array of ‘#validators`, which are run in sequence when `#validate` (or `#validate!`) is called. Errors caught in validation are accessible via `#errors`, and inputs generating errors result in `#valid? # => false`.
A factory method ‘.for` is provided, and each implementation should provides a `.match?(**)` which returns `true` if the options passed indicate the parser can handle the given input. Parsers are checked for `#match?` in the reverse of load order (i.e. the most recently loaded `Parser` classes are given precedence).
rubocop:disable Style/ClassVars
Direct Known Subclasses
Defined Under Namespace
Classes: NoParserError, ValidationError
Constant Summary collapse
- DEFAULT_VALIDATORS =
[].freeze
- @@subclasses =
[]
Instance Attribute Summary collapse
Class Method Summary collapse
-
.for(file:) ⇒ Zizia::Parser
A parser instance appropriate for the arguments.
- .match?(**_opts) ⇒ Boolean abstract
Instance Method Summary collapse
-
#initialize(file:, **_opts) {|_self| ... } ⇒ Parser
constructor
A new instance of Parser.
- #records {|record| ... } ⇒ Enumerable<ImportRecord> abstract
-
#valid? ⇒ Boolean
True if the file input is valid.
-
#validate ⇒ Boolean
True if the file input is valid.
-
#validate! ⇒ true
Always true, unless an error is raised.
Constructor Details
#initialize(file:, **_opts) {|_self| ... } ⇒ Parser
Returns a new instance of Parser.
55 56 57 58 59 60 61 |
# File 'lib/zizia/parser.rb', line 55 def initialize(file:, **_opts) self.file = file @errors = [] @validators ||= self.class::DEFAULT_VALIDATORS yield self if block_given? end |
Instance Attribute Details
#errors ⇒ Array (readonly)
50 |
# File 'lib/zizia/parser.rb', line 50 attr_accessor :file, :validators |
#file ⇒ File
50 51 52 |
# File 'lib/zizia/parser.rb', line 50 def file @file end |
#validators ⇒ Array<Validator>
50 |
# File 'lib/zizia/parser.rb', line 50 attr_accessor :file, :validators |
Class Method Details
.for(file:) ⇒ Zizia::Parser
Returns a parser instance appropriate for the arguments.
71 72 73 74 75 76 77 |
# File 'lib/zizia/parser.rb', line 71 def for(file:) klass = @@subclasses.find { |k| k.match?(file: file) } || raise(NoParserError) klass.new(file: file) end |
.match?(**_opts) ⇒ Boolean
82 |
# File 'lib/zizia/parser.rb', line 82 def match?(**_opts); end |
Instance Method Details
#records {|record| ... } ⇒ Enumerable<ImportRecord>
101 102 103 |
# File 'lib/zizia/parser.rb', line 101 def records raise NotImplementedError end |
#valid? ⇒ Boolean
Returns true if the file input is valid.
107 108 109 |
# File 'lib/zizia/parser.rb', line 107 def valid? errors.empty? end |
#validate ⇒ Boolean
Returns true if the file input is valid.
113 114 115 116 117 118 119 |
# File 'lib/zizia/parser.rb', line 113 def validate validators.each_with_object(errors) do |validator, errs| errs.concat(validator.validate(parser: self)) end valid? end |
#validate! ⇒ true
Returns always true, unless an error is raised.
125 126 127 |
# File 'lib/zizia/parser.rb', line 125 def validate! validate || raise(ValidationError) end |