Skip to content

Instantly share code, notes, and snippets.

@foca
Created September 5, 2010 01:20
Show Gist options
  • Save foca/565646 to your computer and use it in GitHub Desktop.
Save foca/565646 to your computer and use it in GitHub Desktop.
module Pathfinder
class Feat
class List
class NoMatchingSlotsAvailable < StandardError; end
include Enumerable
def initialize
@feats = []
end
def add_slot(&rule)
# general feats go at the end, so restricted feats get filled in first
slot = Slot.new(rule)
rule ? @feats.unshift(slot) : @feats.push(slot)
end
def <<(feat)
slot = @feats.detect {|slot| slot.matches?(feat) }
slot ? slot.fill(feat) : raise(NoMatchingSlotsAvailable, "#{feat} can't be added")
end
def to_a
@feats.map {|slot| slot.feat }
end
def each(&block)
to_a.each(&block)
end
class Slot
attr_reader :feat
def initialize(rule)
@rule = rule
end
def matches?(feat)
empty? && (@rule.nil? || @rule[feat])
end
def fill(feat)
@feat = feat
end
def empty?
@feat.nil?
end
end
end
end
end
character.feats.add_slot # any feat
character.feats.add_slot {|feat| feat.metamagic? }
character.feats.add_slot {|feat| feat.combat? }
character.feats << some_metamagic_feat # fills the slot that matches feat.metamagic?
character.feats << some_metamagic_feat # fills the general slot, because the metamagic one is filled
character.feats << some_non_combat_feat # raises because the only available slot allows combat feats only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment