Class: GlooLang::Persist::PersistMan

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo_lang/persist/persist_man.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine) ⇒ PersistMan

Contructor for the persistence manager.



18
19
20
21
22
# File 'lib/gloo_lang/persist/persist_man.rb', line 18

def initialize( engine )
  @engine = engine
  @maps = []
  @mech = @engine.platform.getFileMech( @engine )
end

Instance Attribute Details

#mapsObject (readonly)

Returns the value of attribute maps.



13
14
15
# File 'lib/gloo_lang/persist/persist_man.rb', line 13

def maps
  @maps
end

#mechObject (readonly)

Returns the value of attribute mech.



13
14
15
# File 'lib/gloo_lang/persist/persist_man.rb', line 13

def mech
  @mech
end

Instance Method Details

#file_extObject

Get the default file extention.



137
138
139
# File 'lib/gloo_lang/persist/persist_man.rb', line 137

def file_ext
  return @mech.file_ext
end

#find_file_storage(obj) ⇒ Object

Find the objects FileStorage in the list.



104
105
106
107
108
109
110
111
# File 'lib/gloo_lang/persist/persist_man.rb', line 104

def find_file_storage( obj )
  @maps.each do |o|
    return o if ( o.obj.pn === obj.pn )
  end

  # It was not found, so return nil.
  return nil
end

#get_full_path_names(name) ⇒ Object

Get the full path and name of the file.



117
118
119
120
121
122
123
124
125
# File 'lib/gloo_lang/persist/persist_man.rb', line 117

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

#gloo_file?(name) ⇒ Boolean

Check to see if a given path name refers to a gloo object file.

Returns:

  • (Boolean)


130
131
132
# File 'lib/gloo_lang/persist/persist_man.rb', line 130

def gloo_file?( name )
  return @mech.valid?( name )
end

#load(name) ⇒ Object

Load the object from the file.



52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/gloo_lang/persist/persist_man.rb', line 52

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

#reload(obj) ⇒ Object

Re-load the given object from file.



93
94
95
96
97
98
99
# File 'lib/gloo_lang/persist/persist_man.rb', line 93

def reload( obj )
  fs = find_file_storage( obj )
  return unless fs

  @engine.heap.unload obj
  fs.load
end

#reload_allObject

Reload all objects.



81
82
83
84
85
86
87
88
# File 'lib/gloo_lang/persist/persist_man.rb', line 81

def reload_all
  return unless @maps
  
  @maps.each do |fs|
    @engine.heap.unload fs.obj
    fs.load
  end
end

#save(name = '') ⇒ Object

Save one object to the file.



27
28
29
# File 'lib/gloo_lang/persist/persist_man.rb', line 27

def save( name = '' )
  name.blank? ? save_all : save_one( name )
end

#save_allObject

Save one object to the file.



34
35
36
# File 'lib/gloo_lang/persist/persist_man.rb', line 34

def save_all
  @maps.each( &:save )
end

#save_one(name) ⇒ Object

Save one object to the file.



41
42
43
44
45
46
47
# File 'lib/gloo_lang/persist/persist_man.rb', line 41

def save_one( name )
  ref = GlooLang::Core::Pn.new( @engine, name )
  obj = ref.resolve

  fs = find_file_storage( obj )
  fs.save
end

#show_mapsObject

Print out all object - persistance mappings. This is a debugging tool.



145
146
147
148
149
# File 'lib/gloo_lang/persist/persist_man.rb', line 145

def show_maps
  @maps.each do |o|
    puts " \t #{o.pn} \t #{o.obj.name}"
  end
end

#unload(obj) ⇒ Object

The given object is unloading. Do any necessary clean up here.



69
70
71
72
73
74
75
76
# File 'lib/gloo_lang/persist/persist_man.rb', line 69

def unload( obj )
  @maps.each_with_index do |o, i|
    if o.obj.pn === obj.pn
      @maps.delete_at( i )
      return
    end
  end
end