class AdminModule::Stages

Attributes

page_factory[R]

Public Class Methods

new(page_factory) click to toggle source
# File lib/admin_module/stages.rb, line 17
def initialize page_factory
  @page_factory = page_factory
end

Public Instance Methods

create(data) click to toggle source

Create stage configuration data for a new stage

# File lib/admin_module/stages.rb, line 50
def create data
  # When creating a stage, we need to set its name and save it so
  # an ID is created in the database to tie the tasks to.
  #
  # Foreign key errors will result otherwise.
  name = data[:name]

  stages_page
    .add
    .set_name(name)
    .save

  # Now, populate the rest of the data.
  stages_page
    .modify(name)
    .set_stage_data(data)
    .save
end
delete(name) click to toggle source

Delete a stage

# File lib/admin_module/stages.rb, line 72
def delete name
  assert_stage_exists name

  stages_page
    .delete name
end
export(file_path) click to toggle source

Export data for all stages to a file

# File lib/admin_module/stages.rb, line 92
def export file_path
  stages = list
  export_data = {}

  stages.each do |stage|
    export_data[stage] = read stage
  end

  File.open(file_path, 'w') do |f|
    f.write export_data.to_yaml
  end

rescue Exception => e
  if e.message.include? 'No such file or directory'
    raise IOError, "No such directory - #{file_path}"
  else
    raise e
  end
end
import(file_path, allow_creation = false) click to toggle source

Import stage data from a file

If the stage name doesn’t currently exist, a new stage will be created. If the stage name exists, it will be updated. If allow_creation is false (default) a stage will NOT be created if it doesn’t exist, existing stages will be updated.

# File lib/admin_module/stages.rb, line 120
def import file_path, allow_creation = false
  raise IOError, "File not found: #{file_path}" unless File.exist? file_path

  import_data = {}
  File.open(file_path, 'r') do |f|
    # Read array of stage hashes
    # FIXME is this REALLY an array?
    import_data = YAML.load(f)
  end

  existing_stages = list

  import_data.each do |name, data|
    if existing_stages.include? name
      update name, data
    else
      if allow_creation
        create data
      else
        puts "Stage '#{name}' does not exist. Skipping import."
      end
    end
  end
end
list() click to toggle source

Return a list of stage names

# File lib/admin_module/stages.rb, line 24
def list
  stages_page.get_stages
end
read(name) click to toggle source

Retrieve stage configuration data for an existing stage

# File lib/admin_module/stages.rb, line 31
def read name
  stages_page
    .modify(name)
    .get_stage_data
end
rename(src, dest) click to toggle source
# File lib/admin_module/stages.rb, line 79
def rename src, dest
  assert_stage_exists src
  assert_stage_does_not_exist dest

  stages_page
    .modify(src)
    .set_name(dest)
    .save
end
update(name, data) click to toggle source

Update stage configuration data for an existing stage

# File lib/admin_module/stages.rb, line 40
def update name, data
  stages_page
    .modify(name)
    .set_stage_data(data)
    .save
end

Private Instance Methods

assert_stage_does_not_exist(name) click to toggle source
# File lib/admin_module/stages.rb, line 155
def assert_stage_does_not_exist name
  fail ArgumentError.new("A stage named '#{name}' already exists") if list.include? name
end
assert_stage_exists(name) click to toggle source
# File lib/admin_module/stages.rb, line 151
def assert_stage_exists name
  fail ArgumentError.new("A stage named '#{name}' does not exist") unless list.include? name
end
stages_page() click to toggle source
# File lib/admin_module/stages.rb, line 147
def stages_page
  page_factory.stages_page
end
valid_stage_data?(data) click to toggle source

Test stage data structure for validity

Required: name

# File lib/admin_module/stages.rb, line 164
def valid_stage_data? data
  if !data.key?(:name) || data[:name].nil? || data[:name].empty?
    return false
  end
  true
end