Class: RbRotate::State

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

Overview

Represents state file.

Constant Summary collapse

@@self =

Holds self-instance as singleton.

nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

Constructor.



44
45
46
# File 'lib/rb.rotate/state.rb', line 44

def initialize
    @path = self.configuration.paths[:"state file"]
end

Class Method Details

.archiveObject

Alias for #archive.



72
73
74
# File 'lib/rb.rotate/state.rb', line 72

def self.archive
    self::get.archive
end

.each_file(&block) ⇒ Object

Alias for #each_file.



88
89
90
# File 'lib/rb.rotate/state.rb', line 88

def self.each_file(&block)
    self::get.each_file(&block)
end

.filesObject

Alias for #files.



80
81
82
# File 'lib/rb.rotate/state.rb', line 80

def self.files
    self::get.files
end

.getObject

Returns self instance.



52
53
54
55
56
57
58
# File 'lib/rb.rotate/state.rb', line 52

def self.get
    if @@self.nil?
        @@self = self::new
    end
    
    return @@self
end

.save!Object

Saves the file. (Shortcut to instance.)



64
65
66
# File 'lib/rb.rotate/state.rb', line 64

def self.save!
    self::get.save!
end

Instance Method Details

#archiveObject

Returns archive accessor instance.



147
148
149
150
151
152
153
# File 'lib/rb.rotate/state.rb', line 147

def archive
    if @archive.nil?
        @archive = StateModule::Archive::new(self.data[:archive])
    end
    
    return @archive
end

#compact!Object

Compacts the file specifications. It removes all empty entries records.



168
169
170
171
172
# File 'lib/rb.rotate/state.rb', line 168

def compact!
    self.files.reject! do |key, value|
        value.empty?
    end
end

#configurationObject

Returns configuration object instance.



193
194
195
# File 'lib/rb.rotate/state.rb', line 193

def configuration
    Configuration::get
end

#create!Object

Creates new storage.



127
128
129
130
# File 'lib/rb.rotate/state.rb', line 127

def create!
    @data = self.new
    self.save! 
end

#dataObject

Returns data array.



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/rb.rotate/state.rb', line 96

def data
    if @data.nil?
        if not ::File.exists? @path
            self.create!
        else
            @data = YAML.load(::File.read(@path))
        end
    end
    
    return @data
end

#each_fileObject

Traverses through all files and emits path and StateModule::File objects.



202
203
204
205
206
207
208
# File 'lib/rb.rotate/state.rb', line 202

def each_file
    self.files.each_pair do |path, data|
        if not data.empty?
            yield path, StateModule::File::new(path, data)
        end
    end
end

#file(path) ⇒ Object

Returns record for appropriate file.



178
179
180
181
182
183
184
185
186
187
# File 'lib/rb.rotate/state.rb', line 178

def file(path)
    data = self.files[path.to_sym]
    
    if data.nil?
        data = { }
        self.files[path.to_sym] = data
    end
    
    StateModule::File::new(path, data)
end

#filesObject

Returns files list.



159
160
161
# File 'lib/rb.rotate/state.rb', line 159

def files
    self.data[:files]
end

#newObject

Formats new storage.



112
113
114
115
116
117
118
119
120
121
# File 'lib/rb.rotate/state.rb', line 112

def new
    Hash[
        :archive => {
            :files => { },
            :directories => { }
        },
        
        :files => { },
    ]
end

#save!Object

Saves the file.



136
137
138
139
140
141
# File 'lib/rb.rotate/state.rb', line 136

def save!
    self.compact!
    ::File.open(@path, "w") do |io|
        io.write(self.data.to_yaml)
    end
end