Skip to content

Commit

Permalink
Basic source parsing.
Browse files Browse the repository at this point in the history
  • Loading branch information
JBlackN committed Sep 10, 2015
1 parent d8b9ec6 commit e5bc85c
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/budik.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
#require './budik/devices.rb'
#require './budik/player.rb'
#require './budik/rng.rb'
#require './budik/sources.rb'
require './lib/budik/sources'
require './lib/budik/version'

module Budik
Expand Down
2 changes: 2 additions & 0 deletions lib/budik/config/lang/en.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
lang: "en" # Do not remove this line.
config:
example: "This is an example string."
sources:
invalid_item: "Item %1 is invalid."
24 changes: 24 additions & 0 deletions lib/budik/config/templates/sources_example.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
- "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"
72 changes: 72 additions & 0 deletions lib/budik/sources.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
module Budik
class Sources
include Singleton

def initialize
@sources = []
end

attr_accessor :sources

def apply_mods(mods)
modified_sources = []

@sources.each do |source|
add = false
mods[:adds].each do |mod|
add = true if mod == (source[:category] & mod)
end
mods[:rms].each do |mod|
add = false if mod == (source[:category] & mod)
end
modified_sources << source if add
end

@sources = modified_sources.uniq
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
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

@sources << normalized_item
end

mods = (mods.is_a? String) ? parse_mods(mods) : mods
apply_mods(mods) if mods
end

def parse_mods(mod_string)
parsed_mods = { adds: [], rms: [] }

mods = mod_string.split(' ')
mods.each do |m|
mod = m.split('.')
if mod.first == ''
mod.shift
parsed_mods[:rms] << mod
else
parsed_mods[:adds] << mod
end
end

return parsed_mods
end
end
end
85 changes: 85 additions & 0 deletions spec/lib/budik/sources_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#require 'r18n-core'
require 'singleton'
require 'spec_helper'
require 'yaml'

require 'budik/sources'

describe Budik::Sources, '#apply_mods' do
it 'filters sources by applying modifiers' do
Budik::Sources.instance.parse(YAML.load_file('./lib/budik/config/templates/sources_example.yml'))

mods = {adds: [['category1']], rms: [['subcategory1']]}
Budik::Sources.instance.apply_mods(mods)
sources_expected_result = [
{name: 'name',
category: ['category1', 'subcategory2'],
path: ['path1', 'path2']}
]

expect(Budik::Sources.instance.sources).to eq sources_expected_result
Budik::Sources.instance.sources = []
end
end

describe Budik::Sources, '#parse' do
context 'without modifiers' do
it 'parses sources to program usable format' do
sources_example = YAML.load_file('./lib/budik/config/templates/sources_example.yml')
Budik::Sources.instance.parse(sources_example)
sources_expected_result = [
{name: 'path',
category: ['default'],
path: 'path'},

{name: 'path1 + path2',
category: ['default'],
path: ['path1', 'path2']},

{name: 'name',
category: ['default'],
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']}
]

expect(Budik::Sources.instance.sources).to eq sources_expected_result
Budik::Sources.instance.sources = []
end
end

context 'with modifiers' do
it 'parses sources to program usable format' do
mods_example = '.subcategory1 .default category1'
sources_example = YAML.load_file('./lib/budik/config/templates/sources_example.yml')
Budik::Sources.instance.parse(sources_example, mods_example)
sources_expected_result = [
{name: 'name',
category: ['category1', 'subcategory2'],
path: ['path1', 'path2']}
]

expect(Budik::Sources.instance.sources).to eq sources_expected_result
Budik::Sources.instance.sources = []
end
end
end

describe Budik::Sources, '#parse_mods' do
it 'parses string with modifiers' do
parsed_mods = Budik::Sources.instance.parse_mods('a.b c.d.e .f .g.h i .j k.l .m.n.o')
expected_result = { adds: [['a', 'b'], ['c', 'd', 'e'], ['i'], ['k', 'l']], rms: [['f'], ['g', 'h'], ['j'], ['m', 'n', 'o']]}

expect(parsed_mods).to eq expected_result
end
end

0 comments on commit e5bc85c

Please sign in to comment.