Class: UniverseCompiler::Persistence::BasicYamlEngine

Inherits:
Object
  • Object
show all
Defined in:
lib/universe_compiler/persistence/basic_yaml_engine.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(universe, universe_uri) ⇒ BasicYamlEngine

Returns a new instance of BasicYamlEngine.



10
11
12
13
# File 'lib/universe_compiler/persistence/basic_yaml_engine.rb', line 10

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

Instance Attribute Details

#universeObject

Returns the value of attribute universe.



8
9
10
# File 'lib/universe_compiler/persistence/basic_yaml_engine.rb', line 8

def universe
  @universe
end

#universe_uriObject

Returns the value of attribute universe_uri.



8
9
10
# File 'lib/universe_compiler/persistence/basic_yaml_engine.rb', line 8

def universe_uri
  @universe_uri
end

Instance Method Details

#export_entity(entity, raise_error: true, force_save: false) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/universe_compiler/persistence/basic_yaml_engine.rb', line 42

def export_entity(entity, raise_error: true, force_save: false)
  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, raise_error: raise_error, force_save: force_save
  UniverseCompiler.logger.info 'Exporting entity "%s" to file "%s"' % [entity.name, entity_file]
  UniverseCompiler.logger.debug "Saved entity:\n%s" % [entity.to_yaml]
end

#export_universeObject



15
16
17
18
19
20
# File 'lib/universe_compiler/persistence/basic_yaml_engine.rb', line 15

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

#import_entity(uri) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/universe_compiler/persistence/basic_yaml_engine.rb', line 51

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

#import_universe(recursive: false, stop_on_error: true, &block) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/universe_compiler/persistence/basic_yaml_engine.rb', line 22

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