Class: Approvals::Approval

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Approval.



8
9
10
11
12
# File 'lib/approvals/approval.rb', line 8

def initialize(subject, options = {})
  @subject = subject
  @namer = options[:namer] || default_namer(options[:name])
  @format = options[:format] || identify_format
end

Class Attribute Details

.namerObject

Returns the value of attribute namer.



4
5
6
# File 'lib/approvals/approval.rb', line 4

def namer
  @namer
end

Instance Attribute Details

#failureObject (readonly)

Returns the value of attribute failure.



7
8
9
# File 'lib/approvals/approval.rb', line 7

def failure
  @failure
end

#namerObject (readonly)

Returns the value of attribute namer.



7
8
9
# File 'lib/approvals/approval.rb', line 7

def namer
  @namer
end

#subjectObject (readonly)

Returns the value of attribute subject.



7
8
9
# File 'lib/approvals/approval.rb', line 7

def subject
  @subject
end

Instance Method Details

#approved?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'lib/approvals/approval.rb', line 50

def approved?
  File.exists? approved_path
end

#approved_pathObject



83
84
85
# File 'lib/approvals/approval.rb', line 83

def approved_path
  full_path('approved')
end

#approved_textObject



91
92
93
# File 'lib/approvals/approval.rb', line 91

def approved_text
  File.read approved_path
end

#default_namer(name) ⇒ Object



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

def default_namer(name)
  Approvals::Approval.namer || Namers::DefaultNamer.new(name)
end

#diff_pathObject



71
72
73
# File 'lib/approvals/approval.rb', line 71

def diff_path
  "#{received_path} #{approved_path}"
end

#fail_with(message) ⇒ Object

Raises:



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/approvals/approval.rb', line 58

def fail_with(message)
  FileUtils.touch(approved_path) unless File.exists? approved_path

  Dotfile.append(diff_path)

  if subject.respond_to?(:on_failure)
    subject.on_failure.call(approved_text) if approved?
    subject.on_failure.call(received_text)
  end

  raise ApprovalError.new("Approval Error: #{message}")
end

#full_path(state) ⇒ Object



75
76
77
# File 'lib/approvals/approval.rb', line 75

def full_path(state)
  "#{namer.output_dir}#{namer.name}.#{state}.#{writer.extension}"
end

#identify_formatObject



18
19
20
21
22
# File 'lib/approvals/approval.rb', line 18

def identify_format
  return :hash if subject.respond_to? :each_pair
  return :array if subject.respond_to? :each_with_index
  return :txt
end

#nameObject



79
80
81
# File 'lib/approvals/approval.rb', line 79

def name
  namer.name
end

#received_matches?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/approvals/approval.rb', line 54

def received_matches?
  FileUtils.cmp received_path, approved_path
end

#received_pathObject



87
88
89
# File 'lib/approvals/approval.rb', line 87

def received_path
  full_path('received')
end

#received_textObject



95
96
97
# File 'lib/approvals/approval.rb', line 95

def received_text
  File.read received_path
end

#success!Object



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

def success!
  File.delete received_path
end

#verifyObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/approvals/approval.rb', line 28

def verify
  unless File.exists?(namer.output_dir)
    FileUtils.mkdir_p(namer.output_dir)
  end

  writer.write(subject, received_path)

  unless approved?
    fail_with "Approval file \"#{approved_path}\" not found."
  end

  unless received_matches?
    fail_with "Received file \"#{received_path}\" does not match approved \"#{approved_path}\"."
  end

  success!
end

#writerObject



24
25
26
# File 'lib/approvals/approval.rb', line 24

def writer
  @writer ||= Writer.for(@format)
end