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

#initialize(root = nil) ⇒ RecordSet

Returns a new instance of RecordSet.



105
106
107
108
109
110
111
112
113
114
# File 'lib/fingerprint/record.rb', line 105

def initialize(root = nil)
  @root = root
  @records = []
  @paths = {}
  @keys = {}
  
  @configuration = nil
  
  @callback = nil
end

Instance Attribute Details

#configurationObject (readonly)

Returns the value of attribute configuration.



122
123
124
# File 'lib/fingerprint/record.rb', line 122

def configuration
  @configuration
end

#keysObject (readonly)

Returns the value of attribute keys.



120
121
122
# File 'lib/fingerprint/record.rb', line 120

def keys
  @keys
end

#pathsObject (readonly)

Returns the value of attribute paths.



119
120
121
# File 'lib/fingerprint/record.rb', line 119

def paths
  @paths
end

#recordsObject (readonly)

Returns the value of attribute records.



118
119
120
# File 'lib/fingerprint/record.rb', line 118

def records
  @records
end

#rootObject (readonly)

Returns the value of attribute root.



116
117
118
# File 'lib/fingerprint/record.rb', line 116

def root
  @root
end

Class Method Details

.load_file(path) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/fingerprint/record.rb', line 92

def self.load_file(path)
  path = File.expand_path(path)
  root = File.dirname(path)
  
  record_set = self.new(root)
  
  File.open(path, "r") do |io|
    record_set.parse(io)
  end
  
  return record_set
end

.parse(input) ⇒ Object



218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
# File 'lib/fingerprint/record.rb', line 218

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
      raise "Unhandled line: #{line}"
    end
  end

  if path
    yield mode, path, 
  end
end

Instance Method Details

#<<(record) ⇒ Object



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

def <<(record)
  @records << record
  
  if record.mode == :configuration
    if @configuration
      raise "Multiple configurations detected!"
    end
    
    @configuration = record
    @root ||= @configuration.path
  else
    @paths[record.path] = record
    
    record.keys.each do |key|
      @keys[key] ||= {}
      
      @keys[key][record[key]] = record
    end
  end
end

#compare(other) ⇒ Object



181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/fingerprint/record.rb', line 181

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)


153
154
155
# File 'lib/fingerprint/record.rb', line 153

def empty?
  @paths.empty?
end

#find(record) ⇒ Object



173
174
175
176
177
178
179
# File 'lib/fingerprint/record.rb', line 173

def find(record)
  result = lookup(record.path)

  return result if result

  return find_by_key(record)
end

#find_by_key(record) ⇒ Object



161
162
163
164
165
166
167
168
169
170
171
# File 'lib/fingerprint/record.rb', line 161

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

#full_path(path) ⇒ Object



124
125
126
# File 'lib/fingerprint/record.rb', line 124

def full_path(path)
  Build::Files::Path.join(@root, path)
end

#include?(path) ⇒ Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/fingerprint/record.rb', line 149

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

#lookup(path) ⇒ Object



157
158
159
# File 'lib/fingerprint/record.rb', line 157

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

#parse(input) ⇒ Object



254
255
256
257
258
# File 'lib/fingerprint/record.rb', line 254

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

#write(output) ⇒ Object



260
261
262
263
264
# File 'lib/fingerprint/record.rb', line 260

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