# Author:: Eric Crane (mailto:eric.crane@mac.com) # Copyright:: Copyright (c) 2019 Eric Crane. All rights reserved. # # The persistance manager. # Keeps a collection of object-file mappings, and then # uses mappings to know how/where to save updated objects. # module GlooLang module Persist class PersistMan attr_reader :maps, :mech # # Contructor for the persistence manager. # def initialize( engine ) @engine = engine @maps = [] @mech = @engine.platform.getFileMech( @engine ) end # # Save one object to the file. # def save( name = '' ) if name.nil? || name.strip.empty? save_all else save_one name end end # # Save one object to the file. # def save_all @maps.each( &:save ) end # # Save one object to the file. # def save_one( name ) ref = GlooLang::Core::Pn.new( @engine, name ) obj = ref.resolve pn = get_full_path_name name fs = GlooLang::Persist::FileStorage.new( @engine, pn, obj ) fs.save end # # Load the object from the file. # def load( name ) pns = get_full_path_names name return unless pns pns.each do |pn| @engine.log.debug "Load file(s) at: #{pn}" fs = GlooLang::Persist::FileStorage.new( @engine, pn ) fs.load @maps << fs @engine.event_manager.on_load fs.obj end end # # Get the full path and name of the file. # def get_full_path_names( name ) return nil if name.strip.empty? if name.strip[ -1 ] == '*' return @mech.get_all_files_in( name[ 0..-2 ] ) else return @mech.expand( name ) end end # # Check to see if a given path name refers to a gloo object file. # def gloo_file?( name ) return @mech.valid?( name ) end # # Get the default file extention. # def file_ext return @mech.file_ext end # # Print out all object - persistance mappings. # This is a debugging tool. # def show_maps @maps.each do |o| puts " \t #{o.pn} \t #{o.obj.name}" end end end end end