Class: GK::Application

Inherits:
Object
  • Object
show all
Defined in:
lib/gk-application.rb

Constant Summary collapse

GEM_NAME =

The name of this gem

'gk-application'
TEMPLATE_PROJECT =

The name of the project template

'my_app.rb'
MSG_INVALID_STATE =

Custom exception messages

'This application does not support the requested state.'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeApplication

This is run once every time an Application instance is created



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/gk-application.rb', line 65

def initialize
  # Initialize event handlers
  @on_starting = proc {
    state = :running
  }
  @on_running = proc {
    state = :stopping
  }
  @on_stopping = proc {
    state = :stopped
  }
  @on_stopped = proc { }
  # Always create the Application instance in the stopped state
  state = :stopped
end

Instance Attribute Details

#on_runningObject

Returns the value of attribute on_running.



60
61
62
# File 'lib/gk-application.rb', line 60

def on_running
  @on_running
end

#on_startingObject

State change event handlers



59
60
61
# File 'lib/gk-application.rb', line 59

def on_starting
  @on_starting
end

#on_stoppedObject

Returns the value of attribute on_stopped.



62
63
64
# File 'lib/gk-application.rb', line 62

def on_stopped
  @on_stopped
end

#on_stoppingObject

Returns the value of attribute on_stopping.



61
62
63
# File 'lib/gk-application.rb', line 61

def on_stopping
  @on_stopping
end

Instance Method Details

#project(name: TEMPLATE_PROJECT) ⇒ Object

This creates a new GK::Application project. The new application will be called “my_app.rb” and be placed in the current directory unless otherwise specified.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/gk-application.rb', line 84

def project(name: TEMPLATE_PROJECT)
  # Get the path for the gk-application gem
  spec = Gem::Specification.find_by_name(GEM_NAME)
  gem_root = spec.gem_dir
  source_file = File.join(gem_root, TEMPLATE_PROJECT);
  # Create the target folder if it doesn't exist
  target_dir = File.dirname(name)
  FileUtils.mkdir_p(target_dir)
  # Copy the my_app.rb file (in the gem directory) to the target
  target_file = File.join(target_dir, name)
  FileUtils.cp(source_file, target_file)
end

#stateObject

An Application instance’s state may be one of :starting :running :stopping :stopped



40
41
42
# File 'lib/gk-application.rb', line 40

def state
  @state
end

#state=(new_state) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/gk-application.rb', line 43

def state=(new_state)
  case new_state
  when :starting
    @on_starting.call
  when :running
    @on_running.call
  when :stopping
    @on_stopping.call
  when :stopped
    @on_stopped.call
  else
    raise MSG_INVALID_STATE
  end
end