Class: MasterDelivery::MasterDelivery

Inherits:
Object
  • Object
show all
Defined in:
lib/master_delivery.rb

Overview

File delivery class

  1. Move the current active files to backup/

  2. Place a symbolic link to the master (or copy of master) in the appropriate directory

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(master_root, backup_root = '') ⇒ MasterDelivery

Returns a new instance of MasterDelivery.

Parameters:

  • master_root (String)

    Path to the dir including master dirs.

  • backup_root (String) (defaults to: '')

    Path to the dir in which backup masters are created.



30
31
32
33
34
35
36
37
# File 'lib/master_delivery.rb', line 30

def initialize(master_root, backup_root = '')
  @master_root = File.expand_path(master_root)
  @backup_root = if backup_root.nil? || backup_root.empty?
                   File.expand_path(master_root + '/backup')
                 else
                   File.expand_path(backup_root)
                 end
end

Instance Attribute Details

#backup_rootObject (readonly)

Returns the value of attribute backup_root.



27
28
29
# File 'lib/master_delivery.rb', line 27

def backup_root
  @backup_root
end

Instance Method Details

#confirm(basics, params) ⇒ Object

Parameters:

  • params (Hash)

    :type, :dryrun, :yes, :quiet



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/master_delivery.rb', line 76

def confirm(basics, params)
  unless params[:quiet]
    puts MSG_CONFIRMATION_INTRO unless params[:yes]
    print_params(basics, params.slice(:type, :dryrun))
    print_sample(basics)
  end
  print MSG_CONFIRMATION.chomp unless params[:yes] # use print instead of puts for '\n'
  return true if params[:yes] || gets.chomp == 'y'

  puts 'aborted.'
  false
end

#deliver(basics, type: :symbolic_link, dryrun: false, verbose: false) ⇒ Object

note: Even if dryrun: true, @backup_dir is actually created! (for name-consistency)

Parameters:

  • master_id (String)

    Top directory name of master

  • delivery_root (String)

    Files will be delivered to this prefix location. If this prefix is empty, it will be placed in the root directory. This mechanism saves you from having to unnecessarily deepen the directory hierarchy under master_id.

    Example of prefix

    master_id: MID
    master: $master_root/MID/a/b/readme.md
    delivery_root: /Users/xxx/yyy
    delivery: /Users/xxx/yyy/a/b/readme.md
    

    Example of no-prefix

    master_id: MID
    master: $master_root/MID/a/b/readme.md
    delivery_root: (empty)
    delivery: /a/b/readme.md
    
  • type (symbol) (defaults to: :symbolic_link)

    only link (:symbolic_link) or copy master (:regular_file)

  • dryrun (boolean) (defaults to: false)

    if set this false, FileUtils::DryRun will be used.



61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/master_delivery.rb', line 61

def deliver(basics, type: :symbolic_link, dryrun: false, verbose: false)
  FileUtils.mkdir_p(@backup_root)
  utils = dryrun ? FileUtils::DryRun : FileUtils

  backup_dir = Dir.mktmpdir("#{basics[:master_id]}-original-", @backup_root)
  puts "mkdir -p #{backup_dir}" if verbose
  mfiles = master_files(basics[:master_id])
  mfiles.each do |master|
    tfile = move_to_backup(master, utils, basics, backup_dir, verbose)
    deliver_to_target(master, utils, tfile, type, verbose)
  end
  [mfiles, backup_dir]
end