Class: Approvals::Approval
- Inherits:
-
Object
- Object
- Approvals::Approval
- Defined in:
- lib/approvals/approval.rb
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
- #verify ⇒ Object
- #writer ⇒ Object
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, = {}) @subject = subject @namer = [:namer] || default_namer([:name]) @format = [:format] || identify_format end |
Class Attribute Details
.namer ⇒ Object
Returns the value of attribute namer.
4 5 6 |
# File 'lib/approvals/approval.rb', line 4 def namer @namer end |
Instance Attribute Details
#failure ⇒ Object (readonly)
Returns the value of attribute failure.
7 8 9 |
# File 'lib/approvals/approval.rb', line 7 def failure @failure end |
#namer ⇒ Object (readonly)
Returns the value of attribute namer.
7 8 9 |
# File 'lib/approvals/approval.rb', line 7 def namer @namer end |
#subject ⇒ Object (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
50 51 52 |
# File 'lib/approvals/approval.rb', line 50 def approved? File.exists? approved_path end |
#approved_path ⇒ Object
83 84 85 |
# File 'lib/approvals/approval.rb', line 83 def approved_path full_path('approved') end |
#approved_text ⇒ Object
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_path ⇒ Object
71 72 73 |
# File 'lib/approvals/approval.rb', line 71 def diff_path "#{received_path} #{approved_path}" end |
#fail_with(message) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/approvals/approval.rb', line 58 def fail_with() 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: #{}") 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_format ⇒ Object
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 |
#name ⇒ Object
79 80 81 |
# File 'lib/approvals/approval.rb', line 79 def name namer.name end |
#received_matches? ⇒ Boolean
54 55 56 |
# File 'lib/approvals/approval.rb', line 54 def received_matches? FileUtils.cmp received_path, approved_path end |
#received_path ⇒ Object
87 88 89 |
# File 'lib/approvals/approval.rb', line 87 def received_path full_path('received') end |
#received_text ⇒ Object
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 |
#verify ⇒ Object
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 |