class RbRotate::StorageModule::Item
Represents an item of some entry in storage.
Attributes
identifier[W]
Public Class Methods
new(entry, identifier = nil, path = nil)
click to toggle source
Constructor.
# File lib/rb.rotate/storage/item.rb, line 45 def initialize(entry, identifier = nil, path = nil) @entry = entry @identifier = identifier @path = path # Loads data self.load_data! end
Public Instance Methods
allocate(method)
click to toggle source
Allocates new record.
# File lib/rb.rotate/storage/item.rb, line 267 def allocate(method) # Prepares directory self.prepare_directory! # Allocates by required action case method when :copy FileUtils.copy(@entry.file.path, self.path) when :move FileUtils.move(@entry.file.path, self.path) when :append self.append!(:"no compress") else raise Exception::new("Invalid allocating method.") end self.compress! self.register! return self end
append!(compress = :compress)
click to toggle source
Appends to item.
# File lib/rb.rotate/storage/item.rb, line 294 def append!(compress = :compress) self.decompress! ::File.open(self.path, "a") do |io| io.write(::File.read(@entry.file.path)) end if compress == :compress self.compress! end end
compress!()
click to toggle source
Compress the file.
# File lib/rb.rotate/storage/item.rb, line 323 def compress! # Checking out configuration configuration = @entry.storage.directory.configuration command, extension = configuration[:compress] decompress = configuration[:decompress] if not command.kind_of? FalseClass # Setting file settings according to current # configuration parameters if command.kind_of? TrueClass command = "gzip --best" extension = "gz" end if decompress.kind_of? TrueClass decompress = "gunzip" end @data[:compression] = { :decompress => decompress, :extension => extension } # Compress system(command.dup << " " << self.path) self.rebuild_path! end end
compressed?()
click to toggle source
Indicates file is or file should be compressed.
# File lib/rb.rotate/storage/item.rb, line 310 def compressed? result = @data[:compression] if result.kind_of? Array result = true end return result end
compression()
click to toggle source
Describes compression.
# File lib/rb.rotate/storage/item.rb, line 370 def compression @data[:compression] end
created_at()
click to toggle source
Returns the creation date.
# File lib/rb.rotate/storage/item.rb, line 378 def created_at @data[:date] end
decompress!()
click to toggle source
Decompress file.
# File lib/rb.rotate/storage/item.rb, line 358 def decompress! if self.compressed? and self.exists? command = self.compression[:decompress] system(command.dup << " " << self.path << " 2> /dev/null") FileUtils.move(self.target_path, self.path) end end
exists?()
click to toggle source
Indicates, item still exists in storage.
# File lib/rb.rotate/storage/item.rb, line 114 def exists? ::File.exists? self.path end
expiration_at()
click to toggle source
Returns the expiration date.
# File lib/rb.rotate/storage/item.rb, line 386 def expiration_at configuration = @entry.storage.directory.configuration period = configuration[:period].to_seconds multiplier = configuration[:rotate] return self.created_at + (period * multiplier) end
expired?()
click to toggle source
Indicates, item is expired.
# File lib/rb.rotate/storage/item.rb, line 398 def expired? recycle = @entry.storage.directory.configuration[:recycle] if recycle.kind_of? FalseClass result = false else recycle = recycle.to_sym end if recycle and (recycle == :remove) or (recycle == :mail) result = self.expiration_at < Time::now end return result end
identifier()
click to toggle source
Returns identifier.
# File lib/rb.rotate/storage/item.rb, line 174 def identifier if @identifier.nil? if @entry.storage.numeric_identifier? @identifier = 1 else item_identifier = @entry.storage.item_identifier if item_identifier.to_sym == :date format = "%Y%m%d.%H%M" else format = item_identifier end @identifier = Time::now.strftime(format) end end return @identifier end
load_data!()
click to toggle source
Returns data.
# File lib/rb.rotate/storage/item.rb, line 58 def load_data! if @data.nil? if not @path.nil? @data = State::get.archive.file(@path) end # Default if @path.nil? or @data.nil? @data = { :date => Time::now, :compression => false } end end end
mail!()
click to toggle source
Mails the file.
# File lib/rb.rotate/storage/item.rb, line 153 def mail! to = @entry.storage.directory.configuration[:mail] self.decompress! require "etc" require "socket" Mail::send( :from => Etc.getlogin.dup << "@" << Socket.gethostname, :to => to, :subject => Socket.gethostname << " : log : " << self.path, :body => ::File.read(self.target_path) ) self.compress! end
path()
click to toggle source
Returns path.
# File lib/rb.rotate/storage/item.rb, line 198 def path if @path.nil? self.rebuild_path! end return @path end
prepare_directory!()
click to toggle source
Prepares directory.
# File lib/rb.rotate/storage/item.rb, line 258 def prepare_directory! directory = FileUtils.mkdir_p(::File.dirname(self.path)).first State::archive.register_directory(directory) end
rebuild_path!()
click to toggle source
Rebuilds path.
# File lib/rb.rotate/storage/item.rb, line 226 def rebuild_path! directory = @entry.storage.directory configuration = directory.configuration @path = configuration[:storage].dup << "/" # Adds archive subdirectories structure if necessary recursive = configuration[:recursive] if (recursive.kind_of? TrueClass) or (configuration[:recursive].to_sym != :flat) relative_path = directory.relative_path if relative_path != ?. @path << relative_path << "/" end end # Adds filename @path << self.state.name.to_s << "." << self.identifier.to_s # Adds extension if necessary if not self.state.extension.nil? @path << "." << self.state.extension end # Adds compression extension if necessary if self.compressed? @path << "." << self.compression[:extension] end end
register!()
click to toggle source
Registers itself.
# File lib/rb.rotate/storage/item.rb, line 122 def register! State::archive.register_file(self.path, @data) end
remove!()
click to toggle source
Removes itself.
# File lib/rb.rotate/storage/item.rb, line 138 def remove! self.unregister! # Eventually mails it if required if @entry.storage.directory.configuration[:recycle].to_sym == :mail self.mail! end FileUtils.remove(self.path) end
rotate!()
click to toggle source
Rotates itself.
# File lib/rb.rotate/storage/item.rb, line 78 def rotate! if @entry.storage.numeric_identifier? and (self.identifier.kind_of? Numeric) ## # Unregisters old filename, increases counter # and register it again. # self.unregister! if self.exists? old_path = self.path self.identifier += 1 self.rebuild_path! self.prepare_directory! FileUtils.move(old_path, self.path) self.register! end end return self end
state()
click to toggle source
Returns state object.
# File lib/rb.rotate/storage/item.rb, line 106 def state @entry.file.state end
target_path()
click to toggle source
Generates target (without compression extension) path from path.
# File lib/rb.rotate/storage/item.rb, line 211 def target_path if self.compressed? extension = self.compression[:extension] result = self.path[0...-(extension.length + 1)] else result = self.path end return result end
unregister!()
click to toggle source
Unregisters itself.
# File lib/rb.rotate/storage/item.rb, line 130 def unregister! State::archive.unregister_file(self.path) end