class Fingerprint::RecordSet

Attributes

configuration[R]
keys[R]
paths[R]
records[R]

Public Class Methods

load(io) click to toggle source
# File lib/fingerprint/record.rb, line 94
def self.load(io)
        self.new.tap{|record_set| record_set.parse(io)}
end
load_file(path) click to toggle source
# File lib/fingerprint/record.rb, line 88
def self.load_file(path)
        File.open(path, "r") do |io|
                self.load(io)
        end
end
new() click to toggle source
# File lib/fingerprint/record.rb, line 98
def initialize
        @records = []
        @paths = {}
        @keys = {}
        
        @configuration = nil

        @callback = nil
end
parse(input) { |mode, path, metadata| ... } click to toggle source
# File lib/fingerprint/record.rb, line 198
def self.parse(input)
        mode = nil
        path = nil
        metadata = nil

        markers = {}
        MODES.each do |key, value|
                markers[value] = key
        end

        # Parse original fingerprint
        input.each_line do |line|
                # Skip comments and blank lines
                next if line.match(/^\s*#/) || line.match(/^\s*$/)

                if line.match(/^([A-Z])\s+(.*)$/)
                        if path
                                yield mode, path, metadata
                        end

                        mode = markers[$1] || :unknown

                        path = $2
                        metadata = {}
                elsif line.match(/^\s+([a-zA-Z\.0-9]+)\s+(.*)$/)
                        metadata[$1] = $2
                else
                        $stderr.puts "Unhandled line: #{line}"
                end
        end

        if path
                yield mode, path, metadata
        end
end

Public Instance Methods

<<(record) click to toggle source
# File lib/fingerprint/record.rb, line 114
def <<(record)
        @records << record
        if record.mode == :configuration
                # What should we do if we get multiple configurations?
                @configuration = record
        else
                @paths[record.path] = record
                record.keys.each do |key|
                        @keys[key] ||= {}
                        
                        @keys[key][record[key]] = record
                end
        end
end
compare(other) click to toggle source
# File lib/fingerprint/record.rb, line 161
def compare(other)
        main = lookup(other.path)

        # Did we find a corresponding other at the same path?
        if main
                # Keep track of how many keys were checked..
                checked = 0

                # Are all the keys of the other record equivalent to the main record?
                other.keys.each do |key|
                        if main[key]
                                checked += 1

                                # Is the key the same?
                                if main[key] != other[key]
                                        return :keys_different, "Key #{key.gsub(/^key\./, '')} does not match"
                                end
                        end
                end

                # Are the records the same size? We put this check second because we do this as a last resort to
                # ensure that the file hasn't been deliberately tampered with.
                if main.metadata['size'] and other.metadata['size'] and main.metadata['size'] != other.metadata['size']
                        return :size_different, "File size differs"
                end

                if checked == 0
                        return :no_keys, "No valid keys to check"
                else
                        # At least one key could be validated.
                        return :valid, "Valid"
                end
        else
                return :not_found, "File not found"
        end
end
empty?() click to toggle source
# File lib/fingerprint/record.rb, line 133
def empty?
        @paths.empty?
end
find(record) click to toggle source
# File lib/fingerprint/record.rb, line 153
def find(record)
        result = lookup(record.path)

        return result if result

        return find_by_key(record)
end
find_by_key(record) click to toggle source
# File lib/fingerprint/record.rb, line 141
def find_by_key(record)
        record.keys.each do |key|
                value = record[key]
                
                result = @keys[key][value]

                return result if result
        end

        return nil
end
include?(path) click to toggle source
# File lib/fingerprint/record.rb, line 129
def include?(path)
        @paths.include?(path)
end
lookup(path) click to toggle source
# File lib/fingerprint/record.rb, line 137
def lookup(path)
        return @paths[path]
end
parse(input) click to toggle source
# File lib/fingerprint/record.rb, line 234
def parse(input)
        self.class.parse(input) do |mode, path, metadata|
                self << Record.new(mode, path, metadata)
        end
end
write(output) click to toggle source
# File lib/fingerprint/record.rb, line 240
def write(output)
        @records.each do |record|
                record.write(output)
        end
end