Class: Card

Inherits:
Object show all
Defined in:
tomes/components/deck_builder/card.rb

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

Instance Method Summary collapse

Instance Attribute Details

#archiveObject

Returns the value of attribute archive.



35
# File 'tomes/components/deck_builder/card.rb', line 35

attr_reader :traits, :archive

#to_sObject

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

#traitsObject

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.

Parameters:



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, []=

Note:

*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`.

Parameters:



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`.

Parameters:

  • type (Symbol)

    The type of trait to be deleted.

Returns:

  • (String, Numeric)

    The value of the recently deleted trait.



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?

Note:

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`.

Parameters:

  • trait (Hash)

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


68
69
70
# File 'tomes/components/deck_builder/card.rb', line 68

def type?(type)
  traits.include? type
end

#typesObject

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.

Returns:

  • (Boolean)


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

#valuesObject

ADD TO TOMES



98
99
100
# File 'tomes/components/deck_builder/card.rb', line 98

def values
  traits.values
end