Class: LanguageCards::CardCollection
- Inherits:
-
Object
- Object
- LanguageCards::CardCollection
- Defined in:
- lib/language_cards/card_collection.rb
Defined Under Namespace
Classes: EmptyCollection
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
Instance Method Summary collapse
- #[](value) ⇒ Object
- #cards ⇒ Object
- #children ⇒ Object
-
#classes(s = nil) ⇒ Object
Recursively build class course names.
- #collection? ⇒ Boolean
- #correct?(input, comp_bitz) ⇒ Boolean
-
#initialize(hsh, name = nil) ⇒ CardCollection
constructor
RULE: Always compare by value regardless of key value ordering in mapping.
- #mapped_as ⇒ Object
- #rand ⇒ Object
- #select_collection(string) ⇒ Object
Constructor Details
#initialize(hsh, name = nil) ⇒ CardCollection
RULE: Always compare by value regardless of key value ordering in mapping.
(This allows for duplicate spellings of the same character translation)
10 11 12 13 14 15 16 17 18 |
# File 'lib/language_cards/card_collection.rb', line 10 def initialize hsh, name=nil @hsh = hsh @name = I18n.t "LanguageName.#{name}" if name send :_build @mappings = @hsh["mapping"] if @mappings @cards = @hsh.select {|k,v| !v.is_a? Mappings} end end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'lib/language_cards/card_collection.rb', line 6 def name @name end |
Instance Method Details
#[](value) ⇒ Object
31 32 33 34 |
# File 'lib/language_cards/card_collection.rb', line 31 def [](value) raise EmptyCollection unless collection? @cards[value] end |
#cards ⇒ Object
72 73 74 |
# File 'lib/language_cards/card_collection.rb', line 72 def cards @cards || raise(I18n.t 'Errors.UpperCollection') end |
#children ⇒ Object
52 53 54 55 56 |
# File 'lib/language_cards/card_collection.rb', line 52 def children @hsh.select {|k,v| v.is_a? CardCollection}.values.map do |cc| {cc.name => cc} end.inject(:merge) end |
#classes(s = nil) ⇒ Object
Recursively build class course names
22 23 24 25 26 27 28 29 |
# File 'lib/language_cards/card_collection.rb', line 22 def classes s=nil s = @name.nil? ? nil : [s, @name].compact.join(JOIN) @hsh.select do |k,v| [CardCollection, Mappings].include? v.class end.flat_map do |k,v| v.send :classes, s end end |
#collection? ⇒ Boolean
68 69 70 |
# File 'lib/language_cards/card_collection.rb', line 68 def collection? !!@cards end |
#correct?(input, comp_bitz) ⇒ Boolean
42 43 44 45 |
# File 'lib/language_cards/card_collection.rb', line 42 def correct? input, comp_bitz raise EmptyCollection unless collection? @comparator.match? input, comp_bitz end |
#mapped_as ⇒ Object
47 48 49 50 |
# File 'lib/language_cards/card_collection.rb', line 47 def mapped_as raise EmptyCollection unless collection? @comparator.mapped_as end |
#rand ⇒ Object
36 37 38 39 40 |
# File 'lib/language_cards/card_collection.rb', line 36 def rand raise EmptyCollection unless collection? v = @cards.keys.sample @comparator.given(v, @cards[v]) end |
#select_collection(string) ⇒ Object
58 59 60 61 62 63 64 65 66 |
# File 'lib/language_cards/card_collection.rb', line 58 def select_collection string first, tail = string.split(JOIN, 2) if collection? @comparator = @mappings.select_mapping(first) return self else children[first].select_collection tail end end |