class AdminModule::Locks

Attributes

page_factory[R]

Public Class Methods

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

Public Instance Methods

create(lock) click to toggle source
# File lib/admin_module/locks.rb, line 35
def create lock
  lock_name = assert_lock_does_not_exist( extract_lock_name(lock) )

  locks_page
    .add
    .set_lock_data(lock)
    .save
end
export(file_path) click to toggle source

No functionality exists to DELETE locks.

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

  locks.each do |lock|
    export_data[lock] = read lock
  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_create = false) click to toggle source

Import lock configurations into the current environment from a file.

# File lib/admin_module/locks.rb, line 88
def import file_path, allow_create = false
  raise IOError, "File not found: #{file_path}" unless File.exists?(file_path)

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

  existing_locks = list

  locks.each do |name, data|
    if existing_locks.include?(name)
      update(data)
    else
      if allow_create
        create(data)
      else
        puts "Unable to create #{name}. allow_create = false"
      end
    end
  end
end
list() click to toggle source
# File lib/admin_module/locks.rb, line 31
def list
  locks_page.get_locks
end
read(lock) click to toggle source
# File lib/admin_module/locks.rb, line 44
def read lock
  lock_name = assert_lock_exists( extract_lock_name(lock) )

  locks_page
    .modify( lock_name )
    .get_lock_data
end
rename(src, dest) click to toggle source
# File lib/admin_module/locks.rb, line 21
def rename src, dest
  src = assert_lock_exists( extract_lock_name(src) )
  dest = assert_lock_does_not_exist( extract_lock_name(dest) )

  locks_page
    .modify(src)
    .set_name(dest)
    .save
end
update(lock) click to toggle source
# File lib/admin_module/locks.rb, line 52
def update lock
  lock_name = assert_lock_exists( extract_lock_name(lock) )

  locks_page
    .modify( lock_name )
    .set_lock_data(lock)
    .save
end

Private Instance Methods

assert_lock_does_not_exist(lock_name) click to toggle source
# File lib/admin_module/locks.rb, line 134
def assert_lock_does_not_exist lock_name
  if list.include? lock_name
    fail ArgumentError.new("A lock named '#{lock_name}' already exists")
  end

  lock_name
end
assert_lock_exists(lock_name) click to toggle source
# File lib/admin_module/locks.rb, line 126
def assert_lock_exists lock_name
  unless list.include? lock_name
    fail ArgumentError.new("A lock named '#{lock_name}' does not exist")
  end

  lock_name
end
extract_lock_name(lock) click to toggle source
# File lib/admin_module/locks.rb, line 118
def extract_lock_name lock
  lock_name = if lock.is_a? Hash
                lock[:name]
              else
                String(lock)
              end
end
locks_page() click to toggle source
# File lib/admin_module/locks.rb, line 114
def locks_page
  page_factory.locks_page
end