Skip to content

Commit

Permalink
Sources: better formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
JBlackN committed Sep 12, 2015
1 parent 14c7096 commit ba5d5aa
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 72 deletions.
45 changes: 22 additions & 23 deletions lib/budik/config/templates/sources_example.yml
Original file line number Diff line number Diff line change
@@ -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"
52 changes: 34 additions & 18 deletions lib/budik/sources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
82 changes: 51 additions & 31 deletions spec/lib/budik/sources_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,46 @@
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
config.load(Hash.new)
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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ba5d5aa

Please sign in to comment.