Class: Git::Approvals::Approval

Inherits:
Object
  • Object
show all
Includes:
Utils
Defined in:
lib/git/approvals/approval.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils

filenamify, transform_filename

Constructor Details

#initialize(path, options = {}) ⇒ Approval

:nodoc:



12
13
14
# File 'lib/git/approvals/approval.rb', line 12

def initialize( path, options={} ) # :nodoc:
  @path = transform_filename( path, options )
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/git/approvals/approval.rb', line 15

def path
  @path
end

Instance Method Details

#diff(string, &block) ⇒ Object

Diffs the given string with this approval file. If the file has not been checked in, this method will raise an exception. Otherwise, the supplied block will only be called if the diff fails, meaning there are differences.



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/git/approvals/approval.rb', line 22

def diff( string, &block )
  # Make sure the directory of the file exists.
  FileUtils.mkdir_p File.dirname( path )

  # Write the new string to the file.
  File.open path, 'w' do |f|
    f << Tilt.new( path ).render( string )
  end

  # If the file hasn't been checked in, raise an error.
  sh "git ls-files #{path} --error-unmatch" do |err|
    raise Errno::ENOENT, path
  end

  # If the file has changed, call the block.
  sh "git diff --exit-code #{path}" do |err|
    block.call err
  end
end

#sh(cmd) {|out| ... } ⇒ Object

Shells out the given command. If the command exits with success, does nothing. If the command does not exit with success, yields the error output to the block.

Yields:

  • (out)


46
47
48
49
# File 'lib/git/approvals/approval.rb', line 46

def sh( cmd )
  out, cmd = Open3.capture2e cmd
  yield out if !cmd.success?
end