Class: Wire::State

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/wire/model/state.rb

Overview

State is a container for a state model :reek:DataClump

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeState

creates an empty state



24
25
26
# File 'lib/wire/model/state.rb', line 24

def initialize
  clean
end

Instance Attribute Details

#changedObject (readonly)

did something change? since last change or load/save



19
20
21
# File 'lib/wire/model/state.rb', line 19

def changed
  @changed
end

#projectObject

backref to project



17
18
19
# File 'lib/wire/model/state.rb', line 17

def project
  @project
end

#stateObject

Hash

of all state entries, key=name, value=StateEntry



15
16
17
# File 'lib/wire/model/state.rb', line 15

def state
  @state
end

Instance Method Details

#changed?Boolean

returns changed flad

Returns:

  • (Boolean)


82
83
84
# File 'lib/wire/model/state.rb', line 82

def changed?
  changed
end

#check(type, name, state_to_check) ⇒ Object

checks if type resource name is in state state_to_check



64
65
66
67
68
69
# 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

#cleanObject

cleans the state



29
30
31
32
33
# File 'lib/wire/model/state.rb', line 29

def clean
  @state = {}
  # +changed+ indicates wether state has changed
  @changed = false
end

#down?(type, name) ⇒ Boolean

checks if resource type name is down

Returns:

  • (Boolean)


77
78
79
# File 'lib/wire/model/state.rb', line 77

def down?(type, name)
  check(type, name, :down)
end

#loadObject

load state from statefile (within project target dir)



108
109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object

combines type and name into a key suitable for the hash



52
53
54
# File 'lib/wire/model/state.rb', line 52

def make_key(type, name)
  "%#{type}%#{name}"
end

#saveObject

save current state to statefile (within project target dir)



94
95
96
97
98
99
100
101
102
103
104
105
# 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) ⇒ Boolean

checks if we have a state for resource given by type and name

Returns:

  • (Boolean)


58
59
60
# File 'lib/wire/model/state.rb', line 58

def state?(type, name)
  @state.key?(make_key(type, name))
end

#state_filenameObject

construct name of state file



123
124
125
# File 'lib/wire/model/state.rb', line 123

def state_filename
  File.join(@project.vartmp_dir, '.state.yaml')
end

#to_pretty_sObject

calls to_pretty_s on a state entries



87
88
89
90
91
# 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) ⇒ Boolean

checks if resource type name is up

Returns:

  • (Boolean)


72
73
74
# File 'lib/wire/model/state.rb', line 72

def up?(type, name)
  check(type, name, :up)
end

#update(type, name, state) ⇒ Object

adds or updates state type i.e. :bridge name i.e. :br0 state, one of :up, :down, :unknown



39
40
41
42
43
44
45
46
47
48
49
# 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