Sha256: bfef6a70aa34c833ee1c14a89345a1af34b3203dadd9421a53ed40ee4cb196f8

Contents?: true

Size: 1.09 KB

Versions: 14

Compression:

Stored size: 1.09 KB

Contents

require 'fileutils'

module UniverseCompiler
  module Entity

    module Persistence

      attr_accessor :source_uri

      def self.load(uri)
        entity = YAML.load_file uri
        entity.source_uri = uri
        entity
      end

      def save(uri = source_uri, raise_error: true, force_save: false)
        if force_save
          if valid? raise_error: false
            UniverseCompiler.logger.debug "Forcing '#{self.as_path}' save."
          else
            UniverseCompiler.logger.warn "Forcing '#{self.as_path}' save despite it is invalid !"
          end
        else
          valid? raise_error: raise_error
        end
        FileUtils.mkpath File.dirname(uri)
        File.write uri, to_yaml, mode: 'w'
        self.source_uri = uri
        UniverseCompiler.logger.debug "Saved '#{self.as_path}' to '#{uri}'"
        self
      end

      def delete
        universe.delete self
        unless self.source_uri.nil?
          FileUtils.rm self.source_uri
          UniverseCompiler.logger.debug "Deleted '#{self.as_path}' (removed file '#{self.source_uri}'"
        end
      end

    end

  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
universe_compiler-0.5.6 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.5.5 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.5.4 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.5.3 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.5.2 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.5.1 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.4.3 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.4.2 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.4.1 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.4.0 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.3.12 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.3.11 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.3.10 lib/universe_compiler/entity/persistence.rb
universe_compiler-0.3.9 lib/universe_compiler/entity/persistence.rb