Class: Approvals::Approval

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

Constant Summary collapse

IDENTITIES =

Add a Proc that tests if subject is a kind of format

{
  hash: Proc.new(){|subject|subject.respond_to? :each_pair},
  array: Proc.new(){|subject|subject.respond_to? :each_with_index},
}
BINARY_FORMATS =
[:binary]

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Approval.



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

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.



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

def namer
  @namer
end

Instance Attribute Details

#failureObject (readonly)

Returns the value of attribute failure.



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

def failure
  @failure
end

#namerObject (readonly)

Returns the value of attribute namer.



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

def namer
  @namer
end

#subjectObject (readonly)

Returns the value of attribute subject.



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

def subject
  @subject
end

Instance Method Details

#approved?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/approvals/approval.rb', line 63

def approved?
  File.exist? approved_path
end

#approved_pathObject



108
109
110
# File 'lib/approvals/approval.rb', line 108

def approved_path
  full_path('approved')
end

#approved_textObject



116
117
118
# File 'lib/approvals/approval.rb', line 116

def approved_text
  File.read(approved_path).chomp
end

#default_namer(name) ⇒ Object



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

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

#diff_pathObject



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

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

#fail_with(message) ⇒ Object



81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/approvals/approval.rb', line 81

def fail_with(message)
  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

  error = ApprovalError.new("Approval Error: #{message}")
  error.approved_path = approved_path
  error.received_path = received_path

  raise error
end

#full_path(state) ⇒ Object



100
101
102
# File 'lib/approvals/approval.rb', line 100

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

#identify_formatObject



25
26
27
28
29
30
31
# File 'lib/approvals/approval.rb', line 25

def identify_format
  IDENTITIES.each_pair do |format, id_test|
    return format if id_test.call(subject)
  end
  # otherwise
  return :txt
end

#nameObject



104
105
106
# File 'lib/approvals/approval.rb', line 104

def name
  namer.name
end

#received_matches?Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/approvals/approval.rb', line 69

def received_matches?
  return verifier
    .new(received_path, approved_path)
    .verify if verifier

  if BINARY_FORMATS.include?(@format) # Read without ERB
    IO.read(received_path).chomp == IO.read(approved_path).chomp
  else
    ERB.new(IO.read(received_path).chomp).result == ERB.new(IO.read(approved_path).chomp).result
  end
end

#received_pathObject



112
113
114
# File 'lib/approvals/approval.rb', line 112

def received_path
  full_path('received')
end

#received_textObject



120
121
122
# File 'lib/approvals/approval.rb', line 120

def received_text
  File.read(received_path).chomp
end

#success!Object



59
60
61
# File 'lib/approvals/approval.rb', line 59

def success!
  File.delete received_path
end

#verifierObject



37
38
39
# File 'lib/approvals/approval.rb', line 37

def verifier
  @verifier ||= Verifier.for(@format)
end

#verifyObject



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/approvals/approval.rb', line 41

def verify
  unless File.exist?(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



33
34
35
# File 'lib/approvals/approval.rb', line 33

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