Class: StateMate::StateSet

Inherits:
Object
  • Object
show all
Defined in:
lib/state_mate.rb

Defined Under Namespace

Classes: State

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeStateSet

from_spec



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/state_mate.rb', line 169

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

#changesObject (readonly)

Returns the value of attribute changes.



24
25
26
# File 'lib/state_mate.rb', line 24

def changes
  @changes
end

#new_valuesObject (readonly)

Returns the value of attribute new_values.



24
25
26
# File 'lib/state_mate.rb', line 24

def new_values
  @new_values
end

#read_valuesObject (readonly)

Returns the value of attribute read_values.



24
25
26
# File 'lib/state_mate.rb', line 24

def read_values
  @read_values
end

#rollback_errorsObject (readonly)

Returns the value of attribute rollback_errors.



24
25
26
# File 'lib/state_mate.rb', line 24

def rollback_errors
  @rollback_errors
end

#specObject

Returns the value of attribute spec.



23
24
25
# File 'lib/state_mate.rb', line 23

def spec
  @spec
end

#statesObject (readonly)

Returns the value of attribute states.



24
25
26
# File 'lib/state_mate.rb', line 24

def states
  @states
end

#states_to_changeObject (readonly)

Returns the value of attribute states_to_change.



24
25
26
# File 'lib/state_mate.rb', line 24

def states_to_change
  @states_to_change
end

#write_errorObject (readonly)

Returns the value of attribute write_error.



24
25
26
# File 'lib/state_mate.rb', line 24

def write_error
  @write_error
end

#written_statesObject (readonly)

Returns the value of attribute written_states.



24
25
26
# File 'lib/state_mate.rb', line 24

def written_states
  @written_states
end

Class Method Details

.from_spec(spec) ⇒ Object



40
41
42
43
44
45
46
47
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
# File 'lib/state_mate.rb', line 40

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::Adapters.get adapter_name

    states = case states
    when Hash
      [states]
    when Array
      states
    else
      raise Error::TypeError.new states, "        each value of the spec needs to be a single state hash or an\n        array or state\n      BLOCK\n    end\n\n    states.each do |state|\n      unless spec.is_a? Hash\n        raise Error::TypeError.new state, \"each state needs to be a Hash\"\n      end\n\n      key = nil\n      directives = []\n      type_name = nil\n      unset_when_false = false\n      \n      # the :unset_when option can be provided to change the directive to\n      # :unset when the option's value is true.\n      # \n      # this is useful for things that should simply unset the key when\n      # turned off instead of setting it to false or something.\n      # \n      unset_when = nil\n      \n      options = state['options'] || {}\n      \n      unless options.is_a? Hash\n        raise TypeError.new binding.erb <<-END\n          options must be a hash, found <%= options.class %>:\n          \n          <%= options.inspect %>\n          \n          state:\n          \n          <%= state.inspect %>\n          \n        END\n      end\n      \n      state.each do |k, v|\n        # normalize to symbols\n        k = k.to_sym if k.is_a? String\n        \n        if k == :key\n          key = v\n        elsif k == :options\n          # pass, dealt with above\n        elsif DIRECTIVES.include? k\n          directives << [k, v]\n        elsif k == :type\n          type_name = v\n        elsif k == :unset_when_false\n          unset_when_false = v\n        elsif k == :unset_when\n          unset_when = StateMate.cast 'bool', v\n        else\n          # any other keys are set as options\n          # this is a little convience feature that avoids having to\n          # nest inside an `options` key unless your option conflicts\n          # with 'key' or a directive.\n          #\n          # check for conflicts\n          if options.key? k\n            raise ArgumentError.new binding.erb <<-END\n              top-level state key \#{ k.inspect } was also provided in the\n              options.\n              \n              state:\n              \n              <%= state.inspect %>\n              \n            END\n          end\n          \n          options[k] = v\n        end\n      end\n\n      directive, value = case directives.length\n      when 0\n        raise \"no directive found in \#{ state.inspect }\"\n      when 1\n        directives.first\n      else\n        raise \"multiple directives found in \#{ state.inspect }\"\n      end\n      \n      # handle :unset_when_false option, which changes the operation to\n      # an unset when the *directive value* is explicity false\n      if  unset_when_false && \n          (value === false || ['False', 'false'].include?(value))\n        directive = :unset\n        value = nil\n      \n      # handle :unset_when, which also changes the operation to :unset\n      # when the option's value is true.\n      elsif unset_when\n        directive = :unset\n        value = nil\n        \n      elsif type_name\n        value = StateMate.cast type_name, value\n      end\n\n      state_set.add adapter, key, directive, value, options\n    end # state.each\n  end # states.each\n\n  state_set\nend\n".unblock

Instance Method Details

#add(adapter, key, directive, value, options = {}) ⇒ Object



183
184
185
# File 'lib/state_mate.rb', line 183

def add adapter, key, directive, value, options = {}
  @states << State.new(adapter, key, directive, value, options)
end

#executeObject



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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
# File 'lib/state_mate.rb', line 187

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.options

    # 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.options

    # 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.options
    rescue Exception => e
      @new_value_error = e
      raise Error::ValueSyncError.new binding.erb "        an error occured when changing a values:\n\n        <%= @new_value_error.format %>\n\n        no changes were attempted to the system, so there is no rollback\n        necessary.\n      BLOCK\n    end\n    # change successful, store the new value along-side the state\n    # for use in the next block\n    @new_values << [state, new_value]\n  end\n\n  new_values.each do |state, new_value|\n    begin\n      state.adapter.write state.key, new_value, state.options\n    rescue Exception => e\n      @write_error = e\n      rollback\n      raise Error::WriteError.new binding.erb <<-BLOCK\n        an error occured when writing new state values:\n\n        <%= @write_error.format %>\n\n        <% if @written_states.empty? %>\n          the error occured on the first write, so no values were rolled\n          back.\n\n        <% else %>\n          <% if @rollback_errors.empty? %>\n            all values were sucessfully rolled back:\n\n          <% else %>\n            some values failed to rollback:\n\n          <% end %>\n\n          <% @written_states.each do |state| %>\n            <% if @rollback_errors[state] %>\n              <% state.key %>: <% @rollback_errors[state].format.indent(8) %>\n            <% else %> \n              <%= state.key %>: rolled back.\n            <% end %>\n          <% end %>\n        <% end %>\n        BLOCK\n    else\n      @written_states << state\n    end # begin / rescue / else\n  end # new_values.each\n\n  # ok, we made it. report the changes\n  new_values_hash = Hash[@new_values]\n  @written_states.each do |state|\n    @changes[[state.adapter.name, state.key]] = [@read_values[state], new_values_hash[state]]\n  end\n  \n  @changes\nend\n"