Class: RbRotate::StorageModule::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/rb.rotate/storage/item.rb

Overview

 Represents an item of some entry in storage.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(entry, identifier = nil, path = nil) ⇒ Item

Constructor.



45
46
47
48
49
50
51
52
# 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

Instance Attribute Details

#identifierObject

Returns identifier.



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# 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

Instance Method Details

#allocate(method) ⇒ Object

Allocates new record.



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# 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) ⇒ Object

Appends to item.



294
295
296
297
298
299
300
301
302
303
304
# 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!Object

Compress the file.



323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
# 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?Boolean

Indicates file is or file should be compressed.

Returns:

  • (Boolean)


310
311
312
313
314
315
316
317
# 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

#compressionObject

Describes compression.



370
371
372
# File 'lib/rb.rotate/storage/item.rb', line 370

def compression
    @data[:compression]
end

#created_atObject

Returns the creation date.



378
379
380
# File 'lib/rb.rotate/storage/item.rb', line 378

def created_at
    @data[:date]
end

#decompress!Object

Decompress file.



358
359
360
361
362
363
364
# 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?Boolean

Indicates, item still exists in storage.

Returns:

  • (Boolean)


114
115
116
# File 'lib/rb.rotate/storage/item.rb', line 114

def exists?
    ::File.exists? self.path
end

#expiration_atObject

Returns the expiration date.



386
387
388
389
390
391
392
# 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?Boolean

Indicates, item is expired.

Returns:

  • (Boolean)


398
399
400
401
402
403
404
405
406
407
408
409
410
411
# 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

#load_data!Object

Returns data.



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# 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!Object

 Mails the file.



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
# 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

#pathObject

Returns path.



198
199
200
201
202
203
204
# File 'lib/rb.rotate/storage/item.rb', line 198

def path
    if @path.nil?
        self.rebuild_path!
    end
    
    return @path
end

#prepare_directory!Object

Prepares directory.



258
259
260
261
# 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!Object

Rebuilds path.



226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# 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!Object

Registers itself.



122
123
124
# File 'lib/rb.rotate/storage/item.rb', line 122

def register!
    State::archive.register_file(self.path, @data)
end

#remove!Object

Removes itself.



138
139
140
141
142
143
144
145
146
147
# 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!Object

Rotates itself.



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# 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

#stateObject

Returns state object.



106
107
108
# File 'lib/rb.rotate/storage/item.rb', line 106

def state
    @entry.file.state
end

#target_pathObject

Generates target (without compression extension) path from path.



211
212
213
214
215
216
217
218
219
220
# 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!Object

Unregisters itself.



130
131
132
# File 'lib/rb.rotate/storage/item.rb', line 130

def unregister!
    State::archive.unregister_file(self.path)
end