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.
11 12 13 14 15 16 17 |
# File 'lib/biosphere/state.rb', line 11 def initialize(filename = nil) if filename load(filename) else self.reset() end end |
Instance Attribute Details
#filename ⇒ Object
Returns the value of attribute filename.
9 10 11 |
# File 'lib/biosphere/state.rb', line 9 def filename @filename end |
#node(name = nil) ⇒ Object
Returns the value of attribute node.
9 10 11 |
# File 'lib/biosphere/state.rb', line 9 def node @node end |
Instance Method Details
#load(filename = nil) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/biosphere/state.rb', line 23 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
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/biosphere/state.rb', line 53 def load_from_structure!(structure) if @node # Objects which might get removed when building a new state need to be stored away before the merge feature_manifests = {} @node.data[:deployments].each do |name, deployment| if deployment[:feature_manifests] feature_manifests[name] = ::DeepDup.deep_dup(deployment[:feature_manifests]) end end # Merge the structured state into the current state @node.data.deep_merge(structure.data, {:overwrite_arrays => true}) # Now re-apply the stored objects on top of the merged state so that removes are handled correctly @node.data[:deployments].each do |name, deployment| removed = {} if feature_manifests[name] feature_manifests[name].each do |section, manifests| diff = deployment[:feature_manifests][section] - manifests removed[section] = diff if diff.length > 0 end deployment[:feature_manifests] = feature_manifests[name] end if removed.length > 0 deployment[:removed_feature_manifests] = removed end end else @node = structure end end |
#merge!(settings) ⇒ Object
40 41 42 |
# File 'lib/biosphere/state.rb', line 40 def merge!(settings) @node.merge!(settings) end |
#save(filename = nil) ⇒ Object
44 45 46 47 48 49 50 51 |
# File 'lib/biosphere/state.rb', line 44 def save(filename=nil) if !filename && @filename filename = @filename end str = Marshal.dump(@node) File.write(filename, str) puts "Saving state to #{filename}" end |