Class: Characterizable::Characteristic
- Inherits:
-
Object
- Object
- Characterizable::Characteristic
- Includes:
- Blockenspiel::DSL
- Defined in:
- lib/characterizable/characteristic.rb
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#display(universe) ⇒ Object
readonly
Returns the value of attribute display.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#prerequisite ⇒ Object
readonly
Returns the value of attribute prerequisite.
-
#trumps ⇒ Object
readonly
Returns the value of attribute trumps.
Instance Method Summary collapse
- #as_json ⇒ Object
- #can_trump?(other) ⇒ Boolean
- #characteristics ⇒ Object
- #displays(&block) ⇒ Object
- #effective?(universe, ignoring = []) ⇒ Boolean
-
#initialize(base, name, options = {}, &block) ⇒ Characteristic
constructor
A new instance of Characteristic.
- #inspect ⇒ Object
- #known?(universe) ⇒ Boolean
- #mutually_trumped?(universe, other, ignoring) ⇒ Boolean
- #potential?(universe) ⇒ Boolean
- #revealed?(universe) ⇒ Boolean
- #reveals(other_name, other_options = {}, &block) ⇒ Object
- #trumped?(universe, ignoring = []) ⇒ Boolean
- #value(universe) ⇒ Object
Constructor Details
#initialize(base, name, options = {}, &block) ⇒ Characteristic
Returns a new instance of Characteristic.
5 6 7 8 9 10 11 12 13 |
# File 'lib/characterizable/characteristic.rb', line 5 def initialize(base, name, = {}, &block) @base = base @name = name @trumps = Array.wrap .delete(:trumps) @prerequisite = .delete(:prerequisite) @display = .delete(:display) @options = Blockenspiel.invoke block, self if block_given? end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
3 4 5 |
# File 'lib/characterizable/characteristic.rb', line 3 def base @base end |
#display(universe) ⇒ Object (readonly)
Returns the value of attribute display.
3 4 5 |
# File 'lib/characterizable/characteristic.rb', line 3 def display @display end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
3 4 5 |
# File 'lib/characterizable/characteristic.rb', line 3 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
3 4 5 |
# File 'lib/characterizable/characteristic.rb', line 3 def @options end |
#prerequisite ⇒ Object (readonly)
Returns the value of attribute prerequisite.
3 4 5 |
# File 'lib/characterizable/characteristic.rb', line 3 def prerequisite @prerequisite end |
#trumps ⇒ Object (readonly)
Returns the value of attribute trumps.
3 4 5 |
# File 'lib/characterizable/characteristic.rb', line 3 def trumps @trumps end |
Instance Method Details
#as_json ⇒ Object
15 16 17 |
# File 'lib/characterizable/characteristic.rb', line 15 def as_json(*) { :name => name, :trumps => trumps, :prerequisite => prerequisite, :options => } end |
#can_trump?(other) ⇒ Boolean
70 71 72 |
# File 'lib/characterizable/characteristic.rb', line 70 def can_trump?(other) trumps.include?(other.name) end |
#characteristics ⇒ Object
23 24 25 |
# File 'lib/characterizable/characteristic.rb', line 23 def characteristics base.characteristics end |
#displays(&block) ⇒ Object
89 90 91 |
# File 'lib/characterizable/characteristic.rb', line 89 def displays(&block) @display = block end |
#effective?(universe, ignoring = []) ⇒ Boolean
49 50 51 52 53 |
# File 'lib/characterizable/characteristic.rb', line 49 def effective?(universe, ignoring = []) known?(universe) and revealed?(universe) and not trumped?(universe, ignoring) end |
#inspect ⇒ Object
19 20 21 |
# File 'lib/characterizable/characteristic.rb', line 19 def inspect "<Characterizable::Characteristic name=#{name.inspect} trumps=#{trumps.inspect} prerequisite=#{prerequisite.inspect} options=#{.inspect}>" end |
#known?(universe) ⇒ Boolean
41 42 43 |
# File 'lib/characterizable/characteristic.rb', line 41 def known?(universe) not value(universe).nil? end |
#mutually_trumped?(universe, other, ignoring) ⇒ Boolean
74 75 76 77 |
# File 'lib/characterizable/characteristic.rb', line 74 def mutually_trumped?(universe, other, ignoring) # special case: mutual trumping. current characteristic is trumped if its friend is otherwise effective and it is not otherwise effective other.effective?(universe, ignoring + [name]) and not effective?(universe, ignoring + [other.name]) end |
#potential?(universe) ⇒ Boolean
45 46 47 |
# File 'lib/characterizable/characteristic.rb', line 45 def potential?(universe) not known?(universe) and revealed? universe and not trumped? universe end |
#revealed?(universe) ⇒ Boolean
79 80 81 82 |
# File 'lib/characterizable/characteristic.rb', line 79 def revealed?(universe) return true if prerequisite.nil? characteristics[prerequisite].effective? universe end |
#reveals(other_name, other_options = {}, &block) ⇒ Object
85 86 87 |
# File 'lib/characterizable/characteristic.rb', line 85 def reveals(other_name, = {}, &block) base.has other_name, .merge(:prerequisite => name), &block end |
#trumped?(universe, ignoring = []) ⇒ Boolean
55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/characterizable/characteristic.rb', line 55 def trumped?(universe, ignoring = []) characteristics.each do |_, other| next if ignoring.include?(other.name) if other.can_trump? self if can_trump?(other) return mutually_trumped?(universe, other, ignoring) elsif other.effective?(universe, ignoring + [name]) return true end end end false end |
#value(universe) ⇒ Object
27 28 29 30 31 32 33 34 |
# File 'lib/characterizable/characteristic.rb', line 27 def value(universe) case universe when Hash universe[name] else universe.send name if universe.respond_to?(name) end end |