class Dump::Snapshot

Base class for dump

Attributes

path[R]

Public Class Methods

list(options = {}) click to toggle source
# File lib/dump/snapshot.rb, line 12
def self.list(options = {})
  dumps = Dir[File.join(Dump.rails_root, 'dump', options[:all] ? '*.*' : '*.tgz')].sort.select{ |path| File.file?(path) }.map{ |path| new(path) }
  dumps = dumps.select{ |dump| dump.name[options[:like]] } if options[:like]
  if options[:tags]
    tags = get_filter_tags(options[:tags])
    dumps = dumps.select{ |dump| (dump.tags & tags[:simple]).present? } if tags[:simple].present?
    dumps = dumps.select{ |dump| (dump.tags & tags[:mandatory]) == tags[:mandatory] } if tags[:mandatory].present?
    dumps = dumps.reject{ |dump| (dump.tags & tags[:forbidden]).present? } if tags[:forbidden].present?
  end
  dumps
end
new(path_or_options = {}) click to toggle source
# File lib/dump/snapshot.rb, line 24
def initialize(path_or_options = {})
  if path_or_options.is_a?(Hash)
    options = path_or_options

    name = Time.now.utc.strftime('%Y%m%d%H%M%S')

    description = clean_description(options[:desc])
    name += "-#{description}" unless description.blank?

    tags = clean_tags(options[:tags])
    name += "@#{tags * ','}" unless tags.empty?

    tgz_name = "#{name}.tgz"

    @path = options[:dir] ? Pathname(options[:dir]) + tgz_name : Pathname(tgz_name)

  else
    @path = Pathname(path_or_options)
  end
end

Public Instance Methods

==(other) click to toggle source
# File lib/dump/snapshot.rb, line 55
def ==(other)
  path == other.path
end
description() click to toggle source
# File lib/dump/snapshot.rb, line 78
def description
  clean_description(parts[:desc])
end
ext() click to toggle source
# File lib/dump/snapshot.rb, line 86
def ext
  parts[:ext]
end
human_size() click to toggle source
# File lib/dump/snapshot.rb, line 99
def human_size
  number = size
  return nil if number.nil?

  degree = 0
  symbols = %w[B K M G T]
  while number >= 1000 && degree < symbols.length - 1
    degree += 1
    number /= 1024.0
  end
  format('%.2f%s', number, symbols[degree])
end
inspect() click to toggle source
# File lib/dump/snapshot.rb, line 112
def inspect
  "#<#{self.class}:0x#{object_id} #{path}>"
end
lock() { || ... } click to toggle source
# File lib/dump/snapshot.rb, line 116
def lock
  lock = File.open(path, 'r')
  begin
    if lock.flock(File::LOCK_EX | File::LOCK_NB)
      yield
    end
  ensure
    lock.flock(File::LOCK_UN)
    lock.close
  end
rescue Errno::ENOENT
  nil
end
name() click to toggle source
# File lib/dump/snapshot.rb, line 90
def name
  @name ||= File.basename(path)
end
Also aliased as: to_s
parts() click to toggle source
# File lib/dump/snapshot.rb, line 59
def parts
  @parts ||= begin
    if (m = name.match(/^(\d{#{4 + 2 + 2 + 2 + 2 + 2}})(-[^@]+)?((?:@[^@]+)+)?\.(tmp|tgz)$/))
      {
        :time => m[1],
        :desc => m[2] && m[2][1, m[2].length],
        :tags => m[3] && m[3][1, m[3].length],
        :ext => m[4],
      }
    else
      {}
    end
  end
end
silence(&block) click to toggle source
# File lib/dump/snapshot.rb, line 130
def silence(&block)
  Rails.logger.silence(&block)
end
size() click to toggle source
# File lib/dump/snapshot.rb, line 95
def size
  File.size?(path)
end
tags() click to toggle source
# File lib/dump/snapshot.rb, line 82
def tags
  clean_tags(parts[:tags])
end
tgz_path() click to toggle source
# File lib/dump/snapshot.rb, line 47
def tgz_path
  path_with_ext('tgz')
end
time() click to toggle source
# File lib/dump/snapshot.rb, line 74
def time
  parts[:time] && Time.utc(*parts[:time].match(/(\d{4})#{'(\d{2})' * 5}/)[1..6])
end
tmp_path() click to toggle source
# File lib/dump/snapshot.rb, line 51
def tmp_path
  path_with_ext('tmp')
end
to_s()
Alias for: name

Protected Instance Methods

path_with_ext(ext) click to toggle source
# File lib/dump/snapshot.rb, line 150
def path_with_ext(ext)
  Pathname(path.to_s.sub(/#{parts[:ext]}$/, ext))
end