Class: Characterizable::Characteristic

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(base, name, options = {}, &block) ⇒ Characteristic

Returns a new instance of Characteristic.



166
167
168
169
170
171
172
173
# File 'lib/characterizable.rb', line 166

def initialize(base, name, options = {}, &block)
  @base = base
  @name = name
  @trumps = Array.wrap options.delete(:trumps)
  @prerequisite = options.delete(:prerequisite)
  @options = options
  Blockenspiel.invoke block, self if block_given?
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



161
162
163
# File 'lib/characterizable.rb', line 161

def base
  @base
end

#nameObject (readonly)

Returns the value of attribute name.



162
163
164
# File 'lib/characterizable.rb', line 162

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



165
166
167
# File 'lib/characterizable.rb', line 165

def options
  @options
end

#prerequisiteObject (readonly)

Returns the value of attribute prerequisite.



164
165
166
# File 'lib/characterizable.rb', line 164

def prerequisite
  @prerequisite
end

#trumpsObject (readonly)

Returns the value of attribute trumps.



163
164
165
# File 'lib/characterizable.rb', line 163

def trumps
  @trumps
end

Instance Method Details

#as_jsonObject



174
175
176
# File 'lib/characterizable.rb', line 174

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

#characteristicsObject



180
181
182
# File 'lib/characterizable.rb', line 180

def characteristics
  base.characteristics
end

#effective?(universe, ignoring = nil) ⇒ Boolean

Returns:

  • (Boolean)


197
198
199
# File 'lib/characterizable.rb', line 197

def effective?(universe, ignoring = nil)
  known?(universe) and revealed? universe and not trumped? universe, ignoring
end

#inspectObject



177
178
179
# File 'lib/characterizable.rb', line 177

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

#known?(universe) ⇒ Boolean

Returns:

  • (Boolean)


191
192
193
# File 'lib/characterizable.rb', line 191

def known?(universe)
  not value(universe).nil?
end

#potential?(universe) ⇒ Boolean

Returns:

  • (Boolean)


194
195
196
# File 'lib/characterizable.rb', line 194

def potential?(universe)
  not known?(universe) and revealed? universe and not trumped? universe
end

#revealed?(universe) ⇒ Boolean

Returns:

  • (Boolean)


213
214
215
216
# File 'lib/characterizable.rb', line 213

def revealed?(universe)
  return true if prerequisite.nil?
  characteristics[prerequisite].effective? universe
end

#reveals(other_name, other_options = {}, &block) ⇒ Object



218
219
220
# File 'lib/characterizable.rb', line 218

def reveals(other_name, other_options = {}, &block)
  base.has other_name, other_options.merge(:prerequisite => name), &block
end

#trumped?(universe, ignoring = nil) ⇒ Boolean

Returns:

  • (Boolean)


200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/characterizable.rb', line 200

def trumped?(universe, ignoring = nil)
  characteristics.each do |_, other|
    if other.trumps.include? name and not ignoring == other.name
      if trumps.include? other.name
        # special case: mutual trumping. current characteristic is trumped if its friend is otherwise effective and it is not otherwise effective
        return true if other.effective? universe, name and not effective? universe, other.name
      else
        return true if other.effective? universe
      end
    end
  end
  false
end

#value(universe) ⇒ Object



183
184
185
186
187
188
189
190
# File 'lib/characterizable.rb', line 183

def value(universe)
  case universe
  when Hash
    universe[name]
  else
    universe.send name if universe.respond_to?(name)
  end
end