Class: Biosphere::State
- Inherits:
-
Object
- Object
- Biosphere::State
- Defined in:
- lib/biosphere/state.rb
Instance Attribute Summary collapse
-
#filename ⇒ Object
Returns the value of attribute filename.
-
#node(name = nil) ⇒ Object
Returns the value of attribute node.
Instance Method Summary collapse
-
#initialize(filename = nil) ⇒ State
constructor
A new instance of State.
- #load(filename = nil) ⇒ Object
- #load_from_structure!(structure) ⇒ Object
- #merge!(settings) ⇒ Object
- #reset ⇒ Object
- #save(filename = nil) ⇒ Object
Constructor Details
#initialize(filename = nil) ⇒ State
Returns a new instance of State.
10 11 12 13 14 15 16 |
# File 'lib/biosphere/state.rb', line 10 def initialize(filename = nil) if filename load(filename) else self.reset() end end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
8 9 10 |
# File 'lib/biosphere/state.rb', line 8 def filename @filename end |
#node(name = nil) ⇒ Object
Returns the value of attribute node.
8 9 10 |
# File 'lib/biosphere/state.rb', line 8 def node @node end |
Instance Method Details
#load(filename = nil) ⇒ Object
22 23 24 25 26 27 28 29 |
# File 'lib/biosphere/state.rb', line 22 def load(filename=nil) if filename @filename = filename end data = Marshal.load(File.read(@filename)) #puts "Loading data from file #{@filename}: #{data}" load_from_structure!(data) end |
#load_from_structure!(structure) ⇒ Object
52 53 54 55 56 57 58 |
# File 'lib/biosphere/state.rb', line 52 def load_from_structure!(structure) if @node @node.data.deep_merge(structure.data) else @node = structure end end |
#merge!(settings) ⇒ Object
39 40 41 |
# File 'lib/biosphere/state.rb', line 39 def merge!(settings) @node.merge!(settings) end |
#reset ⇒ Object
18 19 20 |
# File 'lib/biosphere/state.rb', line 18 def reset() @node = Node.new end |
#save(filename = nil) ⇒ Object
43 44 45 46 47 48 49 50 |
# File 'lib/biosphere/state.rb', line 43 def save(filename=nil) if !filename && @filename filename = @filename end str = Marshal.dump(@node) File.write(filename, str) puts "Saving state to #{filename}" end |