Class: Gambiarra::App
- Inherits:
-
Object
- Object
- Gambiarra::App
- Defined in:
- lib/gambiarra/app.rb
Class Attribute Summary collapse
-
.app_class ⇒ Object
readonly
Returns the value of attribute app_class.
Instance Attribute Summary collapse
-
#debugging ⇒ Object
readonly
Returns the value of attribute debugging.
-
#router ⇒ Object
readonly
Returns the value of attribute router.
Class Method Summary collapse
Instance Method Summary collapse
- #app_name ⇒ Object
- #ask_question(question) ⇒ Object
- #ask_questions(questions) ⇒ Object
- #exit ⇒ Object
- #flow(response) ⇒ Object
-
#initialize(debugging: false) ⇒ App
constructor
A new instance of App.
- #navigation(params) ⇒ Object
- #output(response) ⇒ Object
Constructor Details
Class Attribute Details
.app_class ⇒ Object (readonly)
Returns the value of attribute app_class.
8 9 10 |
# File 'lib/gambiarra/app.rb', line 8 def app_class @app_class end |
Instance Attribute Details
#debugging ⇒ Object (readonly)
Returns the value of attribute debugging.
5 6 7 |
# File 'lib/gambiarra/app.rb', line 5 def debugging @debugging end |
#router ⇒ Object (readonly)
Returns the value of attribute router.
5 6 7 |
# File 'lib/gambiarra/app.rb', line 5 def router @router end |
Class Method Details
.app ⇒ Object
29 30 31 |
# File 'lib/gambiarra/app.rb', line 29 def self.app @app ||= new end |
.inherited(app_class) ⇒ Object
10 11 12 |
# File 'lib/gambiarra/app.rb', line 10 def inherited(app_class) @app_class = app_class end |
.start(initial_route = '') ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/gambiarra/app.rb', line 15 def self.start(initial_route='') path, params = initial_route.split(' ').each_with_object([nil, {}]) do |segment, memo| if segment =~ /:/ key, value = segment.split(':') memo[1].merge!([[key.to_sym, value.gsub('-', ' ')]].to_h) else memo[0] = [memo[0], segment].compact.join(' ') end end response = {path: (path || ''), **params} loop { response = app.flow(**response) } end |
Instance Method Details
#app_name ⇒ Object
70 71 72 |
# File 'lib/gambiarra/app.rb', line 70 def app_name self.class.to_s.split('::').first.underscore.gsub('_', '-') end |
#ask_question(question) ⇒ Object
44 45 46 |
# File 'lib/gambiarra/app.rb', line 44 def ask_question(question) CLI::UI::Prompt.ask(question[:statement], options: question[:options]) end |
#ask_questions(questions) ⇒ Object
40 41 42 |
# File 'lib/gambiarra/app.rb', line 40 def ask_questions(questions) questions.map { |name, question| [name, ask_question(question)] }.to_h end |
#exit ⇒ Object
90 91 92 |
# File 'lib/gambiarra/app.rb', line 90 def exit Kernel.exit(0) end |
#flow(response) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/gambiarra/app.rb', line 74 def flow(response) raise 'Response is empty' unless response output(response) response.delete(:content) questions = response&.delete(:questions) || {} response.merge!((response)) unless response.has_key?(:path) || questions.any? response.merge!(ask_questions(questions)) if questions.any? exit if response[:path] == 'exit' router.get(**response.compact) rescue => e puts e.backtrace if debugging puts e response.delete(:path) retry unless debugging end |
#navigation(params) ⇒ Object
48 49 50 51 52 53 54 55 56 57 |
# File 'lib/gambiarra/app.rb', line 48 def (params) return {} if params.empty? CLI::UI::Prompt.ask('What to do now?') do |h| params.each do |key, _value| h.option("Different #{key}") { params[key] = nil } end h.option("Go back") { params[:path] = '' } end params end |
#output(response) ⇒ Object
59 60 61 62 63 64 65 66 67 68 |
# File 'lib/gambiarra/app.rb', line 59 def output(response) return unless response[:content] puts [ nil, "-> #{app_name} #{router.url}", nil, response[:content], nil ] end |