Class: KafoWizards::AbstractWizard
- Inherits:
-
Object
- Object
- KafoWizards::AbstractWizard
- Defined in:
- lib/kafo_wizards/abstract_wizard.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#description ⇒ Object
readonly
Returns the value of attribute description.
-
#entries ⇒ Object
Returns the value of attribute entries.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
-
#interactive ⇒ Object
Returns the value of attribute interactive.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#renderers ⇒ Object
Returns the value of attribute renderers.
-
#validators ⇒ Object
Returns the value of attribute validators.
Class Method Summary collapse
Instance Method Summary collapse
- #buttons ⇒ Object
- #execute_menu ⇒ Object
- #factory ⇒ Object
-
#initialize(header, options = {}) ⇒ AbstractWizard
constructor
A new instance of AbstractWizard.
- #run ⇒ Object
- #triggers ⇒ Object
- #update(values = {}) ⇒ Object
- #validate(values) ⇒ Object
- #values ⇒ Object
Constructor Details
#initialize(header, options = {}) ⇒ AbstractWizard
Returns a new instance of AbstractWizard.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 7 def initialize(header, = {}) @header = header @interactive = .fetch(:interactive, true) @entries = .fetch(:entries, []) @description = .fetch(:description, '') @renderers = .fetch(:renderers, self.class.default_renderers) @validators = .fetch(:validators, []) @validators << lambda { |values| check_required_entries(values) } @logger = .fetch(:logger) do |l| logger = Logger.new(STDERR) logger.level = Logger::ERROR logger end end |
Instance Attribute Details
#description ⇒ Object (readonly)
Returns the value of attribute description.
5 6 7 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 5 def description @description end |
#entries ⇒ Object
Returns the value of attribute entries.
6 7 8 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 6 def entries @entries end |
#header ⇒ Object (readonly)
Returns the value of attribute header.
5 6 7 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 5 def header @header end |
#interactive ⇒ Object
Returns the value of attribute interactive.
6 7 8 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 6 def interactive @interactive end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
5 6 7 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 5 def logger @logger end |
#renderers ⇒ Object
Returns the value of attribute renderers.
6 7 8 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 6 def renderers @renderers end |
#validators ⇒ Object
Returns the value of attribute validators.
6 7 8 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 6 def validators @validators end |
Class Method Details
.default_renderers ⇒ Object
79 80 81 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 79 def self.default_renderers @default_renderers ||= {} end |
.register_default_renderer(display_type, renderer) ⇒ Object
83 84 85 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 83 def self.register_default_renderer(display_type, renderer) default_renderers[display_type] = renderer end |
Instance Method Details
#buttons ⇒ Object
36 37 38 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 36 def @entries.select { |e| e.class >= KafoWizards::Entries::ButtonEntry } end |
#execute_menu ⇒ Object
33 34 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 33 def end |
#factory ⇒ Object
75 76 77 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 75 def factory Factory.new(self) end |
#run ⇒ Object
22 23 24 25 26 27 28 29 30 31 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 22 def run if @interactive choice = else choice = nil = .find { |b| b.default? } choice = .name if end choice end |
#triggers ⇒ Object
40 41 42 43 44 45 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 40 def triggers .inject([]) do |b_names, b| b_names << b.name if b.trigger_validation? b_names end end |
#update(values = {}) ⇒ Object
54 55 56 57 58 59 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 54 def update(values = {}) entries.each do |entry| entry.update(values[entry.name]) unless values[entry.name].nil? end end |
#validate(values) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 61 def validate(values) errors = [] validated = @validators.inject(values) do |result, lam| begin lam.call(result) rescue ValidationError => e errors += e. result end end raise ValidationError.new errors unless errors.empty? validated end |
#values ⇒ Object
47 48 49 50 51 52 |
# File 'lib/kafo_wizards/abstract_wizard.rb', line 47 def values @entries.inject({}) do |vals, entry| vals[entry.name] = entry.value unless entry.class >= KafoWizards::Entries::ButtonEntry vals end end |