Sha256: b35b2f78006e3679c1dd70c49a65e4f9f7d90c011dd54c0a8b983e4590b28280

Contents?: true

Size: 1.64 KB

Versions: 1

Compression:

Stored size: 1.64 KB

Contents

module EacLauncher
  class Context
    class InstanceManager
      include ::Eac::SimpleCache

      def initialize(context)
        @context = context
      end

      private

      def instances_uncached
        (cached_instances ? cached_instances : search_instances).select(&:included?)
      end

      def search_instances
        cache_instances(::EacLauncher::Context::InstanceDiscovery.new(@context).instances)
      end

      def cached_instances
        return nil if @context.recache
        return nil unless cached_instances_file_content
        CachedInstances.new(cached_instances_file_content).instances
      end

      def cached_instances_file_content_uncached
        r = YAML.load_file(cache_file_path)
        r.is_a?(::Hash) ? r : nil
      rescue Errno::ENOENT
        nil
      end

      def cache_instances(instances)
        r = Hash[instances.map { |i| [i.logical, i.to_h] }]
        ::File.write(cache_file_path, r.to_yaml)
        instances
      end

      def cache_file_path
        ::File.join(@context.cache_root, 'instances.yml')
      end

      class CachedInstances
        def initialize(content)
          @content = content
          @instances = {}
        end

        def instances
          @content.keys.map { |k| by_logical_path(k) }
        end

        def by_logical_path(k)
          return @instances[k] if @instances.key?(k)
          h = @content[k]
          parent_instance = h[:parent] ? by_logical_path(h[:parent]) : nil
          path = ::EacLauncher::Paths::Logical.from_h(@context, h)
          @instances[k] = ::EacLauncher::Instances::Base.instanciate(path, parent_instance)
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
eac_launcher-0.4.0 lib/eac_launcher/context/instance_manager.rb