Sha256: 6126ec4e177c9ab588700b41ccd60fed8d17c05f43e0b9d6bf4e4b98593a6a0e

Contents?: true

Size: 1.9 KB

Versions: 23

Compression:

Stored size: 1.9 KB

Contents

require 'digest'

module UniverseCompiler
  module Entity

    class Reference

      class << self

        def references
          @references ||= {}
        end

        def new_instance(entity = nil)
          k = entity_to_cache_key entity
          return references[k] if references[k]
          e = new entity
          references[k] = e
          e
        end

        def entity_to_cache_key(entity_or_reference)
          universe_name = entity_or_reference.universe.nil? ? '' : entity_or_reference.universe.name
          universe_name ||= ''
          [entity_or_reference.type, entity_or_reference.name, universe_name]
        end

        private :new

      end

      yaml_tag '!psref'

      include UniverseCompiler::Utils::ErrorPropagation
      include UniverseCompiler::Entity::Conversion

      attr_reader :type, :name, :universe

      def initialize(entity = nil)
        self.entity = entity unless entity.nil?
      end

      def entity=(entity)
        @type = entity.type
        @name = entity.name
        self.universe = entity.universe
      end

      def universe=(another_universe)
        k = self.class.entity_to_cache_key self
        self.class.references.delete k
        @universe = another_universe
        self.class.entity_to_cache_key self
      end

      def to_entity(raise_error: true)
        false_or_raise "Cannot get entity '#{to_composite_key.inspect}' if its universe is not defined!", raise_error: raise_error if universe.nil?
        entity = universe.get_entity *to_composite_key
        false_or_raise "Cannot find entity '#{to_composite_key.inspect}' in the universe '#{universe}'", raise_error: raise_error if entity.nil?
        entity
      end

      def encode_with(coder)
        coder['type'] = type
        coder['name'] = name
      end

      def init_with(coder)
        initialize
        @type = coder['type']
        @name = coder['name']
      end

    end

  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
universe_compiler-0.4.3 lib/universe_compiler/entity/reference.rb
universe_compiler-0.4.2 lib/universe_compiler/entity/reference.rb
universe_compiler-0.4.1 lib/universe_compiler/entity/reference.rb
universe_compiler-0.4.0 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.12 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.11 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.10 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.9 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.8 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.7 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.6 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.5 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.4 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.3 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.2 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.1 lib/universe_compiler/entity/reference.rb
universe_compiler-0.3.0 lib/universe_compiler/entity/reference.rb
universe_compiler-0.2.16 lib/universe_compiler/entity/reference.rb
universe_compiler-0.2.15 lib/universe_compiler/entity/reference.rb
universe_compiler-0.2.14 lib/universe_compiler/entity/reference.rb