Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,9 @@ https://github.com/lyrasis/kiba-extend/pull/260[PR#260]

=== Changed

* Initializing a Job class no longer runs the job, resolving https://github.com/lyrasis/kiba-extend/issues/159[#159]
https://github.com/lyrasis/kiba-extend/pull/262[PR#262]

=== Deleted

=== Deprecated/Will break in a future version
Expand Down
37 changes: 14 additions & 23 deletions lib/kiba/extend/jobs/base_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ class BaseJob
include Runner
include Parser

attr_reader :control, :context, :files, :transformer, :srcrows, :outrows
attr_reader :files, :transformer, :srcrows, :outrows

# @param files [Hash]
# @param transformer [Kiba::Control]
# @param mode [:run, :setup, :info] :info mode sets up files only.
# :setup mode sets up files and handles requirements, including
# running any necessary jobs to create sources and/or lookups needed
# by the job. :run does all of the above and runs the job. Since 4.0.0
def initialize(files:, transformer:, mode: :run)
def initialize(files:, transformer:)
@destination_key = files[:destination].is_a?(Symbol) ?
files[:destination] :
files[:destination].first
Expand All @@ -35,28 +31,23 @@ def initialize(files:, transformer:, mode: :run)
extend DependencyJob if @dependency

@files = setup_files(files.transform_values { |v| [v].flatten })
@transformer = transformer
end

unless mode == :info
report_run_start # defined in Reporter
# defined in Runner
%i[source lookup].each do |type|
handle_requirements(type)
end
@control = Kiba::Control.new
@context = Kiba::Context.new(control)
@transformer = transformer
assemble_control # defined in Runner
end
def control = @control ||= Kiba::Control.new

if mode == :run
run
set_row_count_instance_variables
report_run_end # defined in Reporter
end
end
def context = @context ||= Kiba::Context.new(control)

def run
report_run_start # defined in Reporter
# defined in Runner
%i[source lookup].each do |type|
handle_requirements(type)
end
assemble_control # defined in Runner
Kiba.run(control)
set_row_count_instance_variables
report_run_end # defined in Reporter
rescue => err
puts "JOB FAILED: TRANSFORM ERROR IN: #{job_data.creator}"
puts "#{err.class.name}: #{err.message}"
Expand Down
8 changes: 3 additions & 5 deletions lib/kiba/extend/registry/creator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ def initialize(spec)
end

def call
if args
mod.send(meth, **args)
else
mod.send(meth)
end
job = args ? mod.send(meth, **args) : mod.send(meth)
job.run
job
end

def to_s
Expand Down
12 changes: 3 additions & 9 deletions lib/kiba/extend/registry/file_registry_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ def initialize(key, reghash)
validate
end

def dir
path.dirname
end
def dir = path.dirname

# Printable string summarizing the Entry, called by project applications
def summary
Expand Down Expand Up @@ -61,15 +59,11 @@ def summary_creator
lines.map { |line| "#{summary_padding}#{line}" }.join("\n")
end

def summary_padding
" "
end
def summary_padding = " "

# Whether the Entry is valid
# @return [Boolean]
def valid?
valid
end
def valid? = valid

private

Expand Down
2 changes: 2 additions & 0 deletions spec/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ class OutputJob
def initialize(rowct)
@outrows = rowct
end

def run = nil
end

def fake_creator_with_no_job_output
Expand Down
8 changes: 5 additions & 3 deletions spec/kiba/extend/jobs/job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
transform_registry
end
after(:all) { Kiba::Extend.reset_config }

before(:each) do
FileUtils.rm(@dest_file) if File.exist?(@dest_file)
end

after(:each) do
FileUtils.rm(@dest_file) if File.exist?(@dest_file)
end
Expand Down Expand Up @@ -73,7 +75,7 @@
let(:job) { base_job }

it "runs and produces expected result" do
job
job.run
result = CSV.read(@dest_file)
expect(result).to eq(expected_lookup_result)
end
Expand Down Expand Up @@ -103,7 +105,7 @@
end

it "runs and produces expected result" do
job
job.run
result = CSV.read(@dest_file)
expect(result).to eq(expected_lookup_result)
end
Expand Down Expand Up @@ -144,7 +146,7 @@
["two", "b", "bird", "bang"]
]

job
job.run
result = CSV.read(@dest_file)
expect(result).to eq(expected)
end
Expand Down
2 changes: 1 addition & 1 deletion spec/kiba/extend/jobs/json_to_csv_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
end

it "runs and produces expected result" do
job
job.run
result = CSV.table(@dest_file)
expect(result.size).to eq(4)
end
Expand Down
4 changes: 2 additions & 2 deletions spec/kiba/extend/jobs/marc_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
end

it "runs and produces expected result" do
marcjob
marcjob.run
result = CSV.table(@dest_file)
expect(result).to be_a(CSV::Table)
end
Expand Down Expand Up @@ -99,7 +99,7 @@
end

it "runs and produces expected result" do
marcjob
marcjob.run
expect(recs.first).to be_a(MARC::Record)
expect(recs.length).to eq(10)
end
Expand Down
78 changes: 47 additions & 31 deletions spec/kiba/extend/registry/creator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,38 +5,42 @@
# used to test creator validation below
module Helpers
module Project
class FakeJob
attr_reader :args

def initialize(args = {})
@args = args
end

def run = nil
end

module Jobby
module_function

def job
"run!"
end
def job = FakeJob.new
end

module Unjobby
module_function

def prep
"prepped"
end
def prep = FakeJob.new
end

module JobbyArg
module_function

def job(shout: false)
val = "run!"
shout ? val.upcase : val
end
def job(args) = FakeJob.new(**args)
# def job(shout: false)
# val = "run!"
# shout ? val.upcase : val
# end
end

module UnjobbyArg
module_function

def prep(shout: false)
val = "prepped"
shout ? val.upcase : val
end
def prep(args) = FakeJob.new(**args)
end
end
end
Expand Down Expand Up @@ -145,43 +149,55 @@ def prep(shout: false)
end

describe "#call" do
let(:result) { creator.call }

context "with no args" do
context "with method" do
let(:spec) { Helpers::Project::Unjobby.method(:prep) }

it "calls as expected" do
expect(result).to eq("prepped")
unjobby = class_double(Helpers::Project::Unjobby)
expect(unjobby).to receive(:prep)
.and_return(Helpers::Project::FakeJob.new)
c = Kiba::Extend::Registry::Creator.new(unjobby.method(:prep))
result = c.call
expect(result).to be_a(Helpers::Project::FakeJob)
end
end

context "with jobby module" do
let(:spec) { Helpers::Project::Jobby }

it "calls as expected" do
expect(result).to eq("run!")
expect(Helpers::Project::Jobby).to receive(:job)
.and_return(Helpers::Project::FakeJob.new)
c = Kiba::Extend::Registry::Creator.new(Helpers::Project::Jobby)
result = c.call
expect(result).to be_a(Helpers::Project::FakeJob)
end
end
end

context "with args" do
context "with method" do
let(:spec) do
{callee: Helpers::Project::UnjobbyArg.method(:prep),
args: {shout: true}}
end

it "calls as expected" do
expect(result).to eq("PREPPED")
args = {shout: true}
spec = {callee: Helpers::Project::UnjobbyArg.method(:prep),
args: args}
expect(Helpers::Project::UnjobbyArg).to receive(:prep)
.and_return(Helpers::Project::FakeJob.new(**args))
c = Kiba::Extend::Registry::Creator.new(spec)
result = c.call
expect(result).to be_a(Helpers::Project::FakeJob)
expect(result.args[:shout]).to be true
end
end

context "with jobby module" do
let(:spec) { {callee: Helpers::Project::JobbyArg, args: {shout: true}} }

it "calls as expected" do
expect(result).to eq("RUN!")
args = {shout: true}
spec = {callee: Helpers::Project::JobbyArg,
args: args}
expect(Helpers::Project::JobbyArg).to receive(:job)
.and_return(Helpers::Project::FakeJob.new(**args))
c = Kiba::Extend::Registry::Creator.new(spec)
result = c.call
expect(result).to be_a(Helpers::Project::FakeJob)
expect(result.args[:shout]).to be true
end
end
end
Expand Down
21 changes: 10 additions & 11 deletions spec/kiba/extend/registry/file_registry_entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module Section
def desc
<<~DESC
Here is the job description.

Blah blah blah.
DESC
end
Expand All @@ -36,7 +35,6 @@ def headers
end
end

# rubocop:disable Metrics/BlockLength
RSpec.describe "Kiba::Extend::Registry::FileRegistryEntry" do
let(:path) { File.join("spec", "fixtures", "fkey.csv") }
let(:entry) { Kiba::Extend::Registry::FileRegistryEntry.new(:job__key, data) }
Expand Down Expand Up @@ -223,22 +221,24 @@ def headers
it "invalid as expected" do
expect(entry.creator).to be_nil
expect(entry.valid?).to be false
# rubocop:todo Layout/LineLength
expect(entry.errors.key?("Kiba::Extend::Registry::Creator::TypeError")).to be true
# rubocop:enable Layout/LineLength
expect(
entry.errors.key?("Kiba::Extend::Registry::Creator::TypeError")
).to be true
end
end

# rubocop:todo Layout/LineLength
context "when a Module not containing a `job` method, and no method given" do
context "when a Module not containing a `job` method, "\
"and no method given" do
# rubocop:enable Layout/LineLength
let(:data) { {path: path, creator: Helpers::Project::JoblessSection} }
it "invalid as expected" do
expect(entry.creator).to be_nil
expect(entry.valid?).to be false
# rubocop:todo Layout/LineLength
expect(entry.errors.key?("Kiba::Extend::Registry::Creator::JoblessModuleCreatorError")).to be true
# rubocop:enable Layout/LineLength
expect(
entry.errors.key?(
"Kiba::Extend::Registry::Creator::JoblessModuleCreatorError"
)
).to be true
end
end

Expand Down Expand Up @@ -272,4 +272,3 @@ def headers
end
end
end
# rubocop:enable Metrics/BlockLength
Loading