class Wire::State
State
is a container for a state model :reek: DataClump
Attributes
changed[R]
did something change? since last change or load/save
project[RW]
backref to project
state[RW]
- Hash
-
of all
state
entries, key=name, value=StateEntry
Public Class Methods
new()
click to toggle source
creates an empty state
# File lib/wire/model/state.rb, line 24 def initialize clean end
Public Instance Methods
changed?()
click to toggle source
returns changed flad
# File lib/wire/model/state.rb, line 82 def changed? changed end
check(type, name, state_to_check)
click to toggle source
checks if type
resource name
is in state state_to_check
# File lib/wire/model/state.rb, line 64 def check(type, name, state_to_check) key = (make_key(type, name)) entry = @state[key] return entry.state == state_to_check if entry false end
clean()
click to toggle source
cleans the state
# File lib/wire/model/state.rb, line 29 def clean @state = {} # +changed+ indicates wether state has changed @changed = false end
down?(type, name)
click to toggle source
checks if resource type
name
is down
# File lib/wire/model/state.rb, line 77 def down?(type, name) check(type, name, :down) end
load()
click to toggle source
load state from statefile (within project target dir)
# File lib/wire/model/state.rb, line 108 def load statefile_filename = state_filename if File.exist?(statefile_filename) && File.file?(statefile_filename) && File.readable?(statefile_filename) $log.debug "Loading state from #{statefile_filename}" @state = YAML.load_file(statefile_filename) else $log.debug 'No statefile found.' clean end @changed = false end
make_key(type, name)
click to toggle source
combines type
and name
into a key suitable for the hash
# File lib/wire/model/state.rb, line 52 def make_key(type, name) "%#{type}%#{name}" end
save()
click to toggle source
save current state to statefile (within project target dir)
# File lib/wire/model/state.rb, line 94 def save unless @changed $log.debug 'Not saving state, nothing changed' return end statefile_filename = state_filename $log.debug "Saving state to #{statefile_filename}" File.open(statefile_filename, 'w') do |file| file.puts state.to_yaml end @changed = false end
state?(type, name)
click to toggle source
checks if we have a state for resource given by type
and name
# File lib/wire/model/state.rb, line 58 def state?(type, name) @state.key?(make_key(type, name)) end
state_filename()
click to toggle source
construct name of state file
# File lib/wire/model/state.rb, line 123 def state_filename File.join(@project.vartmp_dir, '.state.yaml') end
to_pretty_s()
click to toggle source
calls to_pretty_s
on a state entries
# File lib/wire/model/state.rb, line 87 def to_pretty_s @state.reduce([]) do |arr, entry| arr << entry[1].to_pretty_s end.join(',') end
up?(type, name)
click to toggle source
checks if resource type
name
is up
# File lib/wire/model/state.rb, line 72 def up?(type, name) check(type, name, :up) end
update(type, name, state)
click to toggle source
adds or updates state type
i.e. :bridge name
i.e. :br0 state
, one of :up, :down, :unknown
# File lib/wire/model/state.rb, line 39 def update(type, name, state) key = (make_key(type, name)) entry = @state[key] if entry (entry.state != state) && @changed = true entry.state = state else @state.store(key, StateEntry.new(type, name, state)) @changed = true end end