Class: Fingerprint::RecordSet

Inherits:
Object
  • Object
show all
Defined in:
lib/fingerprint/record.rb

Direct Known Subclasses

SparseRecordSet

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRecordSet

Returns a new instance of RecordSet.



98
99
100
101
102
103
104
105
106
# File 'lib/fingerprint/record.rb', line 98

def initialize
  @records = []
  @paths = {}
  @keys = {}
  
  @configuration = nil

  @callback = nil
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



112
113
114
# File 'lib/fingerprint/record.rb', line 112

def configuration
  @configuration
end

#keysObject (readonly)

Returns the value of attribute keys.



110
111
112
# File 'lib/fingerprint/record.rb', line 110

def keys
  @keys
end

#pathsObject (readonly)

Returns the value of attribute paths.



109
110
111
# File 'lib/fingerprint/record.rb', line 109

def paths
  @paths
end

#recordsObject (readonly)

Returns the value of attribute records.



108
109
110
# File 'lib/fingerprint/record.rb', line 108

def records
  @records
end

Class Method Details

.load(io) ⇒ Object



94
95
96
# File 'lib/fingerprint/record.rb', line 94

def self.load(io)
  self.new.tap{|record_set| record_set.parse(io)}
end

.load_file(path) ⇒ Object



88
89
90
91
92
# File 'lib/fingerprint/record.rb', line 88

def self.load_file(path)
  File.open(path, "r") do |io|
    self.load(io)
  end
end

.parse(input) ⇒ Object



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/fingerprint/record.rb', line 198

def self.parse(input)
  mode = nil
  path = nil
   = 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, 
      end

      mode = markers[$1] || :unknown

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

  if path
    yield mode, path, 
  end
end

Instance Method Details

#<<(record) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
# 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) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# 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.['size'] and other.['size'] and main.['size'] != other.['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?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/fingerprint/record.rb', line 133

def empty?
  @paths.empty?
end

#find(record) ⇒ Object



153
154
155
156
157
158
159
# 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) ⇒ Object



141
142
143
144
145
146
147
148
149
150
151
# 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) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/fingerprint/record.rb', line 129

def include?(path)
  @paths.include?(path)
end

#lookup(path) ⇒ Object



137
138
139
# File 'lib/fingerprint/record.rb', line 137

def lookup(path)
  return @paths[path]
end

#parse(input) ⇒ Object



234
235
236
237
238
# File 'lib/fingerprint/record.rb', line 234

def parse(input)
  self.class.parse(input) do |mode, path, |
    self << Record.new(mode, path, )
  end
end

#write(output) ⇒ Object



240
241
242
243
244
# File 'lib/fingerprint/record.rb', line 240

def write(output)
  @records.each do |record|
    record.write(output)
  end
end