From 3590a0e3b8149a339b7e18b8974e5bdc2046aff5 Mon Sep 17 00:00:00 2001 From: Petr Schmied Date: Sat, 12 Sep 2015 16:19:11 +0200 Subject: [PATCH] Sources: better formatting --- .../config/templates/sources_example.yml | 45 +++++----- lib/budik/sources.rb | 52 ++++++++---- spec/lib/budik/sources_spec.rb | 82 ++++++++++++------- 3 files changed, 107 insertions(+), 72 deletions(-) diff --git a/lib/budik/config/templates/sources_example.yml b/lib/budik/config/templates/sources_example.yml index 6d7da53..e85fe49 100644 --- a/lib/budik/config/templates/sources_example.yml +++ b/lib/budik/config/templates/sources_example.yml @@ -1,24 +1,23 @@ --- -- "path" -- - - "path1" - - "path2" -- - name: "name" - path: "path" -- - name: "name" - category: ["category1", "subcategory1"] - path: "path" -- - name: "name" - category: ["category1", "subcategory2"] - path: ["path1", "path2"] -- - name: "name" - category: - - "category2" - - "subcategory1" - path: - - "path1" - - "path2" +category1: + subcategory1: + - "path" + - + - "path1" + - "path2" + - name: + - "path" + - name: + - "path1" + - "path2" + subcategory2: + - "path" +category2: + subcategory1: + subsubcategory1: + - "path1" + - "path2" + subsubcategory2: + - "path3" + subcategory2: + - "path4" diff --git a/lib/budik/sources.rb b/lib/budik/sources.rb index e9d5407..bb9aa33 100644 --- a/lib/budik/sources.rb +++ b/lib/budik/sources.rb @@ -57,27 +57,43 @@ def download_youtube(address, dir) end end - def parse(sources, mods = nil) - sources.each do |item| - if item.is_a? Array - normalized_item = { name: '', category: ['default'], path: []} - item.each do |subitem| - normalized_item[:name] += (subitem + ' + ') - normalized_item[:path] << subitem + def normalize(item, category) + normalized_item = { name: '', category: category, path: [] } + + if item.is_a? Array + item.each do |subitem| + normalized_item[:name] += (subitem + ' + ') + normalized_item[:path] << subitem + end + 3.times { normalized_item[:name].chop! } + elsif item.is_a? Hash + item.each do |name, paths| + normalized_item[:name] = name + paths.each do |path| + normalized_item[:path] << path end - 3.times { normalized_item[:name].chop! } - elsif item.is_a? Hash - normalized_item = {} - normalized_item[:name] = item['name'] - normalized_item[:category] = item['category'] ? item['category'] : ['default'] - normalized_item[:path] = item['path'] - elsif item.is_a? String - normalized_item = { name: item, category: ['default'], path: item } - else # TODO: test - fail Config.instance.lang.sources.invalid_item item.to_s end + elsif item.is_a? String + normalized_item[:name] = item + normalized_item[:path] << item + else + fail 'Invalid item in sources' # TODO + end - @sources << normalized_item + return normalized_item + end + + def parse(sources, mods = nil, current_category = []) + sources.each do |category, contents| + if contents.is_a? Hash + parse(contents, mods, current_category + ([] << category)) + elsif contents.is_a? Array + contents.each do |item| + @sources << normalize(item, current_category + ([] << category)) + end + else + fail 'Invalid sources format' # TODO + end end mods = (mods.is_a? String) ? parse_mods(mods) : mods diff --git a/spec/lib/budik/sources_spec.rb b/spec/lib/budik/sources_spec.rb index e7cc343..6627ae5 100644 --- a/spec/lib/budik/sources_spec.rb +++ b/spec/lib/budik/sources_spec.rb @@ -10,19 +10,31 @@ Budik::Sources.instance.sources = [] Budik::Sources.instance.parse(YAML.load_file('./lib/budik/config/templates/sources_example.yml')) - mods = {adds: [['category1']], rms: [['subcategory1']]} + mods = { + adds: [ + ['category1', 'subcategory2'], + ['category2', 'subcategory1'] + ], + rms: [ + ['category2', 'subcategory1', 'subsubcategory1'] + ] + } Budik::Sources.instance.apply_mods(mods) sources_expected_result = [ - {name: 'name', + {name: 'path', category: ['category1', 'subcategory2'], - path: ['path1', 'path2']} + path: ['path']}, + + {name: 'path3', + category: ['category2', 'subcategory1', 'subsubcategory2'], + path: ['path3']} ] expect(Budik::Sources.instance.sources).to eq sources_expected_result end end -describe Budik::Sources, '#download' do +describe Budik::Sources, '#download' do # TODO: rewrite context 'using specified number' do it 'downloads an item' do config = Budik::Config.instance @@ -30,18 +42,14 @@ config.options['sources']['download']['dir'] = './spec/' sources_example = [ - { - name: 'Test item 1', - category: ['test'], - path: 'https://www.youtube.com/watch?v=ghxo4OMh1YU' - }, - - { - name: 'Test item 2', - category: ['test'], - path: ['https://www.youtube.com/watch?v=tPEE9ZwTmy0', - 'https://www.youtube.com/watch?v=wGyUP4AlZ6I'] - } + {name: 'Test item 1', + category: ['test'], + path: ['https://www.youtube.com/watch?v=ghxo4OMh1YU']}, + + {name: 'Test item 2', + category: ['test'], + path: ['https://www.youtube.com/watch?v=tPEE9ZwTmy0', + 'https://www.youtube.com/watch?v=wGyUP4AlZ6I']} ] Budik::Sources.instance.sources = sources_example @@ -90,28 +98,40 @@ Budik::Sources.instance.parse(sources_example) sources_expected_result = [ {name: 'path', - category: ['default'], - path: 'path'}, + category: ['category1', 'subcategory1'], + path: ['path']}, - {name: 'path1 + path2', - category: ['default'], + {name: 'path1 + path2', + category: ['category1', 'subcategory1'], path: ['path1', 'path2']}, {name: 'name', - category: ['default'], - path: 'path'}, + category: ['category1', 'subcategory1'], + path: ['path']}, {name: 'name', category: ['category1', 'subcategory1'], - path: 'path'}, + path: ['path1', 'path2']}, - {name: 'name', + {name: 'path', category: ['category1', 'subcategory2'], - path: ['path1', 'path2']}, + path: ['path']}, - {name: 'name', - category: ['category2', 'subcategory1'], - path: ['path1', 'path2']} + {name: 'path1', + category: ['category2', 'subcategory1', 'subsubcategory1'], + path: ['path1']}, + + {name: 'path2', + category: ['category2', 'subcategory1', 'subsubcategory1'], + path: ['path2']}, + + {name: 'path3', + category: ['category2', 'subcategory1', 'subsubcategory2'], + path: ['path3']}, + + {name: 'path4', + category: ['category2', 'subcategory2'], + path: ['path4']} ] expect(Budik::Sources.instance.sources).to eq sources_expected_result @@ -120,14 +140,14 @@ context 'with modifiers' do it 'parses sources to program usable format' do - mods_example = '.subcategory1 .default category1' + mods_example = '.subcategory1 category1' sources_example = YAML.load_file('./lib/budik/config/templates/sources_example.yml') Budik::Sources.instance.sources = [] Budik::Sources.instance.parse(sources_example, mods_example) sources_expected_result = [ - {name: 'name', + {name: 'path', category: ['category1', 'subcategory2'], - path: ['path1', 'path2']} + path: ['path']} ] expect(Budik::Sources.instance.sources).to eq sources_expected_result