class AdminModule::Snapshots

Attributes

page_factory[R]

Public Class Methods

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

Public Instance Methods

export(file_path) click to toggle source
# File lib/admin_module/snapshots.rb, line 53
def export file_path
  defns = list
  export_data = {}

  defns.each do |defn|
    export_data[defn] = read defn
  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) click to toggle source
# File lib/admin_module/snapshots.rb, line 33
def import file_path
  assert_file_exists file_path

  defns = {}
  File.open(file_path, 'r') do |f|
    # Read array of definition hashes
    defns = YAML.load(f)
  end

  existing_defns = list

  defns.each do |name, data|
    if existing_defns.include?(name)
      update(data)
    else
      create(data)
    end
  end
end
list() click to toggle source
# File lib/admin_module/snapshots.rb, line 19
def list
  snapshot_page.get_definitions
end
read(name) click to toggle source
# File lib/admin_module/snapshots.rb, line 73
def read name
  name = assert_definition_exists name

  snapshot_page
    .modify(name)
    .get_definition_data
end
rename(src, dest) click to toggle source
# File lib/admin_module/snapshots.rb, line 23
def rename src, dest
  src = assert_definition_exists src
  dest = assert_definition_does_not_exist dest

  snapshot_page
    .modify(src)
    .set_name(dest)
    .save
end

Private Instance Methods

assert_definition_does_not_exist(name) click to toggle source
# File lib/admin_module/snapshots.rb, line 95
def assert_definition_does_not_exist name
  if list.include? name
    fail ArgumentError.new("A data clearing definition named '#{name}' already exists")
  end

  name
end
assert_definition_exists(name) click to toggle source
# File lib/admin_module/snapshots.rb, line 87
def assert_definition_exists name
  unless list.include? name
    fail ArgumentError.new("A data clearing definition named '#{name}' does not exist")
  end

  name
end
assert_file_exists(file_path) click to toggle source
# File lib/admin_module/snapshots.rb, line 103
def assert_file_exists file_path
  raise IOError, "File not found: #{file_path}" unless File.exists?(file_path)
end
create(data) click to toggle source
# File lib/admin_module/snapshots.rb, line 116
def create data
  name = assert_definition_does_not_exist( extract_defn_name(data) )

  snapshot_page
    .add
    .set_definition_data(data)
    .save
end
extract_defn_name(data) click to toggle source
# File lib/admin_module/snapshots.rb, line 125
def extract_defn_name data
  name = if data.is_a? Hash
           data[:name]
         else
           String(data)
         end
end
snapshot_page() click to toggle source
# File lib/admin_module/snapshots.rb, line 83
def snapshot_page
  page_factory.snapshot_definitions_page
end
update(data) click to toggle source
# File lib/admin_module/snapshots.rb, line 107
def update data
  name = assert_definition_exists( extract_defn_name(data) )

  snapshot_page
    .modify(name)
    .set_definition_data(data)
    .save
end