Sha256: eb01883a00869e3b822ed1b43fcfb4723f91dd9b2396eee9dcc68c6a2d743d1b

Contents?: true

Size: 1.37 KB

Versions: 6

Compression:

Stored size: 1.37 KB

Contents

module Gamefic
  # The simplest class that can compose an object for use in a plot.
  # Most game objects, especially tangible items in the game, should derive
  # from the Entity class. Elements, on the other hand, can be used for
  # abstractions and ideas that don't have a physical presence but still might
  # need to be referenced in a command.
  #
  class Element
    include Gamefic::Describable
    include Gamefic::Serialize

    def initialize(args = {})
      klass = self.class
      defaults = {}
      while klass <= Element
        defaults = klass.default_attributes.merge(defaults)
        klass = klass.superclass
      end
      defaults.merge(args).each_pair do |k, v|
        public_send "#{k}=", v
      end
      post_initialize
      yield self if block_given?
    end

    def post_initialize
      # raise NotImplementedError, "#{self.class} must implement post_initialize"
    end

    class << self
      # Set or update the default values for new instances.
      #
      # @param attrs [Hash] The attributes to be merged into the defaults.
      def set_default attrs = {}
        default_attributes.merge! attrs
      end

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

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
gamefic-2.4.0 lib/gamefic/element.rb
gamefic-2.3.0 lib/gamefic/element.rb
gamefic-2.2.3 lib/gamefic/element.rb
gamefic-2.2.2 lib/gamefic/element.rb
gamefic-2.2.1 lib/gamefic/element.rb
gamefic-2.2.0 lib/gamefic/element.rb