Class: Characterizable::Characteristic

Inherits:
Object
  • Object
show all
Includes:
Blockenspiel::DSL
Defined in:
lib/characterizable/characteristic.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {}, &block)
  @base = base
  @name = name
  @trumps = Array.wrap options.delete(:trumps)
  @prerequisite = options.delete(:prerequisite)
  @display = options.delete(:display)
  @options = options
  Blockenspiel.invoke block, self if block_given?
end

Instance Attribute Details

#baseObject (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

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/characterizable/characteristic.rb', line 3

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/characterizable/characteristic.rb', line 3

def options
  @options
end

#prerequisiteObject (readonly)

Returns the value of attribute prerequisite.



3
4
5
# File 'lib/characterizable/characteristic.rb', line 3

def prerequisite
  @prerequisite
end

#trumpsObject (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_jsonObject



15
16
17
# File 'lib/characterizable/characteristic.rb', line 15

def as_json(*)
  { :name => name, :trumps => trumps, :prerequisite => prerequisite, :options => options }
end

#can_trump?(other) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
# File 'lib/characterizable/characteristic.rb', line 70

def can_trump?(other)
  trumps.include?(other.name)
end

#characteristicsObject



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

Returns:

  • (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

#inspectObject



19
20
21
# File 'lib/characterizable/characteristic.rb', line 19

def inspect
  "<Characterizable::Characteristic name=#{name.inspect} trumps=#{trumps.inspect} prerequisite=#{prerequisite.inspect} options=#{options.inspect}>"
end

#known?(universe) ⇒ Boolean

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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

Returns:

  • (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, other_options = {}, &block)
  base.has other_name, other_options.merge(:prerequisite => name), &block
end

#trumped?(universe, ignoring = []) ⇒ Boolean

Returns:

  • (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