Skip to content

Commit

Permalink
refactor collections and selections
Browse files Browse the repository at this point in the history
  • Loading branch information
gkostin1966 committed Jun 12, 2019
1 parent f2b065f commit 3e5930e
Show file tree
Hide file tree
Showing 16 changed files with 76 additions and 76 deletions.
14 changes: 7 additions & 7 deletions app/modules/lib_ptg_box.rb
Expand Up @@ -4,8 +4,8 @@
require_relative 'lib_ptg_box/unmarshaller'

require_relative 'lib_ptg_box/work'
require_relative 'lib_ptg_box/product'
require_relative 'lib_ptg_box/product_family'
require_relative 'lib_ptg_box/selection'
require_relative 'lib_ptg_box/collection'
require_relative 'lib_ptg_box/lib_ptg_box'

module LibPtgBox
Expand All @@ -17,11 +17,11 @@ module LibPtgBox
#
# class << self
# def test
# LibPtgBox.new.product_families.each do |product_family|
# puts product_family.name
# product_family.products.each do |product|
# puts "--> " + product.name + " " + product.yyyy + '-' + product.mm + '-' + product.dd
# product.works.each do |work|
# LibPtgBox.new.collections.each do |collection|
# puts collection.name
# collection.selections.each do |selection|
# puts "--> " + selection.name + " " + selection.year + " " + selection.updated
# selection.works.each do |work|
# puts "----> #{work.doi}" unless work.marc?
# end
# end
Expand Down
@@ -1,22 +1,22 @@
# frozen_string_literal: true

module LibPtgBox
class ProductFamily
class Collection
attr_reader :name

def initialize(family_folder)
@family_folder = family_folder
@name = family_folder.name
def initialize(sub_folder)
@sub_folder = sub_folder
@name = sub_folder.name
end

def products
@products ||= @family_folder.kbart_folder.kbart_files.map { |kbart_file| Product.new(self, kbart_file) }
def selections
@selections ||= @sub_folder.kbart_folder.kbart_files.map { |kbart_file| Selection.new(self, kbart_file) }
end

def catalog
@catalog ||= begin
complete_marc_file = nil
@family_folder.cataloging_marc_folder.marc_files.each do |marc_file|
@sub_folder.cataloging_marc_folder.marc_files.each do |marc_file|
next unless /complete\.xml/i.match?(marc_file.name)

complete_marc_file = marc_file
Expand Down
4 changes: 2 additions & 2 deletions app/modules/lib_ptg_box/lib_ptg_box.rb
Expand Up @@ -2,8 +2,8 @@

module LibPtgBox
class LibPtgBox
def product_families
@product_families ||= Unmarshaller::RootFolder.family_folders.map { |family_folder| ProductFamily.new(family_folder) }
def collections
@collections ||= Unmarshaller::RootFolder.sub_folders.map { |sub_folder| Collection.new(sub_folder) }
end
end
end
Expand Up @@ -4,8 +4,8 @@ module LibPtgBox
class Selection
attr_reader :collection, :name, :year, :updated

def initialize(product_family, kbart_file)
@product_family = product_family
def initialize(collection, kbart_file)
@collection = collection
@kbart_file = kbart_file
match = /(^.+)_(\d{4}-\d{2}-\d{2})\.(.+$)/.match(@kbart_file.name)
@name = match[1]
Expand Down
2 changes: 1 addition & 1 deletion app/modules/lib_ptg_box/unmarshaller.rb
Expand Up @@ -6,7 +6,7 @@
require_relative 'unmarshaller/kbart'
require_relative 'unmarshaller/kbart_file'
require_relative 'unmarshaller/kbart_folder'
require_relative 'unmarshaller/family_folder'
require_relative 'unmarshaller/sub_folder'
require_relative 'unmarshaller/root_folder'

# require_relative 'unmarshaller/unmarshaller'
Expand Down
4 changes: 2 additions & 2 deletions app/modules/lib_ptg_box/unmarshaller/root_folder.rb
Expand Up @@ -4,8 +4,8 @@ module LibPtgBox
module Unmarshaller
class RootFolder
class << self
def family_folders
Box::Service.new.folder(BOX_LIB_PTG_BOX_PATH).folders.map { |folder| FamilyFolder.new(folder) }
def sub_folders
Box::Service.new.folder(BOX_LIB_PTG_BOX_PATH).folders.map { |folder| SubFolder.new(folder) }
end
end
end
Expand Down
Expand Up @@ -2,7 +2,7 @@

module LibPtgBox
module Unmarshaller
class FamilyFolder < SimpleDelegator
class SubFolder < SimpleDelegator
def kbart_folder
@kbart_folder ||= begin
f = Box::Folder.null_folder
Expand Down
6 changes: 3 additions & 3 deletions app/modules/lib_ptg_box/work.rb
Expand Up @@ -4,8 +4,8 @@ module LibPtgBox
class Work
attr_reader :doi

def initialize(product, kbart)
@product = product
def initialize(selection, kbart)
@selection = selection
@kbart = kbart
@doi = kbart.doi
end
Expand All @@ -15,7 +15,7 @@ def marc?
end

def marc
@marc ||= @product.product_family.catalog.marc(@doi)
@marc ||= @selection.collection.catalog.marc(@doi)
end
end
end
2 changes: 1 addition & 1 deletion app/programs/assemble_marc_files/assemble_marc_files.rb
Expand Up @@ -22,7 +22,7 @@ def synchronize(lib_ptg_box) # rubocop:disable Metrics/AbcSize
next unless /umpebc/i.match?(collection.name)

collection.selections.each do |selection|
record = LibPtgFolder.find_or_create_by!(name: selection.name)
record = UmpebcKbart.find_or_create_by!(name: selection.name)
log += record.id.to_s + " " + collection.name + " " + selection.name + "\n"
puts record.inspect
end
Expand Down
4 changes: 2 additions & 2 deletions spec/modules/lib_ptg_box/catalog_spec.rb
Expand Up @@ -3,9 +3,9 @@
require 'rails_helper'

RSpec.describe LibPtgBox::Catalog do
subject(:catalog) { described_class.new(product, complete_marc_file) }
subject(:catalog) { described_class.new(selection, complete_marc_file) }

let(:product) { instance_double(LibPtgBox::Product, 'product') }
let(:selection) { instance_double(LibPtgBox::Selection, 'selection') }
let(:complete_marc_file) { object_double(LibPtgBox::Unmarshaller::MarcFile.new(marc_box_file), 'complete_marc_file') }
let(:marc_box_file) { instance_double(Box::File, 'marc_box_file', content: content) }
let(:content) { instance_double(String, 'content') }
Expand Down
Expand Up @@ -2,42 +2,42 @@

require 'rails_helper'

RSpec.describe LibPtgBox::ProductFamily do
subject(:product_family) { described_class.new(family_folder) }
RSpec.describe LibPtgBox::Collection do
subject(:collection) { described_class.new(sub_folder) }

let(:family_folder) { object_double(LibPtgBox::Unmarshaller::FamilyFolder.new(family_box_folder), 'family_folder') }
let(:family_box_folder) { instance_double(Box::Folder, 'family_box_folder', name: 'Product_Family') }
let(:sub_folder) { object_double(LibPtgBox::Unmarshaller::SubFolder.new(sub_box_folder), 'sub_folder') }
let(:sub_box_folder) { instance_double(Box::Folder, 'sub_box_folder', name: 'Collection') }

before do
allow(family_folder).to receive(:name).and_return(family_box_folder.name)
allow(sub_folder).to receive(:name).and_return(sub_box_folder.name)
end

describe '#name' do
subject { product_family.name }
subject { collection.name }

it { is_expected.to eq(family_box_folder.name) }
it { is_expected.to eq(sub_box_folder.name) }
end

describe '#products' do
subject { product_family.products }
describe '#selections' do
subject { collection.selections }

let(:kbart_folder) { instance_double(LibPtgBox::Unmarshaller::KbartFolder, 'kbart_folder') }
let(:kbart_file) { object_double(LibPtgBox::Unmarshaller::KbartFile.new(kbart_box_file), 'kbart_box_file') }
let(:kbart_box_file) { instance_double(Box::File, 'kbart_box_file', name: 'Product_0000_0000-00-00.csv') }
let(:product) { 'product' }
let(:kbart_box_file) { instance_double(Box::File, 'kbart_box_file', name: 'Selection_1999_1970-01-01.csv') }
let(:selection) { 'selection' }

before do
allow(family_folder).to receive(:kbart_folder).and_return(kbart_folder)
allow(sub_folder).to receive(:kbart_folder).and_return(kbart_folder)
allow(kbart_folder).to receive(:kbart_files).and_return([kbart_file])
allow(kbart_file).to receive(:name).and_return(kbart_box_file.name)
allow(LibPtgBox::Product).to receive(:new).with(product_family, kbart_file).and_return(product)
allow(LibPtgBox::Selection).to receive(:new).with(collection, kbart_file).and_return(selection)
end

it { is_expected.to contain_exactly(product) }
it { is_expected.to contain_exactly(selection) }
end

describe '#catalog' do
subject { product_family.catalog }
subject { collection.catalog }

let(:marc_folder) { object_double(LibPtgBox::Unmarshaller::MarcFolder.new(marc_box_folder), 'marc_folder') }
let(:marc_box_folder) { instance_double(Box::Folder, 'marc_box_folder', name: 'name') }
Expand All @@ -49,12 +49,12 @@
let(:catalog) { 'catalog' }

before do
allow(family_folder).to receive(:cataloging_marc_folder).and_return(marc_folder)
allow(sub_folder).to receive(:cataloging_marc_folder).and_return(marc_folder)
allow(marc_folder).to receive(:marc_files).and_return([product_marc_file, complete_marc_file])
allow(product_marc_file).to receive(:name).and_return(product_box_file.name)
allow(complete_marc_file).to receive(:name).and_return(complete_box_file.name)
allow(complete_marc_file).to receive(:content).and_return(complete_box_file.content)
allow(LibPtgBox::Catalog).to receive(:new).with(product_family, complete_marc_file).and_return(catalog)
allow(LibPtgBox::Catalog).to receive(:new).with(collection, complete_marc_file).and_return(catalog)
end

it { is_expected.to eq(catalog) }
Expand Down
14 changes: 7 additions & 7 deletions spec/modules/lib_ptg_box/lib_ptg_box_spec.rb
Expand Up @@ -3,17 +3,17 @@
require 'rails_helper'

RSpec.describe LibPtgBox::LibPtgBox do
describe "#product_families" do
subject { described_class.new.product_families }
describe "#collections" do
subject { described_class.new.collections }

let(:family_folder) { 'family_folder' }
let(:product_family) { 'product_family' }
let(:sub_folder) { 'sub_folder' }
let(:collection) { 'collection' }

before do
allow(LibPtgBox::Unmarshaller::RootFolder).to receive(:family_folders).and_return([family_folder])
allow(LibPtgBox::ProductFamily).to receive(:new).with(family_folder).and_return(product_family)
allow(LibPtgBox::Unmarshaller::RootFolder).to receive(:sub_folders).and_return([sub_folder])
allow(LibPtgBox::Collection).to receive(:new).with(sub_folder).and_return(collection)
end

it { is_expected.to contain_exactly(product_family) }
it { is_expected.to contain_exactly(collection) }
end
end
Expand Up @@ -2,10 +2,10 @@

require 'rails_helper'

RSpec.describe LibPtgBox::Product do
subject(:product) { described_class.new(product_family, kbart_file) }
RSpec.describe LibPtgBox::Selection do
subject(:selection) { described_class.new(collection, kbart_file) }

let(:product_family) { instance_double(LibPtgBox::ProductFamily, 'product_family') }
let(:collection) { instance_double(LibPtgBox::Collection, 'collection') }
let(:kbart_file) { object_double(LibPtgBox::Unmarshaller::KbartFile.new(kbart_box_file), 'kbart_file') }
let(:kbart_box_file) { instance_double(Box::File, 'kbart_box_file', name: kbart_file_name) }
let(:kbart_file_name) { 'Prefix_1970_Suffix_1999-01-01.csv' }
Expand All @@ -17,14 +17,14 @@
it { expect(selection.updated).to eq(Date.parse('1999-01-01')) }

describe '#works' do
subject { product.works }
subject { selection.works }

let(:kbart) { 'kbart' }
let(:work) { 'work' }

before do
allow(kbart_file).to receive(:kbarts).and_return([kbart])
allow(LibPtgBox::Work).to receive(:new).with(product, kbart).and_return(work)
allow(LibPtgBox::Work).to receive(:new).with(selection, kbart).and_return(work)
end

it { is_expected.to contain_exactly(work) }
Expand Down
18 changes: 9 additions & 9 deletions spec/modules/lib_ptg_box/unmarshaller/root_folder_spec.rb
Expand Up @@ -3,21 +3,21 @@
require 'rails_helper'

RSpec.describe LibPtgBox::Unmarshaller::RootFolder do
describe '#family_folders' do
subject { described_class.family_folders }
describe '#collections' do
subject { described_class.sub_folders }

let(:box_service) { instance_double(Box::Service, 'box_service') }
let(:lib_ptg_box_folder) { instance_double(Box::Folder, 'lib_ptg_box_folder') }
let(:box_family_folder) { instance_double(Box::Folder, 'box_family_folder') }
let(:family_folder) { instance_double(LibPtgBox::Unmarshaller::FamilyFolder, 'family_folder') }
let(:root_box_folder) { instance_double(Box::Folder, 'root_box_folder') }
let(:sub_box_folder) { instance_double(Box::Folder, 'sub_box_folder') }
let(:sub_folder) { instance_double(LibPtgBox::Unmarshaller::SubFolder, 'sub_folder') }

before do
allow(Box::Service).to receive(:new).and_return(box_service)
allow(box_service).to receive(:folder).with(LibPtgBox::BOX_LIB_PTG_BOX_PATH).and_return(lib_ptg_box_folder)
allow(lib_ptg_box_folder).to receive(:folders).and_return([box_family_folder])
allow(LibPtgBox::Unmarshaller::FamilyFolder).to receive(:new).with(box_family_folder).and_return(family_folder)
allow(box_service).to receive(:folder).with(LibPtgBox::BOX_LIB_PTG_BOX_PATH).and_return(root_box_folder)
allow(root_box_folder).to receive(:folders).and_return([sub_box_folder])
allow(LibPtgBox::Unmarshaller::SubFolder).to receive(:new).with(sub_box_folder).and_return(sub_folder)
end

it { is_expected.to contain_exactly(family_folder) }
it { is_expected.to contain_exactly(sub_folder) }
end
end
Expand Up @@ -2,18 +2,18 @@

require 'rails_helper'

RSpec.describe LibPtgBox::Unmarshaller::FamilyFolder do
subject(:family_folder) { described_class.new(family_box_folder) }
RSpec.describe LibPtgBox::Unmarshaller::SubFolder do
subject(:sub_folder) { described_class.new(sub_box_folder) }

let(:family_box_folder) { instance_double(Box::Folder, 'family_box_folder') }
let(:sub_box_folder) { instance_double(Box::Folder, 'sub_box_folder') }
let(:box_folders) { [] }

before do
allow(family_box_folder).to receive(:folders).and_return(box_folders)
allow(sub_box_folder).to receive(:folders).and_return(box_folders)
end

describe '#kbart_folder' do
subject(:kbart_folder) { family_folder.kbart_folder }
subject(:kbart_folder) { sub_folder.kbart_folder }

it { expect(kbart_folder.name).to eq('NullFolder') }

Expand All @@ -26,7 +26,7 @@
end

describe '#marc_folder' do
subject(:marc_folder) { family_folder.marc_folder }
subject(:marc_folder) { sub_folder.marc_folder }

it { expect(marc_folder.name).to eq('NullFolder') }

Expand All @@ -39,7 +39,7 @@
end

describe '#cataloging_marc_folder' do
subject(:cataloging_marc_folder) { family_folder.cataloging_marc_folder }
subject(:cataloging_marc_folder) { sub_folder.cataloging_marc_folder }

it { expect(cataloging_marc_folder.name).to eq('NullFolder') }

Expand Down
6 changes: 3 additions & 3 deletions spec/modules/lib_ptg_box/work_spec.rb
Expand Up @@ -3,10 +3,10 @@
require 'rails_helper'

RSpec.describe LibPtgBox::Work do
subject(:work) { described_class.new(product, kbart) }
subject(:work) { described_class.new(selection, kbart) }

let(:product) { instance_double(LibPtgBox::Product, 'product', product_family: product_family) }
let(:product_family) { instance_double(LibPtgBox::ProductFamily, 'product_family', catalog: catalog) }
let(:selection) { instance_double(LibPtgBox::Selection, 'selection', collection: collection) }
let(:collection) { instance_double(LibPtgBox::Collection, 'collection', catalog: catalog) }
let(:catalog) { instance_double(LibPtgBox::Catalog, 'catalog') }
let(:kbart) { instance_double(LibPtgBox::Unmarshaller::Kbart, 'kbart', doi: 'doi') }
let(:marc) { nil }
Expand Down

0 comments on commit 3e5930e

Please sign in to comment.