Class: Approvals::Approval
- Inherits:
-
Object
- Object
- Approvals::Approval
- 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
-
.namer ⇒ Object
Returns the value of attribute namer.
Instance Attribute Summary collapse
-
#failure ⇒ Object
readonly
Returns the value of attribute failure.
-
#namer ⇒ Object
readonly
Returns the value of attribute namer.
-
#subject ⇒ Object
readonly
Returns the value of attribute subject.
Instance Method Summary collapse
- #approved? ⇒ Boolean
- #approved_path ⇒ Object
- #approved_text ⇒ Object
- #default_namer(name) ⇒ Object
- #diff_path ⇒ Object
- #fail_with(message) ⇒ Object
- #full_path(state) ⇒ Object
- #identify_format ⇒ Object
-
#initialize(subject, options = {}) ⇒ Approval
constructor
A new instance of Approval.
- #name ⇒ Object
- #received_matches? ⇒ Boolean
- #received_path ⇒ Object
- #received_text ⇒ Object
- #success! ⇒ Object
- #verifier ⇒ Object
- #verify ⇒ Object
- #writer ⇒ Object
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, = {}) @subject = subject @namer = [:namer] || default_namer([:name]) @format = [:format] || identify_format end |
Class Attribute Details
.namer ⇒ Object
Returns the value of attribute namer.
5 6 7 |
# File 'lib/approvals/approval.rb', line 5 def namer @namer end |
Instance Attribute Details
#failure ⇒ Object (readonly)
Returns the value of attribute failure.
8 9 10 |
# File 'lib/approvals/approval.rb', line 8 def failure @failure end |
#namer ⇒ Object (readonly)
Returns the value of attribute namer.
8 9 10 |
# File 'lib/approvals/approval.rb', line 8 def namer @namer end |
#subject ⇒ Object (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
63 64 65 |
# File 'lib/approvals/approval.rb', line 63 def approved? File.exist? approved_path end |
#approved_path ⇒ Object
108 109 110 |
# File 'lib/approvals/approval.rb', line 108 def approved_path full_path('approved') end |
#approved_text ⇒ Object
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_path ⇒ Object
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() 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: #{}") 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_format ⇒ Object
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 |
#name ⇒ Object
104 105 106 |
# File 'lib/approvals/approval.rb', line 104 def name namer.name end |
#received_matches? ⇒ 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_path ⇒ Object
112 113 114 |
# File 'lib/approvals/approval.rb', line 112 def received_path full_path('received') end |
#received_text ⇒ Object
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 |
#verifier ⇒ Object
37 38 39 |
# File 'lib/approvals/approval.rb', line 37 def verifier @verifier ||= Verifier.for(@format) end |
#verify ⇒ Object
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 |