Class: Piston::WorkingCopy

Inherits:
Object
  • Object
show all
Defined in:
lib/piston/working_copy.rb

Direct Known Subclasses

Git::WorkingCopy, Svn::WorkingCopy

Defined Under Namespace

Classes: NotWorkingCopy, UnhandledWorkingCopy

Constant Summary collapse

@@handlers =
Array.new

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ WorkingCopy

Returns a new instance of WorkingCopy.



36
37
38
39
# File 'lib/piston/working_copy.rb', line 36

def initialize(path)
  @path = path.kind_of?(Pathname) ? path : Pathname.new(path)
  logger.debug {"Initialized on #{@path}"}
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



34
35
36
# File 'lib/piston/working_copy.rb', line 34

def path
  @path
end

Class Method Details

.add_handler(handler) ⇒ Object



24
25
26
# File 'lib/piston/working_copy.rb', line 24

def add_handler(handler)
  @@handlers << handler
end

.guess(path) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/piston/working_copy.rb', line 11

def guess(path)
  path = path.kind_of?(Pathname) ? path : Pathname.new(path.to_s)
  logger.info {"Guessing the working copy type of #{path.inspect}"}
  handler = handlers.detect do |handler|
    logger.debug {"Asking #{handler.name} if it understands #{path}"}
    handler.understands_dir?(path)
  end

  raise UnhandledWorkingCopy, "Don't know what working copy type #{path} is." if handler.nil?
  handler.new(path)
end

.handlersObject



28
29
30
# File 'lib/piston/working_copy.rb', line 28

def handlers
  @@handlers
end

.loggerObject



7
8
9
# File 'lib/piston/working_copy.rb', line 7

def logger
  @@logger ||= Log4r::Logger["handler"]
end

Instance Method Details

#after_remember(path) ⇒ Object

Callback after #remember is done, to do whatever the working copy needs to do with the file.



105
106
# File 'lib/piston/working_copy.rb', line 105

def after_remember(path)
end

#copy_from(revision) ⇒ Object

Copy files from revision. revision must #respond_to?(:each), and return each file that is to be copied. Only files must be returned.

Each item yielded by Revision#each must be a relative path.

WorkingCopy will call Revision#copy_to with the full path to where the file needs to be copied.



75
76
77
78
79
80
81
82
83
# File 'lib/piston/working_copy.rb', line 75

def copy_from(revision)
  revision.each do |relpath|
    target = path + relpath
    target.dirname.mkdir rescue nil

    logger.debug {"Copying #{relpath} to #{target}"}
    revision.copy_to(relpath, target)
  end
end

#createObject

Creates the initial working copy for pistonizing a new repository.



63
64
65
# File 'lib/piston/working_copy.rb', line 63

def create
  logger.debug {"Creating working copy at #{path}"}
end

#exist?Boolean

Returns:

  • (Boolean)


49
50
51
# File 'lib/piston/working_copy.rb', line 49

def exist?
  @path.exist? && @path.directory?
end

#finalizeObject



113
114
115
# File 'lib/piston/working_copy.rb', line 113

def finalize
  logger.debug {"Finalizing #{path}"}
end

#infoObject

Returns basic information about this working copy.



118
119
120
# File 'lib/piston/working_copy.rb', line 118

def info
  recall
end

#loggerObject



41
42
43
# File 'lib/piston/working_copy.rb', line 41

def logger
  self.class.logger
end

#pistonized?Boolean

Returns:

  • (Boolean)


53
54
55
# File 'lib/piston/working_copy.rb', line 53

def pistonized?
  yaml_path.exist? && yaml_path.file?
end

#recallObject

Recalls a Hash of values from the working copy.



109
110
111
# File 'lib/piston/working_copy.rb', line 109

def recall
  YAML.load(File.read(yaml_path))
end

#remember(values, handler_values) ⇒ Object

Stores a Hash of values that can be retrieved later.



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/piston/working_copy.rb', line 86

def remember(values, handler_values)
  values["format"] = 1

  # Stringify keys
  values.keys.each do |key|
    values[key.to_s] = values.delete(key)
  end

  logger.debug {"Remembering #{values.inspect} as well as #{handler_values.inspect}"}
  File.open(yaml_path, "wb") do |f|
    f.write(values.merge("handler" => handler_values).to_yaml)
  end

  logger.debug {"Calling \#after_remember on #{yaml_path}"}
  after_remember(yaml_path)
end

#to_sObject



45
46
47
# File 'lib/piston/working_copy.rb', line 45

def to_s
  "Piston::WorkingCopy(#{@path})"
end

#validate!Object

Raises:



57
58
59
60
# File 'lib/piston/working_copy.rb', line 57

def validate!
  raise NotWorkingCopy unless self.pistonized?
  self
end