class GK::Application

Constants

GEM_NAME

The name of this gem

MSG_INVALID_STATE

Custom exception messages

TEMPLATE_PROJECT

The name of the project template

Attributes

on_running[RW]
on_starting[RW]

State change event handlers

on_stopped[RW]
on_stopping[RW]

Public Class Methods

new() click to toggle source

This is run once every time an Application instance is created

# 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

Public Instance Methods

project(name: TEMPLATE_PROJECT) click to toggle source

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.

# 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
state() click to toggle source

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

# File lib/gk-application.rb, line 40
def state
  @state
end
state=(new_state) click to toggle source
# 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