Class: XRT::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/xrt/transaction.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTransaction



4
5
6
# File 'lib/xrt/transaction.rb', line 4

def initialize
  @files = {}
end

Instance Attribute Details

#filesObject (readonly)

Returns the value of attribute files.



3
4
5
# File 'lib/xrt/transaction.rb', line 3

def files
  @files
end

Instance Method Details

#add(path, content) ⇒ Object



43
44
45
# File 'lib/xrt/transaction.rb', line 43

def add(path, content)
  @files[path.to_s] = content
end

#commit!Object



35
36
37
38
39
40
41
# File 'lib/xrt/transaction.rb', line 35

def commit!
  files.each_pair{|path, content|
    full_path(path).open('w') {|f|
      f.write content
    }
  }
end

#edit(path, content) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/xrt/transaction.rb', line 8

def edit(path, content)
  unless full_path(path).exist?
    raise 'Editing new file'
  end
  if full_path(path).read == content
    return
  end
  add(path, content)
end

#full_path(*fragments) ⇒ Object



31
32
33
# File 'lib/xrt/transaction.rb', line 31

def full_path(*fragments)
  Pathname(fragments.shift).join(*fragments)
end

#new_file(path, content) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/xrt/transaction.rb', line 18

def new_file(path, content)
  if full_path(path).exist?
    if full_path(path).read == content
      # nothing will change
      return
    else
      raise "File #{path} already exists"
    end
  end

  add(path, content)
end