Class: StateMate::StateSet
- Inherits:
-
Object
- Object
- StateMate::StateSet
- Defined in:
- lib/state_mate.rb
Overview
Error
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
152 153 154 155 156 157 158 159 160 161 162 163 164 |
# File 'lib/state_mate.rb', line 152 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.
37 38 39 |
# File 'lib/state_mate.rb', line 37 def changes @changes end |
#new_values ⇒ Object (readonly)
Returns the value of attribute new_values.
37 38 39 |
# File 'lib/state_mate.rb', line 37 def new_values @new_values end |
#read_values ⇒ Object (readonly)
Returns the value of attribute read_values.
37 38 39 |
# File 'lib/state_mate.rb', line 37 def read_values @read_values end |
#rollback_errors ⇒ Object (readonly)
Returns the value of attribute rollback_errors.
37 38 39 |
# File 'lib/state_mate.rb', line 37 def rollback_errors @rollback_errors end |
#spec ⇒ Object
Returns the value of attribute spec.
36 37 38 |
# File 'lib/state_mate.rb', line 36 def spec @spec end |
#states ⇒ Object (readonly)
Returns the value of attribute states.
37 38 39 |
# File 'lib/state_mate.rb', line 37 def states @states end |
#states_to_change ⇒ Object (readonly)
Returns the value of attribute states_to_change.
37 38 39 |
# File 'lib/state_mate.rb', line 37 def states_to_change @states_to_change end |
#write_error ⇒ Object (readonly)
Returns the value of attribute write_error.
37 38 39 |
# File 'lib/state_mate.rb', line 37 def write_error @write_error end |
#written_states ⇒ Object (readonly)
Returns the value of attribute written_states.
37 38 39 |
# File 'lib/state_mate.rb', line 37 def written_states @written_states end |
Class Method Details
.from_spec(spec) ⇒ 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 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 |
# File 'lib/state_mate.rb', line 53 def self.from_spec spec state_set = self.new state_set.spec = spec unless spec.is_a? Hash raise Error::TypeError.new spec, "spec must be a Hash of adapter names to states" end spec.each do |adapter_name, states| adapter = StateMate.get_adapter adapter_name states = case states when Hash [states] when Array states else raise 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 Error::TypeError.new state, "each state needs to be a Hash" end key = nil directives = [] type_name = 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| if k == 'key' key = v elsif k == 'options' # pass, dealt with above elsif DIRECTIVES.include? k directives << [k, v] elsif k == 'type' type_name = v else # any other keys are set as options # this is a little convience 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 unless type_name.nil? 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
166 167 168 |
# File 'lib/state_mate.rb', line 166 def add adapter, key, directive, value, = {} @states << State.new(adapter, key, directive, value, ) end |
#execute ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 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 |
# File 'lib/state_mate.rb', line 170 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 # 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 Error::ValueChangeError.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 neessicary. 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 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 |