class Biosphere::State

Attributes

filename[RW]
node[RW]

Public Class Methods

new(filename = nil) click to toggle source
# File lib/biosphere/state.rb, line 11
def initialize(filename = nil)
    if filename
        load(filename)
    else
        self.reset()
    end
end

Public Instance Methods

load(filename=nil) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# File lib/biosphere/state.rb, line 40
def merge!(settings)
    @node.merge!(settings)
end
reset() click to toggle source
# File lib/biosphere/state.rb, line 19
def reset()
    @node = Node.new
end
save(filename=nil) click to toggle source
# 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