Class: Card
Overview
A data structure consisting of a set of traits.
A "trait" is a key value-pair consisting of a *type*
(i.e. "suit") and a *value* (i.e. "spades").
Instance Attribute Summary collapse
-
#archive ⇒ Object
readonly
Returns the value of attribute archive.
-
#to_s ⇒ Object
Returns the value of attribute to_s.
-
#traits ⇒ Object
readonly
A Hash of all the card’s traits.
Instance Method Summary collapse
-
#[](type) ⇒ Object
Allows Card values to be accessed like a Hash.
-
#add_trait(type, value) ⇒ Object
(also: #change_trait, #change, #[]=)
Creates a trait and adds it to ‘#traits`.
-
#remove_trait(type) ⇒ String, Numeric
Deletes a trait from ‘#traits`.
-
#trait?(trait) ⇒ Boolean
(also: #include?)
Checks the given trait is a proper subset of ‘#traits`.
-
#type?(type) ⇒ Boolean
(also: #has?)
Checks for the given type.
-
#types ⇒ Object
ADD TO TOMES.
-
#value?(*values) ⇒ Boolean
(also: #is?)
Checks for an instance of the given values.
-
#values ⇒ Object
ADD TO TOMES.
Instance Attribute Details
#archive ⇒ Object
Returns the value of attribute archive.
35 |
# File 'tomes/components/deck_builder/card.rb', line 35 attr_reader :traits, :archive |
#to_s ⇒ Object
Returns the value of attribute to_s.
36 37 38 |
# File 'tomes/components/deck_builder/card.rb', line 36 def to_s @to_s end |
#traits ⇒ Object
A Hash of all the card’s traits.
35 36 37 |
# File 'tomes/components/deck_builder/card.rb', line 35 def traits @traits end |
Instance Method Details
#[](type) ⇒ Object
Allows Card values to be accessed like a Hash.
62 63 64 |
# File 'tomes/components/deck_builder/card.rb', line 62 def [](type) traits[type] end |
#add_trait(type, value) ⇒ Object Also known as: change_trait, change, []=
*Do not* distribute traits to Cards unevenly. This may cause other classes that handle them to function improperly. If certain Cards have a trait that others don’t, give those other Cards the same type with a default value. See the Hanafuda demo for an example.
Creates a trait and adds it to ‘#traits`.
46 47 48 |
# File 'tomes/components/deck_builder/card.rb', line 46 def add_trait(type, value) traits[type] = value end |
#remove_trait(type) ⇒ String, Numeric
Deletes a trait from ‘#traits`.
56 57 58 |
# File 'tomes/components/deck_builder/card.rb', line 56 def remove_trait(type) traits.delete(type) end |
#trait?(trait) ⇒ Boolean Also known as: include?
It is possible to check for multiple traits at a time, but this method will return true only if *all of them* exist in ‘#traits`.
Checks the given trait is a proper subset of ‘#traits`.
87 88 89 |
# File 'tomes/components/deck_builder/card.rb', line 87 def trait?(trait) traits >= trait end |
#type?(type) ⇒ Boolean Also known as: has?
Checks for the given type.
68 69 70 |
# File 'tomes/components/deck_builder/card.rb', line 68 def type?(type) traits.include? type end |
#types ⇒ Object
ADD TO TOMES
93 94 95 |
# File 'tomes/components/deck_builder/card.rb', line 93 def types traits.keys end |
#value?(*values) ⇒ Boolean Also known as: is?
Checks for an instance of the given values. Returns true if *at least one of them* are found.
75 76 77 78 79 |
# File 'tomes/components/deck_builder/card.rb', line 75 def value?(*values) values.map do |value| traits.value? value end.any? end |
#values ⇒ Object
ADD TO TOMES
98 99 100 |
# File 'tomes/components/deck_builder/card.rb', line 98 def values traits.values end |