Class: RbRotate::Storage

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

Overview

Represents storage for archived files.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(directory) ⇒ Storage

Constructor.



26
27
28
# File 'lib/rb.rotate/storage.rb', line 26

def initialize(directory)
    @directory = directory
end

Instance Attribute Details

#directoryObject (readonly)

Returns the value of attribute directory.



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

def directory
  @directory
end

Class Method Details

.each_directoryObject

Traverses through all directories in storage.



201
202
203
204
205
# File 'lib/rb.rotate/storage.rb', line 201

def self.each_directory
    Configuration::each_directory do |directory|
        yield self::get(directory)
    end
end

.each_entryObject

Traverses through all entries.



187
188
189
190
191
192
193
194
195
# File 'lib/rb.rotate/storage.rb', line 187

def self.each_entry
    State::each_file do |path, state|
        file = File::new(path)
        storage = self::new(file.directory)
        entry = StorageModule::Entry::new(storage, file)
        
        yield entry
    end
end

.each_item(&block) ⇒ Object

Traverses through all items in global storage.



177
178
179
180
181
# File 'lib/rb.rotate/storage.rb', line 177

def self.each_item(&block)
    self.each_entry do |entry|
        entry.each_item(&block)
    end
end

.get(directory) ⇒ Object

Alias for new.



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

def self.get(directory)
    self::new(directory)
end

.put(file) ⇒ Object

Creates storage according to file and puts it to it.



42
43
44
# File 'lib/rb.rotate/storage.rb', line 42

def self.put(file)
    self::get(file.directory).put(file)
end

.remove_orphans!Object

Removes orphans.



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/rb.rotate/storage.rb', line 127

def self.remove_orphans!
    self::each_entry do |entry|
        items_count = 0
        
        entry.each_item do |item|
            if item.expired?
                item.remove!
            elsif not item.exists?
                item.unregister!
            else
                items_count += 1
            end
        end
        
        file = entry.file
        if (not file.exists?) and (items_count <= 0)
            file.state.destroy!
        end                    
    end
end

Instance Method Details

#cleanup!Object

Cleanups expired items from the storage.



101
102
103
104
105
# File 'lib/rb.rotate/storage.rb', line 101

def cleanup!
    self.each_entry do |entry|
        entry.cleanup!
    end
end

#compressed?Boolean

Indicates, storage is compressed.

Returns:

  • (Boolean)


58
59
60
# File 'lib/rb.rotate/storage.rb', line 58

def compressed?
    self.directory.compressable?
end

#do_actions!(file) ⇒ Object

Runs file actions.



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/rb.rotate/storage.rb', line 66

def do_actions!(file)
    entry = StorageModule::Entry::new(self, file)

    # Loads them
    actions = @directory.configuration[:action].split("+")
    actions.map! do |i| 
        i.strip!
        k, v = i.split(":", 2)
        [k.to_sym, v]
    end
    
    # Does them
    variables = { }
    
    actions.each do |action, arguments|
        case action
            when :move, :copy, :append
                variables[:filepath] = entry.put! action
            when :remove
                variables[:filepath] = file.remove!
            when :create, :truncate
                variables[:filepath] = file.create!
            when :mail
                variables[:filepath] = entry.mail! arguments
            when :hook
                name, arguments = arguments.split(":", 2)
                variables.merge! Hook::new(name.to_sym, arguments, variables).run!
        end
    end
end

#each_entryObject

Traverses through all entries of this directory storage.



162
163
164
165
166
167
168
169
170
171
# File 'lib/rb.rotate/storage.rb', line 162

def each_entry
    State::each_file do |path, state|
        if state.directory == self.directory.identifier
            file = File::new(path)
            entry = StorageModule::Entry::new(self, file)
            
            yield entry
        end
    end
end

#each_item(&block) ⇒ Object

Traverses through each item in current storage.



152
153
154
155
156
# File 'lib/rb.rotate/storage.rb', line 152

def each_item(&block)
    self.each_entry do |entry|
        entry.each_item(&block)
    end
end

#item_identifierObject

Returns item identifier of the storage.



111
112
113
# File 'lib/rb.rotate/storage.rb', line 111

def item_identifier
    self.directory.configuration[:identifier]
end

#numeric_identifier?Boolean

Indicates numeric identifier.

Returns:

  • (Boolean)


119
120
121
# File 'lib/rb.rotate/storage.rb', line 119

def numeric_identifier?
    self.item_identifier.to_sym == :numeric
end

#put(file) ⇒ Object

 Puts file to storage.



50
51
52
# File 'lib/rb.rotate/storage.rb', line 50

def put(file)
    self.do_actions! file
end