Class: RbRotate::StorageModule::Entry

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

Overview

Represents an entry of the storage. Entry is archived items of one file.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(storage, file) ⇒ Entry

Constructor.



34
35
36
37
# File 'lib/rb.rotate/storage/entry.rb', line 34

def initialize(storage, file)
    @storage = storage
    @file = file
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



21
22
23
# File 'lib/rb.rotate/storage/entry.rb', line 21

def file
  @file
end

#storageObject (readonly)

Returns the value of attribute storage.



28
29
30
# File 'lib/rb.rotate/storage/entry.rb', line 28

def storage
  @storage
end

Instance Method Details

#cleanup!Object

Cleanups the items.



75
76
77
78
79
80
81
# File 'lib/rb.rotate/storage/entry.rb', line 75

def cleanup!
    self.each_item do |item|
        if item.expired?
            item.remove!
        end
    end
end

#each_itemObject

Traverses through all items.



112
113
114
115
116
# File 'lib/rb.rotate/storage/entry.rb', line 112

def each_item
    self.file.state.items.each_pair do |identifier, path|
        yield Item::new(self, identifier, path)
    end
end

#mail!(to) ⇒ Object

Mail file to specified address.



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rb.rotate/storage/entry.rb', line 87

def mail!(to)
    if to.nil?
        to = @storage.directory.configuration[:mail]
    end
    if to.nil?
        raise Exception("No e-mail address specified for sending log to.")
    end
    
    require "etc"
    require "socket"

    Mail::send(
        :from => Etc.getlogin.dup << "@" << Socket.gethostname,
        :to => to,
        :subject => Socket.gethostname.dup << " : log : " << self.file.path,
        :body => ::File.read(self.file.path)
    )
    
    return self.file.path
end

#put!(method) ⇒ Object

Puts current version of the file to items.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/rb.rotate/storage/entry.rb', line 43

def put!(method)

    # If necessary, creates the state record
    if not self.file.state.exists?
        self.file.state.create(@file)
    end

    # Rotates other items
    new_list = { }
    self.each_item do |item|
        if item.exists?
            item.rotate!
            new_list[item.identifier] = item.path
        else
            item.unregister!
        end
    end
    
    # Puts new item
    item = Item::new(self).allocate(method)
    new_list[item.identifier] = item.path
    
    self.file.state.touch!
    self.file.state.items = new_list
    
    return self.file.path
end