class Demiurge::DSL::TopLevelBuilder
This is the top-level DSL
Builder class, for parsing the top syntactic level of the World Files.
@since 0.0.1
Public Class Methods
Constructor for a new set of World Files and their top-level state.
# File lib/demiurge/dsl.rb, line 427 def initialize(options = {}) @zones = [] @engine = options["engine"] || ::Demiurge::Engine.new(types: @@types, state: []) end
It's hard to figure out where and how to register types and plugins for the World File format. By their nature, they need to be in place before an Engine
exists, so that's not the right place. If they didn't exist before engines, we'd somehow need to register them with each engine as it was created. Since Engines keep track of that, that's exactly the same problem we're trying to solve, just for the Engine
builder. And it seems like “register this plugin with Demiurge
World Files” is more of a process-global operation than a per-Engine operation. So these wind up in awkward spots.
# File lib/demiurge/dsl.rb, line 483 def self.register_type(name, klass) if @@types[name.to_s] raise("Attempting to re-register type #{name.inspect} with a different class!") unless @@types[name.to_s] == klass else @@types[name.to_s] = klass end end
Public Instance Methods
Return the built Engine
, but first call the .finished_init callback. This will make sure that cached and duplicated data structures are properly filled in.
# File lib/demiurge/dsl.rb, line 494 def built_engine @engine.finished_init @engine end
For now, this just declares an InertStateItem
for a given name. It doesn't change the behavior at all. It just keeps that item name from being “orphaned” state that doesn't correspond to any state item.
Later, this may be a way to describe how important or transitory state is - is it reset like a zone? Completely transient? Cleared per reboot?
@param item_name [String] The item name for scoping the state in the Engine
@param options [Hash] Options about the InertStateItem
@option options [String] zone The zone this InertStateItem
considers itself to be in, defaults to “admin” @option options [Hash] state The initial state Hash @option options [String] type The object type to instantiate, if not InertStateItem
@return [void] @since 0.0.1
# File lib/demiurge/dsl.rb, line 448 def inert(item_name, options = {}) zone_name = options["zone"] || "admin" state = options["state"] || {} state.merge! "zone" => zone_name, "home_zone" => zone_name inert_item = ::Demiurge::StateItem.from_name_type(@engine, options["type"] || "InertStateItem", item_name, state) @engine.register_state_item(inert_item) nil end
Start a new Zone
block, using a ZoneBuilder
.
# File lib/demiurge/dsl.rb, line 458 def zone(name, options = {}, &block) if @zones.any? { |z| z.name == name } # Reopening an existing zone builder = ZoneBuilder.new(name, @engine, options.merge("existing" => @zones.detect { |z| z.name == name })) else builder = ZoneBuilder.new(name, @engine, options) end builder.instance_eval(&block) new_zone = builder.built_item @zones |= [ new_zone ] if new_zone # Add if not already present nil end