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.



56
57
58
59
60
61
62
# File 'lib/damagecontrol/visitor/yaml_persister.rb', line 56

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.



66
67
68
# File 'lib/damagecontrol/visitor/yaml_persister.rb', line 66

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
# File 'lib/damagecontrol/visitor/yaml_persister.rb', line 39

def load_upto(last_changeset_identifier, prior)
  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|
      changesets.add(YAML::load_file("#{@changesets_dir}/#{identifier.to_s}/changeset.yaml"))
    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