lib/characterizable.rb in characterizable-0.0.15 vs lib/characterizable.rb in characterizable-0.0.16
- old
+ new
@@ -30,12 +30,12 @@
# In Ruby 1.9, running select/reject/etc. gives you back a hash
if RUBY_VERSION < '1.9'
def to_hash
Hash.new.replace self
end
- def to_json(*)
- to_hash.to_json
+ def as_json(*)
+ to_hash
end
def reject(&block)
inject(Characterizable::BetterHash.new) do |memo, ary|
unless block.call(*ary)
memo[ary[0]] = ary[1]
@@ -63,31 +63,31 @@
end
end
end
class Snapshot < BetterHash
- attr_reader :target
- def initialize(target)
- @target = target
+ attr_reader :universe
+ def initialize(universe)
+ @universe = universe
_take_snapshot
end
def _take_snapshot
- target.characterizable_base.characteristics.each do |_, c|
- if c.known?(target)
- if c.effective?(target)
- self[c.name] = c.value(target)
- elsif !c.untrumped?(target)
+ universe.characterizable_base.characteristics.each do |_, c|
+ if c.known?(universe)
+ if c.effective?(universe)
+ self[c.name] = c.value(universe)
+ elsif c.trumped?(universe)
trumped_keys.push c.name
- elsif !c.revealed?(target)
+ elsif !c.revealed?(universe)
wasted_keys.push c.name
lacking_keys.push c.prerequisite
end
end
end
end
def []=(key, value)
- target.expire_snapshot!
+ universe.expire_snapshot!
super
end
def wasted_keys
@wasted_keys ||= Array.new
end
@@ -96,23 +96,23 @@
end
def lacking_keys
@lacking_keys ||= Array.new
end
def effective
- target.characterizable_base.characteristics.select { |_, c| c.effective?(self) }
+ universe.characterizable_base.characteristics.select { |_, c| c.effective?(self) }
end
def potential
- target.characterizable_base.characteristics.select { |_, c| c.potential?(self) }
+ universe.characterizable_base.characteristics.select { |_, c| c.potential?(self) }
end
def wasted
- target.characterizable_base.characteristics.slice(*wasted_keys)
+ universe.characterizable_base.characteristics.slice(*wasted_keys)
end
def lacking
- target.characterizable_base.characteristics.slice(*(lacking_keys - wasted_keys))
+ universe.characterizable_base.characteristics.slice(*(lacking_keys - wasted_keys))
end
def trumped
- target.characterizable_base.characteristics.slice(*trumped_keys)
+ universe.characterizable_base.characteristics.slice(*trumped_keys)
end
end
module ClassMethods
def characterize(&block)
@@ -124,12 +124,10 @@
end
end
class CharacteristicAlreadyDefined < ArgumentError
end
- class CyclicalTrumping < ArgumentError
- end
class Base
attr_reader :klass
def initialize(klass)
@klass = klass
@@ -167,53 +165,52 @@
@name = name
@trumps = Array.wrap options.delete(:trumps)
@prerequisite = options.delete(:prerequisite)
@options = options
Blockenspiel.invoke block, self if block_given?
- trumps.each do |trump|
- if c = characteristics[trump] and c.trumps.include? name
- raise CyclicalTrumping, "On #{base.klass}, '#{c.name}' and '#{name}' trump each other"
- end
- end
end
- def to_json(*)
- { :name => name, :trumps => trumps, :prerequisite => prerequisite, :options => options }.to_json
+ def as_json(*)
+ { :name => name, :trumps => trumps, :prerequisite => prerequisite, :options => options }
end
def inspect
"<Characterizable::Characteristic name=#{name.inspect} trumps=#{trumps.inspect} prerequisite=#{prerequisite.inspect} options=#{options.inspect}>"
end
- def trumped_by
- characteristics.select { |_, c| c.trumps.include? name }
- end
def characteristics
base.characteristics
end
- def value(target)
- case target
+ def value(universe)
+ case universe
when Hash
- target[name]
+ universe[name]
else
- target.send name if target.respond_to?(name)
+ universe.send name if universe.respond_to?(name)
end
end
- def known?(target)
- !value(target).nil?
+ def known?(universe)
+ not value(universe).nil?
end
- def potential?(target)
- !known?(target) and revealed? target and untrumped? target
+ def potential?(universe)
+ not known?(universe) and revealed? universe and not trumped? universe
end
- def effective?(target)
- known?(target) and revealed? target and untrumped? target
+ def effective?(universe, ignoring = nil)
+ known?(universe) and revealed? universe and not trumped? universe, ignoring
end
- def untrumped?(target)
- return true if trumped_by.empty?
- trumped_by.none? do |_, c|
- c.effective? target
+ 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
- def revealed?(target)
+ def revealed?(universe)
return true if prerequisite.nil?
- characteristics[prerequisite].effective? target
+ characteristics[prerequisite].effective? universe
end
include Blockenspiel::DSL
def reveals(other_name, other_options = {}, &block)
base.has other_name, other_options.merge(:prerequisite => name), &block
end