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)


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

def approved?
  File.exists? approved_path
end

#approved_pathObject



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

def approved_path
  full_path('approved')
end

#approved_textObject



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

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



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

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

#fail_with(message) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/approvals/approval.rb', line 73

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



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

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



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

def name
  namer.name
end

#received_matches?Boolean

Returns:

  • (Boolean)


65
66
67
68
69
70
71
# File 'lib/approvals/approval.rb', line 65

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

#received_pathObject



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

def received_path
  full_path('received')
end

#received_textObject



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

def received_text
  File.read(received_path).chomp
end

#success!Object



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

def success!
  File.delete received_path
end

#verifyObject



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/approvals/approval.rb', line 37

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



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

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