Sha256: 6c07115a39aebc196a165f7e8b557f1bd0bd3c39a786a490654cfc87dfbf8b2d

Contents?: true

Size: 1.86 KB

Versions: 5

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

module Gamefic
  # Entities are the people, places, and things that exist in a Gamefic
  # narrative. Authors are encouraged to define Entity subclasses to create
  # entity types that have additional features or need special handling in
  # actions.
  #
  class Entity
    include Describable
    include Node

    def initialize **args
      klass = self.class
      defaults = {}
      while klass <= Entity
        defaults = klass.default_attributes.merge(defaults)
        klass = klass.superclass
      end
      defaults.merge(args).each_pair { |k, v| send "#{k}=", v }

      yield(self) if block_given?

      post_initialize
    end

    # This method can be overridden for additional processing after the entity
    # has been created.
    #
    def post_initialize; end

    # A freeform property dictionary.
    # Authors can use the session hash to assign custom properties to the
    # entity. It can also be referenced directly using [] without the method
    # name, e.g., entity.session[:my_value] or entity[:my_value].
    #
    # @return [Hash]
    def session
      @session ||= {}
    end

    # @param key [Symbol] The property's name
    # @return The value of the property
    def [](key)
      session[key]
    end

    # @param key [Symbol] The property's name
    # @param value The value to set
    def []=(key, value)
      session[key] = value
    end

    def inspect
      "#<#{self.class} #{name}>"
    end

    class << self
      # Set or update the default attributes for new instances.
      #
      def set_default **attrs
        default_attributes.merge! attrs
      end

      # A hash of default attributes when creating an instance.
      #
      # @return [Hash]
      def default_attributes
        @default_attributes ||= {}
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
gamefic-3.4.0 lib/gamefic/entity.rb
gamefic-3.3.0 lib/gamefic/entity.rb
gamefic-3.2.1 lib/gamefic/entity.rb
gamefic-3.2.0 lib/gamefic/entity.rb
gamefic-3.1.0 lib/gamefic/entity.rb