Sha256: db06b6af711b95405283ccfb0f7ed75023f0bab9e4cb21349d237ca4866f3321

Contents?: true

Size: 1.98 KB

Versions: 15

Compression:

Stored size: 1.98 KB

Contents

require 'fileutils'

module UniverseCompiler
  module Persistence

    class BasicYamlEngine

      attr_accessor :universe, :universe_uri

      def initialize(universe, universe_uri)
        @universe = universe
        @universe_uri = universe_uri
      end

      def export_universe
        FileUtils.mkdir_p universe_uri
        universe.get_entities.each do |entity|
          export_entity entity
        end
      end

      def import_universe(recursive: false, stop_on_error: true, &block)
        sub_search_path = recursive ? '**' : '*'
        glob_pattern = File.join universe_uri, sub_search_path, '*.yaml'
        Dir.glob(glob_pattern) do |file|
          UniverseCompiler.logger.debug "Importing '#{file}' entity file."
          new_entity = nil
          begin
            new_entity = import_entity file
          rescue => e
            raise e if stop_on_error
          end
          if new_entity.nil?
            UniverseCompiler.logger.debug "#{e.message}\nBacktrace:\n#{e.backtrace.join("\n\t")}"
            UniverseCompiler.logger.warn "Could not load entity from file '#{file}' because '#{e.message}'"
          else
            block.call new_entity if block_given?
          end
        end
      end

      def export_entity(entity)
        dir = File.join(universe_uri, entity.type.to_s)
        FileUtils.mkdir_p dir
        entity_file = entity.source_uri || File.join(dir, "#{entity.name}.yaml")
        entity.save entity_file
        UniverseCompiler.logger.info 'Exporting entity "%s" to file "%s"' % [entity.name, entity_file]
        UniverseCompiler.logger.debug "Saved entity:\n%s" % [entity.to_yaml]
      end

      def import_entity(uri)
        entity = UniverseCompiler::Entity::Persistence.load uri
        universe.add entity
        # Fix references link to universe
        entity.traverse_fields do |leaf|
          if leaf.is_a? UniverseCompiler::Entity::Reference
            leaf.universe = universe
          end
        end
        entity
      end



    end

  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
universe_compiler-0.3.8 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.3.7 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.3.6 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.3.5 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.3.4 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.3.3 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.3.2 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.3.1 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.3.0 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.2.16 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.2.15 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.2.14 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.2.13 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.2.12 lib/universe_compiler/persistence/basic_yaml_engine.rb
universe_compiler-0.2.11 lib/universe_compiler/persistence/basic_yaml_engine.rb