Class: Budik::Sources
- Inherits:
-
Object
- Object
- Budik::Sources
- Includes:
- Singleton
- Defined in:
- lib/budik/sources.rb
Overview
‘Sources’ class loads and parses media sources file.
Instance Attribute Summary collapse
-
#sources ⇒ Object
Gets sources.
Instance Method Summary collapse
-
#apply_mods(mods) ⇒ Object
Applies category modifiers.
-
#apply_mods_check(category, mod) ⇒ Object
Checks if mod applies to category.
-
#count ⇒ Object
Returns total count of sources.
-
#get(number) ⇒ Object
Returns source by number.
-
#initialize ⇒ Sources
constructor
Initializes sources instance variable and strings in currently set language.
-
#normalize(source, category) ⇒ Object
Normalizes item.
-
#normalize_multiple_items(source, category) ⇒ Object
Normalizes unnamed source with multiple items.
-
#normalize_named_source(source, category) ⇒ Object
Normalizes named source.
-
#normalize_unnamed_source(source, category) ⇒ Object
Normalizes unnamed source with single item.
-
#parse(sources, current_category = []) ⇒ Object
Parses sources’ categories.
-
#parse_category(contents, category) ⇒ Object
Parses category contents.
-
#parse_mods(mods) ⇒ Object
Parses string of category modifiers into two arrays (adds, rms).
Constructor Details
Instance Attribute Details
#sources ⇒ Object
Gets sources
23 24 25 |
# File 'lib/budik/sources.rb', line 23 def sources @sources end |
Instance Method Details
#apply_mods(mods) ⇒ Object
Applies category modifiers.
-
Args:
-
mods-> Category modifiers (Hash).
-
30 31 32 33 34 35 36 37 38 |
# File 'lib/budik/sources.rb', line 30 def apply_mods(mods) @sources.keep_if do |source| mods[:adds].any? { |mod| apply_mods_check(source[:category], mod) } end @sources.delete_if do |source| mods[:rms].any? { |mod| apply_mods_check(source[:category], mod) } end end |
#apply_mods_check(category, mod) ⇒ Object
Checks if mod applies to category.
-
Args:
-
category-> Category to be checked (Array). -
mod-> Modifier to be checked (Array).
-
-
Returns:
-
true or false
-
48 49 50 51 52 53 54 55 |
# File 'lib/budik/sources.rb', line 48 def apply_mods_check(category, mod) mod_len = mod.length - 1 cat_len = category.length - 1 len = mod_len <= cat_len ? mod_len : cat_len map = category[0..len].zip(mod[0..len]).map { |c, m| c == m } !map.include? false end |
#count ⇒ Object
Returns total count of sources
58 59 60 |
# File 'lib/budik/sources.rb', line 58 def count @sources.length end |
#get(number) ⇒ Object
Returns source by number.
-
Args:
-
number-> Fixnum.
-
67 68 69 |
# File 'lib/budik/sources.rb', line 67 def get(number) @sources[number] end |
#normalize(source, category) ⇒ Object
Normalizes item.
-
Args:
-
item-> Item to normalize (Array, Hash or String). -
category-> Item’s category (Array).
-
-
Returns:
-
Normalized source (Hash).
-
-
Raises:
-
RuntimeError-> If item is not Array, Hash or String.
-
81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/budik/sources.rb', line 81 def normalize(source, category) case source when Array normalize_multiple_items(source, category) when Hash normalize_named_source(source, category) when String normalize_unnamed_source(source, category) else fail @strings.invalid_format end end |
#normalize_multiple_items(source, category) ⇒ Object
Normalizes unnamed source with multiple items.
-
Args:
-
source-> Source to normalize (Array). -
category-> Source’s category (Array).
-
-
Returns:
-
Normalized source (Hash).
-
102 103 104 |
# File 'lib/budik/sources.rb', line 102 def normalize_multiple_items(source, category) { name: source.join(' + '), category: category, path: source } end |
#normalize_named_source(source, category) ⇒ Object
Normalizes named source.
-
Args:
-
source-> Source to normalize (Hash). -
category-> Source’s category (Array).
-
-
Returns:
-
Normalized source (Hash).
-
114 115 116 |
# File 'lib/budik/sources.rb', line 114 def normalize_named_source(source, category) { name: source.keys[0], category: category, path: source.values[0] } end |
#normalize_unnamed_source(source, category) ⇒ Object
Normalizes unnamed source with single item.
-
Args:
-
source-> Source to normalize (String). -
category-> Source’s category (Array).
-
-
Returns:
-
Normalized source (Hash).
-
126 127 128 |
# File 'lib/budik/sources.rb', line 126 def normalize_unnamed_source(source, category) { name: source, category: category, path: [] << source } end |
#parse(sources, current_category = []) ⇒ Object
Parses sources’ categories.
-
Args:
-
sources-> Sources loaded from YAML (Hash). -
current_category-> Source’s category (Array).
-
-
Raises:
-
RuntimeError-> If category’s contents is not Array nor Hash.
-
138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/budik/sources.rb', line 138 def parse(sources, current_category = []) sources.each do |category, contents| case contents when Hash parse(contents, current_category + ([] << category)) when Array parse_category(contents, current_category + ([] << category)) else fail @strings.invalid_format end end end |
#parse_category(contents, category) ⇒ Object
Parses category contents.
-
Args:
-
contents-> Category’s contents (Array). -
category-> Source’s category (Array).
-
157 158 159 |
# File 'lib/budik/sources.rb', line 157 def parse_category(contents, category) contents.each { |source| @sources << normalize(source, category) } end |
#parse_mods(mods) ⇒ Object
Parses string of category modifiers into two arrays (adds, rms).
-
Args:
-
mods-> Category modifiers (String).
-
-
Returns:
-
Parsed modifiers (Hash).
-
168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/budik/sources.rb', line 168 def parse_mods(mods) parsed_mods = { adds: [], rms: [] } mods.split(' ').each do |mod| if mod.split('.').first.empty? parsed_mods[:rms] << mod.split('.').drop(1) else parsed_mods[:adds] << mod.split('.') end end parsed_mods end |