Class: JsDuck::Options::Record
- Inherits:
-
Object
- Object
- JsDuck::Options::Record
- Defined in:
- lib/jsduck/options/record.rb
Overview
Stores values of command line options.
All options are initially defined with an #attribute method, which ensures that accessing an unexisting option will result in an error.
Instance Method Summary collapse
-
#attribute(name, default = nil) ⇒ Object
Defines accessor for an option, and assigns a default value for it.
-
#initialize ⇒ Record
constructor
A new instance of Record.
-
#validate!(name = nil) ⇒ Object
Runs all the validators.
-
#validator(name, &block) ⇒ Object
Defines a validator function that gets run after all the options have been parsed.
Constructor Details
#initialize ⇒ Record
Returns a new instance of Record.
10 11 12 |
# File 'lib/jsduck/options/record.rb', line 10 def initialize @validators = {} end |
Instance Method Details
#attribute(name, default = nil) ⇒ Object
Defines accessor for an option, and assigns a default value for it.
16 17 18 19 20 21 22 |
# File 'lib/jsduck/options/record.rb', line 16 def attribute(name, default=nil) instance_variable_set("@#{name}", default) # Use `send` to invoke private attr_accessor method. As we only # expect a single OptionsRecord to exist for the lifetime of the # app, it should be safe to define a method on a class. self.class.send(:attr_accessor, name) end |
#validate!(name = nil) ⇒ Object
Runs all the validators. Returns an error message string from the first failed validation or nil when everything is OK.
Alternatively runs just one validator by name. Used in testing.
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/jsduck/options/record.rb', line 37 def validate!(name=nil) validators = name ? [@validators[name]] : @validators.values validators.each do |block| if err = block.call() return err end end return nil end |
#validator(name, &block) ⇒ Object
Defines a validator function that gets run after all the options have been parsed. When validation fails, the function should return an error message string (or an array of string for multi-line error message) otherwise nil, to signify success.
29 30 31 |
# File 'lib/jsduck/options/record.rb', line 29 def validator(name, &block) @validators[name] = block end |