Class: Gloo::Persist::FileSaver

Inherits:
Object
  • Object
show all
Defined in:
lib/gloo/persist/file_saver.rb

Instance Method Summary collapse

Constructor Details

#initialize(pn, obj) ⇒ FileSaver

Set up a file storage for an object.



14
15
16
17
# File 'lib/gloo/persist/file_saver.rb', line 14

def initialize( pn, obj )
  @pn = pn
  @obj = obj
end

Instance Method Details

#get_obj(obj, indent = 0) ⇒ Object

Convert an object to textual representation. This is a recursive function.



38
39
40
41
42
43
44
45
# File 'lib/gloo/persist/file_saver.rb', line 38

def get_obj( obj, indent = 0 )
  t = tabs( indent )
  str = "#{t}#{obj.name} [#{obj.type_display}] : #{obj.value_display}\n"
  obj.children.each do |child|
    str << get_obj( child, indent + 1 )
  end
  return str
end

#saveObject

Save the object to the file.



22
23
24
25
# File 'lib/gloo/persist/file_saver.rb', line 22

def save
  data = get_obj( @obj )
  File.write( @pn, data )
end

#tabs(indent = 0) ⇒ Object

Get string of tabs for indentation.



30
31
32
# File 'lib/gloo/persist/file_saver.rb', line 30

def tabs( indent = 0 )
  return "\t" * indent
end