Class: StateMate::StateSet
- Inherits:
-
Object
- Object
- StateMate::StateSet
- Defined in:
- lib/state_mate/state_set.rb
Overview
Definitions
Defined Under Namespace
Classes: State
Instance Attribute Summary collapse
-
#changes ⇒ Object
readonly
Returns the value of attribute changes.
-
#new_values ⇒ Object
readonly
Returns the value of attribute new_values.
-
#read_values ⇒ Object
readonly
Returns the value of attribute read_values.
-
#rollback_errors ⇒ Object
readonly
Returns the value of attribute rollback_errors.
-
#spec ⇒ Object
Returns the value of attribute spec.
-
#states ⇒ Object
readonly
Returns the value of attribute states.
-
#states_to_change ⇒ Object
readonly
Returns the value of attribute states_to_change.
-
#write_error ⇒ Object
readonly
Returns the value of attribute write_error.
-
#written_states ⇒ Object
readonly
Returns the value of attribute written_states.
Class Method Summary collapse
Instance Method Summary collapse
- #add(adapter, key, directive, value, options = {}) ⇒ Object
- #execute ⇒ Object
-
#initialize ⇒ StateSet
constructor
from_spec.
Constructor Details
#initialize ⇒ StateSet
from_spec
178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/state_mate/state_set.rb', line 178 def initialize @spec = nil @states = [] @read_values = {} @states_to_change = [] @new_values = [] @written_states = [] @write_error = nil # map of states to errors raised when trying to rollback @rollback_errors = {} # report of changes made @changes = {} end |
Instance Attribute Details
#changes ⇒ Object (readonly)
Returns the value of attribute changes.
32 33 34 |
# File 'lib/state_mate/state_set.rb', line 32 def changes @changes end |
#new_values ⇒ Object (readonly)
Returns the value of attribute new_values.
32 33 34 |
# File 'lib/state_mate/state_set.rb', line 32 def new_values @new_values end |
#read_values ⇒ Object (readonly)
Returns the value of attribute read_values.
32 33 34 |
# File 'lib/state_mate/state_set.rb', line 32 def read_values @read_values end |
#rollback_errors ⇒ Object (readonly)
Returns the value of attribute rollback_errors.
32 33 34 |
# File 'lib/state_mate/state_set.rb', line 32 def rollback_errors @rollback_errors end |
#spec ⇒ Object
Returns the value of attribute spec.
31 32 33 |
# File 'lib/state_mate/state_set.rb', line 31 def spec @spec end |
#states ⇒ Object (readonly)
Returns the value of attribute states.
32 33 34 |
# File 'lib/state_mate/state_set.rb', line 32 def states @states end |
#states_to_change ⇒ Object (readonly)
Returns the value of attribute states_to_change.
32 33 34 |
# File 'lib/state_mate/state_set.rb', line 32 def states_to_change @states_to_change end |
#write_error ⇒ Object (readonly)
Returns the value of attribute write_error.
32 33 34 |
# File 'lib/state_mate/state_set.rb', line 32 def write_error @write_error end |
#written_states ⇒ Object (readonly)
Returns the value of attribute written_states.
32 33 34 |
# File 'lib/state_mate/state_set.rb', line 32 def written_states @written_states end |
Class Method Details
.from_spec(spec) ⇒ Object
48 49 50 51 52 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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/state_mate/state_set.rb', line 48 def self.from_spec spec state_set = self.new state_set.spec = spec unless spec.is_a? Hash raise StateMate::Error::TypeError.new spec, "spec must be a Hash of adapter names to states" end spec.each do |adapter_name, states| adapter = StateMate::Adapters.get adapter_name states = case states when Hash [states] when Array states else raise StateMate::Error::TypeError.new states, <<-BLOCK.unblock each value of the spec needs to be a single state hash or an array or state BLOCK end states.each do |state| unless spec.is_a? Hash raise StateMate::Error::TypeError.new state, "each state needs to be a Hash" end key = nil directives = [] type_name = nil unset_when_false = false # the :unset_when option can be provided to change the directive to # :unset when the option's value is true. # # this is useful for things that should simply unset the key when # turned off instead of setting it to false or something. # unset_when = nil = state['options'] || {} unless .is_a? Hash raise TypeError.new binding.erb <<-END options must be a hash, found <%= options.class %>: <%= options.inspect %> state: <%= state.inspect %> END end state.each do |k, v| # normalize to symbols k = k.to_sym if k.is_a? String if k == :key key = v elsif k == :options # pass, dealt with above elsif StateMate::DIRECTIVES.include? k directives << [k, v] elsif k == :type type_name = v elsif k == :unset_when_false unset_when_false = v elsif k == :unset_when unset_when = StateMate.cast 'bool', v else # any other keys are set as options # this is a little convenience feature that avoids having to # nest inside an `options` key unless your option conflicts # with 'key' or a directive. # # check for conflicts if .key? k raise ArgumentError.new binding.erb <<-END top-level state key #{ k.inspect } was also provided in the options. state: <%= state.inspect %> END end [k] = v end end directive, value = case directives.length when 0 raise "no directive found in #{ state.inspect }" when 1 directives.first else raise "multiple directives found in #{ state.inspect }" end # handle :unset_when_false option, which changes the operation to # an unset when the *directive value* is explicitly false if unset_when_false && (value === false || ['False', 'false'].include?(value)) directive = :unset value = nil # handle :unset_when, which also changes the operation to :unset # when the option's value is true. elsif unset_when directive = :unset value = nil elsif type_name value = StateMate.cast type_name, value end state_set.add adapter, key, directive, value, end # state.each end # states.each state_set end |
Instance Method Details
#add(adapter, key, directive, value, options = {}) ⇒ Object
192 193 194 |
# File 'lib/state_mate/state_set.rb', line 192 def add adapter, key, directive, value, = {} @states << State.new(adapter, key, directive, value, ) end |
#execute ⇒ Object
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 |
# File 'lib/state_mate/state_set.rb', line 196 def execute # find out what needs to be changed @states.each do |state| # read the current value read_value = state.adapter.read state.key, state. # store it for use in the actual change @read_values[state] = read_value # the test method is the directive with a '?' appended, # like `set?` or `array_contains?` test_method = StateMate.method "#{ state.directive }?" # find out if the state is in sync in_sync = test_method.call state.key, read_value, state.value, state.adapter, state. # add to the list of changes to be made for states that are # out of sync @states_to_change << state unless in_sync end # if everything is in sync, no changes need to be attempted # reutrn the empty hash of changes return @changes if @states_to_change.empty? # do the change to each in-memory value # this will raise an excption if the operation can't be done for # some reason states_to_change.each do |state| sync_method = StateMate.method state.directive # we want to catch any error and report it begin new_value = sync_method.call state.key, @read_values[state], state.value, state. rescue Exception => e @new_value_error = e raise StateMate::Error::ValueSyncError.new binding.erb <<-BLOCK an error occured when changing a values: <%= @new_value_error.format %> no changes were attempted to the system, so there is no rollback necessary. BLOCK end # change successful, store the new value along-side the state # for use in the next block @new_values << [state, new_value] end new_values.each do |state, new_value| begin state.adapter.write state.key, new_value, state. rescue Exception => e @write_error = e rollback raise StateMate::Error::WriteError.new binding.erb <<-BLOCK an error occured when writing new state values: <%= @write_error.format %> <% if @written_states.empty? %> the error occured on the first write, so no values were rolled back. <% else %> <% if @rollback_errors.empty? %> all values were sucessfully rolled back: <% else %> some values failed to rollback: <% end %> <% @written_states.each do |state| %> <% if @rollback_errors[state] %> <% state.key %>: <% @rollback_errors[state].format.indent(8) %> <% else %> <%= state.key %>: rolled back. <% end %> <% end %> <% end %> BLOCK else @written_states << state end # begin / rescue / else end # new_values.each # ok, we made it. report the changes new_values_hash = Hash[@new_values] @written_states.each do |state| @changes[[state.adapter.name, state.key]] = [@read_values[state], new_values_hash[state]] end @changes end |