public
Last active

DSLs

  • Download Gist
context.md
Markdown
dsl.rb
Ruby
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
class Adventure
attr_reader \
:current_room,
:rooms
 
class Room
attr_accessor \
:exits,
:long,
:short
def initialize( short , long , exits )
@short = short
@long = long
@exits = exits
end
def long( val = false )
if val
self.long = val
else
self.long
end
end
def short( val = false )
if val
self.short = val
else
self.short
end
end
def door(direction, destination, &block)
if exits.has_key?( direction )
puts "Error: Room #{@current_room} already has an exit defined in #{direction}"
else
exits[ direction ] = {:where => destination, :condition => block_given? ? block : true}
end
end
end
def initialize
@rooms = Hash.new
@current_room = nil
end
 
def room(reference, &block)
@current_room =
if @rooms.has_key?(reference)
puts "Error: Room #{reference} has already been defined"
@rooms[ reference ]
else
@rooms[ reference ] = Room.new '' , '' , Hash.new
end
 
@current_room.instance_eval( &block ) if block_given?
end
end
 
class Object
def adventure(&block)
a = Adventure.new
a.instance_eval( &block ) if block_given?
end
end
 
adventure do
room :cave do
short "You are in a large cave"
long "You are in a very large cave"
door :west, :another_cave
door :east, :exit do
if player.has_item(:key)
true
else
false
end
end
end
 
room :another_cave do
short "A cave"
long "Oh great ... another cave"
door :east, :cave
end
 
room :exit do
short "Freedom"
long "Freeeeeeeeeeedom!!!!!!"
door :west, :cave
end
 
require 'pp'
pp @rooms
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.