Skip to content

Commit 678cbd2

Browse files
committed
feat: Add Kiba::Extend::Job.output_fields method
1 parent cda7483 commit 678cbd2

4 files changed

Lines changed: 77 additions & 1 deletion

File tree

lib/kiba/extend/job.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,24 @@ module Extend
66
module Job
77
module_function
88

9+
# @param jobkey [Symbol] registry entry for job with namespace
10+
# @return [nil, Array<Symbol>] headers/fields for given job output
11+
# @note Only works for CSV and JsonArray destinations. For JsonArray, only
12+
# returns the top-level fields of the objects/rows in the output.
13+
def output_fields(jobkey)
14+
return unless output?(jobkey)
15+
16+
entry = Kiba::Extend.registry.resolve(jobkey)
17+
path = Pathname.new(entry.path)
18+
dest = entry.dest_class.new(filename: path)
19+
unless dest.respond_to?(:fields)
20+
raise "No output field extraction logic exists for "\
21+
"#{entry.dest_class}"
22+
end
23+
24+
dest.fields
25+
end
26+
927
# @param jobkey [Symbol] registry entry for job with namespace
1028
# @return [true] if output file already exists when run, or when running
1129
# job results in 1 or more rows being written

spec/fixtures/output.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[{"Archival_Object__title":"Image of Bob and Sue","Archival_Object__dates":[{"Archival_Object__dates__begin":"1987","Archival_Object__dates__label":"other","Archival_Object__dates__date_type":"inclusive"},{"Archival_Object__dates__end":"1989","Archival_Object__dates__label":"other","Archival_Object__dates__date_type":"inclusive"}]},{"Archival_Object__title":"Audio recording of a horse","Archival_Object__subjects":["/Subjects/1","/Subjects/2"]}]

spec/helpers.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def populate_registry(more_entries: {})
180180
briefdescription]}
181181
},
182182
json_arr: {
183-
path: fkeypath,
183+
path: File.join(fixtures_dir, "output.json"),
184184
dest_class: Kiba::Extend::Destinations::JsonArray,
185185
creator: Helpers.method(:fake_creator_method)
186186
},

spec/kiba/extend/job_spec.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,63 @@
33
require "spec_helper"
44

55
RSpec.describe Kiba::Extend::Job do
6+
describe ".output_fields" do
7+
let(:result) { Kiba::Extend::Job.output_fields(key) }
8+
9+
context "when key not registered" do
10+
let(:key) { :foo__bar }
11+
12+
it "returns nil" do
13+
expect(result).to be_nil
14+
end
15+
end
16+
17+
context "when job has output" do
18+
before(:context) do
19+
Kiba::Extend.config.registry = Kiba::Extend::Registry::FileRegistry
20+
prepare_registry
21+
end
22+
after(:context) { Kiba::Extend.reset_config }
23+
let(:key) { :foo }
24+
25+
it "returns headers" do
26+
expect(result).to eq(
27+
[:objectnumber, :numberofobjects, :briefdescription,
28+
:distinguishingfeatures, :comment]
29+
)
30+
end
31+
end
32+
33+
context "when job has no output" do
34+
before(:context) do
35+
Kiba::Extend.config.registry = Kiba::Extend::Registry::FileRegistry
36+
prepare_registry
37+
end
38+
after(:context) { Kiba::Extend.reset_config }
39+
let(:key) { :noresultfile }
40+
41+
it "returns nil" do
42+
expect(result).to be_nil
43+
end
44+
end
45+
46+
context "when job has JSON destination" do
47+
before(:context) do
48+
Kiba::Extend.config.registry = Kiba::Extend::Registry::FileRegistry
49+
prepare_registry
50+
end
51+
after(:context) { Kiba::Extend.reset_config }
52+
let(:key) { :json_arr }
53+
54+
it "returns top-level fields/keys" do
55+
expect(result).to eq(
56+
["Archival_Object__title", "Archival_Object__dates",
57+
"Archival_Object__subjects"]
58+
)
59+
end
60+
end
61+
end
62+
663
describe ".output?" do
764
let(:result) { Kiba::Extend::Job.output?(key) }
865

0 commit comments

Comments
 (0)