From 93691d0e2601eeb5715714b4fb92840bb3b3ff8b Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 5 Mar 2016 17:20:40 +0900 Subject: [PATCH] add new methods: Book#catalog=(catalog) and Catalog.new(obj) --- lib/review/book/base.rb | 4 ++++ lib/review/catalog.rb | 6 +++++- test/test_catalog.rb | 17 +++++++++++++++++ 3 files changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/review/book/base.rb b/lib/review/book/base.rb index c3e04a90e..000196622 100644 --- a/lib/review/book/base.rb +++ b/lib/review/book/base.rb @@ -201,6 +201,10 @@ def catalog @catalog end + def catalog=(catalog) + @catalog = catalog + end + def read_CHAPS if catalog catalog.chaps diff --git a/lib/review/catalog.rb b/lib/review/catalog.rb index 60d3271cf..079d5390e 100644 --- a/lib/review/catalog.rb +++ b/lib/review/catalog.rb @@ -3,7 +3,11 @@ module ReVIEW class Catalog def initialize(file) - @yaml = YAML.load(file.read) + if file.respond_to? :read + @yaml = YAML.load(file.read) + else ## as Object + @yaml = file + end @yaml ||= {} end diff --git a/test/test_catalog.rb b/test/test_catalog.rb index 616144566..90fd46c86 100644 --- a/test/test_catalog.rb +++ b/test/test_catalog.rb @@ -81,6 +81,16 @@ def test_postdef assert_equal(exp.chomp, sut.postdef) end + def test_from_object + sut = Catalog.new(yaml_hash) + exp =<<-EOS +ch01.re +ch02.re + EOS + assert_equal(exp.chomp, sut.chaps) + end + + private def yaml StringIO.new <<-EOS @@ -103,6 +113,13 @@ def yaml EOS end + def yaml_hash + {"PREDEF" => %w(pre01.re pre02.re), + "CHAPS" => %w(ch01.re ch02.re), + "APPENDIX" => %w(post01.re post02.re), + "POSTDEF" => %w(back01.re back02.re)} + end + def yaml_with_parts StringIO.new <<-EOS CHAPS: