Class: DamageControl::Visitor::YamlPersister

Inherits:
Object
  • Object
show all
Defined in:
lib/damagecontrol/visitor/yaml_persister.rb

Overview

Visitor that saves each ChangeSet in a folder with the name of each ChangeSet’s identifier.

Is also able to load changesets.

Instance Method Summary collapse

Constructor Details

#initialize(changesets_dir) ⇒ YamlPersister

Returns a new instance of YamlPersister.



14
15
16
# File 'lib/damagecontrol/visitor/yaml_persister.rb', line 14

def initialize(changesets_dir)
  @changesets_dir = changesets_dir
end

Instance Method Details

#identifiersObject

Returns a sorted array of Time or int representing the changeset directories.



65
66
67
68
69
70
71
# File 'lib/damagecontrol/visitor/yaml_persister.rb', line 65

def identifiers
  # This is pretty quick - even with a lot of directories.
  # TODO: the method is called 5 times for a page refresh!
  dirs = Dir["#{@changesets_dir}/*"].find_all {|f| File.directory?(f) && File.exist?("#{f}/changeset.yaml")}
  # Turn them into ints so they can be sorted.
  dirs.collect { |dir| File.basename(dir).to_identifier }.sort
end

#latest_identifierObject

Returns the identifier of the latest changeset.



75
76
77
# File 'lib/damagecontrol/visitor/yaml_persister.rb', line 75

def latest_identifier
  identifiers[-1]
end

#load_upto(last_changeset_identifier, prior) ⇒ Object

Loads prior number of changesets upto last_changeset_identifier. last_changeset_identifier should be the dirname of the folder containing the last changeset.



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/damagecontrol/visitor/yaml_persister.rb', line 39

def load_upto(last_changeset_identifier, prior)
  Log.info "Loading #{prior} changesets from #{@changesets_dir} (from #{last_changeset_identifier} and down)"
  last = identifiers.index(last_changeset_identifier)

  changesets = RSCM::ChangeSets.new
  if(last)
    first = last - prior + 1
    first = 0 if first < 0

    identifiers[first..last].each do |identifier|
      changeset_yaml = "#{@changesets_dir}/#{identifier.to_s}/changeset.yaml"
      Log.info "Loading changesets from #{changeset_yaml}"
      begin
        changesets.add(YAML::load_file(changeset_yaml))
      rescue Exception => e
        # Sometimes the yaml files get corrupted
        Log.error "Error loading changesets file: #{File.expand_path(changeset_yaml)}"
        # Todo: delete it and schedule it for re-retrieval somehow.
      end
    end
  end
  changesets
end

#visit_change(change) ⇒ Object



30
31
# File 'lib/damagecontrol/visitor/yaml_persister.rb', line 30

def visit_change(change)
end

#visit_changeset(changeset) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/damagecontrol/visitor/yaml_persister.rb', line 21

def visit_changeset(changeset)
  changeset_file = "#{@changesets_dir}/#{changeset.identifier.to_s}/changeset.yaml"
  dir = File.dirname(changeset_file)
  FileUtils.mkdir_p(dir)
  File.open(changeset_file, "w") do |io|
    YAML::dump(changeset, io)
  end
end

#visit_changesets(changesets) ⇒ Object



18
19
# File 'lib/damagecontrol/visitor/yaml_persister.rb', line 18

def visit_changesets(changesets)
end