Class: GitPrepareBranch::App

Inherits:
Object
  • Object
show all
Defined in:
lib/git-prepare-branch/app.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil) ⇒ App

Returns a new instance of App.



17
18
19
# File 'lib/git-prepare-branch/app.rb', line 17

def initialize(logger: nil)
  @logger = logger || Logger.new
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/git-prepare-branch/app.rb', line 13

def logger
  @logger
end

#routerObject

Returns the value of attribute router.



15
16
17
# File 'lib/git-prepare-branch/app.rb', line 15

def router
  @router
end

#titleObject

Returns the value of attribute title.



15
16
17
# File 'lib/git-prepare-branch/app.rb', line 15

def title
  @title
end

Class Method Details

.configure(*options, &block) ⇒ Object



93
94
95
# File 'lib/git-prepare-branch/app.rb', line 93

def configure(*options, &block)
  self.new(*options).configure(&block)
end

Instance Method Details

#add_event_handler(event, &block) ⇒ Object



21
22
23
24
# File 'lib/git-prepare-branch/app.rb', line 21

def add_event_handler(event, &block)
  event_handlers[event] ||= []
  event_handlers[event] << block
end

#add_screen(screen) ⇒ Object



26
27
28
29
# File 'lib/git-prepare-branch/app.rb', line 26

def add_screen screen
  screen.context = context
  screens[screen.name] = screen
end

#add_variable(name, capture: nil, value: nil) ⇒ Object



31
32
33
# File 'lib/git-prepare-branch/app.rb', line 31

def add_variable name, capture: nil, value: nil
  context.variables[name] = Variable.new(name, context: context, capture: capture, value: value).value
end

#configure(&block) ⇒ Object



35
36
37
38
# File 'lib/git-prepare-branch/app.rb', line 35

def configure(&block)
  Configurator.new(self).apply(&block)
  self
end

#contextObject



40
41
42
43
44
45
# File 'lib/git-prepare-branch/app.rb', line 40

def context
  @context ||= Context.new(
    terminal: terminal,
    variables: {}
  )
end

#current_screenObject



47
48
49
# File 'lib/git-prepare-branch/app.rb', line 47

def current_screen
  screens[current_screen_name]
end

#current_screen_nameObject



51
52
53
# File 'lib/git-prepare-branch/app.rb', line 51

def current_screen_name
  router.call(context)
end

#screensObject



55
56
57
# File 'lib/git-prepare-branch/app.rb', line 55

def screens
  @screens ||= {}
end

#start(variables = []) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/git-prepare-branch/app.rb', line 59

def start(variables = [])
  variables.each { |name, args| add_variable(name, args) }
  trigger :load
  while true do
    begin
      terminal.clear
      trigger :display
      terminal.write_line format(current_screen.heading, context.variables.to_h), current_screen.heading_style
      terminal.call current_screen.display, context.variables.to_h
      terminal.say 'Press a command key or ? for help', :hint
      begin
        result = terminal.wait_for_keypress
        current_screen.handle_keypress result, context
      rescue Interrupt
      end
    rescue Interrupt
      exit
    end
  end
end

#terminalObject



80
81
82
# File 'lib/git-prepare-branch/app.rb', line 80

def terminal
  @terminal ||= Terminal.new(logger: logger)
end

#trigger(event) ⇒ Object



84
85
86
87
88
89
90
# File 'lib/git-prepare-branch/app.rb', line 84

def trigger event
  return unless event_handlers[event]

  event_handlers[event].each do |block|
    block.call(context)
  end
end