easy_state 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 066f5b39b27137a950089f9acc7cb880c7f87725
4
+ data.tar.gz: 4d1e39f608673dbe0573a273753629d65b54859e
5
+ SHA512:
6
+ metadata.gz: 83759bd21983893b8dce111bf836efeab031b80639064394aec85c43da21b369df0c0ef21c8ecd31e66538e79f7154551c2929058ff652b5fad0c04b6bbca33a
7
+ data.tar.gz: 0aa6a0d9b89c66dfecfc7061d22e675cc7574f713b7762a3d5dbeac2d1d2c07ae4041687f8d4bdbe52e0ed618a81eced578019ef7effb1fc3a9b4a6a2bd268b6
data/LICENSE ADDED
@@ -0,0 +1,201 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "[]"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright [yyyy] [name of copyright owner]
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
@@ -0,0 +1,26 @@
1
+ #
2
+ # Author:: Alex Munoz (<amunoz951@gmail.com>)
3
+ # Copyright:: Copyright (c) 2020 Alex Munoz
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+
18
+ require 'digest'
19
+ require 'easy_io'
20
+ require 'hashly'
21
+ require 'json'
22
+ require 'tmpdir'
23
+ require 'fileutils'
24
+
25
+ require_relative 'easy_state/config'
26
+ require_relative 'easy_state/state'
@@ -0,0 +1,15 @@
1
+ module EasyState
2
+ module_function
3
+
4
+ def config
5
+ @config ||= EasyJSON.config(defaults: defaults)
6
+ end
7
+
8
+ def defaults
9
+ {
10
+ 'paths' => {
11
+ 'cache' => Dir.tmpdir,
12
+ },
13
+ }
14
+ end
15
+ end
@@ -0,0 +1,242 @@
1
+ module EasyState
2
+ @cache_path = "#{EasyState.config['paths']['cache']}/easy_state"
3
+ attr_writer :states_location # Allows overriding of the filesystem states folder where state files are saved.
4
+
5
+ module_function
6
+
7
+ # Description:
8
+ # Provides the location where filesystem states are saved.
9
+ # Returns:
10
+ # A string containing the filesystem path to the states folder where state files are saved.
11
+ def states_location
12
+ @states_location ||= "#{@cache_path}/state_checksums"
13
+ end
14
+
15
+ # Description:
16
+ # Gets the state of an object. It takes a SHA256 checksum of the content of the object's to_s.
17
+ # For hashes, it traverses the entire hash recursively taking checksums of every non-hash value.
18
+ # Parameters:
19
+ # obj: An object to be converted into a state. It can be any type of object, but the to_s method must represent its state.
20
+ # sort_if_sortable: (optional) Sorts sortable objects passed such as Arrays before taking a checksum of the object for comparison.
21
+ # default: true
22
+ def object_state(obj, sort_if_sortable: true)
23
+ case obj
24
+ when ::Hash
25
+ processed_hash = sort_if_sortable ? Hashly.deep_sort(obj) : obj
26
+ hash_state(processed_hash)
27
+ else
28
+ processed_obj = sort_if_sortable && obj.respond_to?(:sort) ? obj.sort : obj
29
+ Digest::SHA256.hexdigest(processed_obj.to_s)
30
+ end
31
+ end
32
+
33
+ # Description:
34
+ # Read the state of a directory.
35
+ # Returns:
36
+ # A hash with keys being the relative path from the directory and values being the checksum of each path.
37
+ # Parameters:
38
+ # path: The path to the directory
39
+ # filter: (optional) A wildcard filter for searching for specific files.
40
+ # default: '**/*'
41
+ def directory_state(path, filter: nil)
42
+ filter ||= '**/*'
43
+ path = path.tr('\\', '/')
44
+ Dir.glob("#{path}/#{filter}").each_with_object({}) do |entry_path, hash|
45
+ relative_path = entry_path.sub(path, '')
46
+ next hash[relative_path] = 'directory' if ::File.directory?(entry_path)
47
+ hash[relative_path] = Digest::SHA256.file(entry_path).hexdigest
48
+ end
49
+ end
50
+
51
+ # Description:
52
+ # Takes checksums of every leaf in a hash so that no sensitive values are present.
53
+ # Returns:
54
+ # A hash containing all the same keys, but the values that are not a hash are converted to checksums recursively.
55
+ # Parameters:
56
+ # hash: A ruby hash to be converted into a state hash
57
+ # sort_if_sortable: (optional) Sort objects that are sortable (like an Array) before taking their checksum.
58
+ # default: true
59
+ def hash_state(hash, sort_if_sortable: true)
60
+ return hash if hash.nil? || hash.empty?
61
+ Hashly.stringify_all_keys(hash).each_with_object({}) do |(k, v), checksum_hash|
62
+ next checksum_hash[k] = hash_state(v) if v.is_a?(::Hash)
63
+ value = sort_if_sortable && v.respond_to?(:sort) ? v.sort : v
64
+ checksum_hash[k] = Digest::SHA256.hexdigest(value.to_s)
65
+ end
66
+ end
67
+
68
+ # Description:
69
+ # Saves the state of the object at the state_path specified using filesystem storage.
70
+ # Parameters:
71
+ # state_path: A path separated by forward slashes specifying where to store the state, much like Hashicorp Vault paths.
72
+ # It is NOT a filesystem path - EG: 'myproject/environment/sql_credentials'
73
+ # obj: An object containing the value of the desired state (not a checksum). It can be any type of object.
74
+ # states_folder: (optional) The location where the state files are saved if previous_state_hash is nil.
75
+ # default: The setting specified in the config which defaults to a subfolder under the user's temp directory.
76
+ # sort_if_sortable: (optional) Sorts sortable objects passed such as Arrays before taking a checksum of the object for comparison.
77
+ # default: true
78
+ def save_object_state(state_path, obj, states_folder: nil, sort_if_sortable: true)
79
+ obj_checksum = obj_checksum(obj, sort_if_sortable: sort_if_sortable)
80
+ save_state(state_path, obj_checksum, states_folder: states_folder)
81
+ end
82
+
83
+ # Description:
84
+ # Save the state of the directory structure at the path specified
85
+ # Parameters:
86
+ # path: A filesystem path for which the state is to be saved. This will take checksums of all files under this path recursively.
87
+ # filter: (optional) A glob-style filter can be provided in order to use wildcards. EG: (**/myfile*.config)
88
+ # default: '**/*' (all files and subfolders)
89
+ def save_directory_state(path, filter: nil)
90
+ checksum_hash = directory_state(path, filter: filter)
91
+ checksum_hash['directory_state'] = obj_checksum(checksum_hash)
92
+ save_object_state(path.tr(':', '_drive'), checksum_hash)
93
+ end
94
+
95
+ # Description:
96
+ # Saves the state provided at the state_path specified, updating the appropriate state file in the filesystem.
97
+ # Parameters:
98
+ # state_path: A path separated by forward slashes specifying where to store the state, much like Hashicorp Vault paths.
99
+ # It is NOT a filesystem path - EG: 'myproject/environment/sql_credentials'
100
+ # checksum_data: a SHA256 checksum or hash of checksums to save at the path specified.
101
+ # states_folder: (optional) The location where the state files are saved.
102
+ # default: The setting specified in the config which defaults to a subfolder under the user's temp directory.
103
+ def save_state(state_path, checksum_data, states_folder: nil, merge_with_existing_states: true, silent: false)
104
+ raise 'Failed to save state! state_path is a required argument!' if state_path.nil? || state_path.empty?
105
+ state_keys = state_path.split('/')
106
+ states_folder ||= states_location
107
+ state_hash = {}
108
+ current_path_hash = state_hash
109
+ if state_keys.count == 1
110
+ raise "When saving the entire state of a file, the checksum_data must be in the form of a hash! Type given: #{checksum_data.class}" unless checksum_data.is_a?(::Hash)
111
+ state_hash = checksum_data
112
+ else
113
+ state_keys.drop(1).each do |state_key|
114
+ if state_key == state_keys.last
115
+ current_path_hash[state_key] = checksum_data # on the leaf (last key), save the object's checksum to it
116
+ break
117
+ end
118
+ current_path_hash[state_key] = {}
119
+ current_path_hash = current_path_hash[state_key] # move to the next level in the state_path
120
+ end
121
+ end
122
+ FileUtils.mkdir_p(states_folder) unless ::File.directory?(states_folder)
123
+ EasyIO::Disk.open_file_and_wait_for_exclusive_lock("#{states_folder}/#{state_keys.first}.state") do |file|
124
+ file_content = file.read
125
+ previous_state_hash = begin
126
+ file_content.strip.empty? ? {} : JSON.parse(file_content)
127
+ rescue JSON::ParserError
128
+ {}
129
+ end
130
+
131
+ new_state_content = merge_with_existing_states ? Hashly.deep_merge(previous_state_hash, state_hash) : state_hash
132
+ file.truncate(0)
133
+ file.rewind
134
+ file.write(JSON.pretty_generate(new_state_content))
135
+ EasyIO.logger.info 'State saved to file.' unless silent
136
+ end
137
+ end
138
+
139
+ # Description:
140
+ # Reads the state at the state_path specified.
141
+ # Returns:
142
+ # The checksum or hash of checksums at the state_path specified.
143
+ # Parameters:
144
+ # state_path: A path separated by forward slashes specifying where to store the state, much like Hashicorp Vault paths.
145
+ # It is NOT a filesystem path - EG: 'myproject/environment/sql_credentials'
146
+ # previous_state_hash: (optional) A ruby hash containing all the checksums of the previous states.
147
+ # This should be the full hash at the root level (the value of the root key), not the actual previous state of the specified state_path.
148
+ # default: If no hash is specified, it will assume filesystem storage is being used and will look for the file and read it if it exists.
149
+ # states_folder: (optional) The location where the state files are saved if previous_state_hash is nil.
150
+ # default: The setting specified in the config which defaults to a subfolder under the user's temp directory.
151
+ def read_state(state_path, previous_state_hash: nil, states_folder: nil, silent: false)
152
+ raise 'Failed to save state! state_path is a required argument!' if state_path.nil? || state_path.empty?
153
+ state_keys = state_path.split('/')
154
+ states_folder ||= states_location
155
+ previous_state_hash ||= read_state_file(state_keys.first, states_folder: states_folder, silent: silent)
156
+ return previous_state_hash if state_keys.count == 1 # The previous_state_hash is the entire content of the root level state
157
+ current_path_hash = previous_state_hash
158
+ state_keys.drop(1).each do |state_key|
159
+ return nil if current_path_hash[state_key].nil?
160
+ return current_path_hash[state_key] if state_key == state_keys.last # on the leaf (last key), return the checksum
161
+ current_path_hash = current_path_hash[state_key] # move up one level
162
+ end
163
+ end
164
+
165
+ # Description:
166
+ # Reads a state file from the filesystem.
167
+ # Returns:
168
+ # A hash containing the last known states for the state_path_root specified.
169
+ # Parameters:
170
+ # state_path_root: The first segment of a state_path (first entry before the first slash).
171
+ # states_folder: (optional) The location where the state files are saved if previous_state_hash is nil.
172
+ # default: The setting specified in the config which defaults to a subfolder under the user's temp directory.
173
+ def read_state_file(state_path_root, states_folder: nil, silent: false)
174
+ states_folder ||= states_location
175
+ state_hash = {}
176
+ return state_hash unless ::File.exist?("#{states_folder}/#{state_path_root}.state")
177
+ EasyIO.logger.info 'Reading state file...' unless silent
178
+ EasyIO::Disk.open_file_and_wait_for_exclusive_lock("#{states_folder}/#{state_path_root}.state") do |file|
179
+ content = file.read
180
+ state_hash = JSON.parse(content)
181
+ end
182
+ state_hash
183
+ rescue JSON::ParserError
184
+ EasyIO.logger.warn "#{states_folder}/#{state_path_root}.state did not contain valid json! Resetting state!"
185
+ {}
186
+ end
187
+
188
+ # Description:
189
+ # Checks to see if the entry (obj) at the path specified matches what was last saved at that path in the previous state hash.
190
+ # Returns:
191
+ # true or false
192
+ # Parameters:
193
+ # state_path: A path separated by forward slashes specifying where to store the state, much like Hashicorp Vault paths.
194
+ # It is NOT a filesystem path - EG: 'myproject/environment/sql_credentials'
195
+ # obj: An object containing the value of the desired state (not a checksum). It can be any type of object.
196
+ # previous_state_hash: (optional) A ruby hash containing all the checksums of the previous states.
197
+ # This should be the full hash at the root level, not the actual previous state of the specified state_path.
198
+ # default: If no hash is specified, it will assume filesystem storage is being used and will look for the file and read it.
199
+ # states_folder: (optional) The location where the state files are saved if previous_state_hash is nil.
200
+ # default: The setting specified in the config which defaults to a subfolder under the user's temp directory.
201
+ # sort_if_sortable: (optional) Sorts sortable objects passed such as Arrays before taking a checksum of the object for comparison.
202
+ # default: true
203
+ def object_state_changed?(state_path, obj, previous_state_hash: nil, states_folder: nil, sort_if_sortable: true)
204
+ current_obj_state = object_state(obj, sort_if_sortable: sort_if_sortable)
205
+ state_changed?(state_path, current_obj_state, previous_state_hash: previous_state_hash, states_folder: states_folder)
206
+ end
207
+
208
+ # Description:
209
+ # Checks to see if the state at the path specified matches what was last saved at that path in the previous state hash.
210
+ # Returns:
211
+ # true or false
212
+ # Parameters:
213
+ # state_path: A path separated by forward slashes specifying where to store the state, much like Hashicorp Vault paths.
214
+ # It is NOT a filesystem path - EG: 'myproject/environment/sql_credentials'
215
+ # current_obj_state: An object containing the checksum or hash of checksums of the current state.
216
+ # previous_state_hash: (optional) A ruby hash containing all the checksums of the previous states.
217
+ # This should be the full hash at the root level, not the actual previous state of the specified state_path.
218
+ # default: If no hash is specified, it will assume filesystem storage is being used and will look for the file and read it.
219
+ # states_folder: (optional) The location where the state files are saved if previous_state_hash is nil.
220
+ # default: The setting specified in the config which defaults to a subfolder under the user's temp directory.
221
+ def state_changed?(state_path, current_obj_state, previous_state_hash: nil, states_folder: nil)
222
+ previous_state = read_state(state_path, previous_state_hash: previous_state_hash, states_folder: states_folder, silent: true)
223
+ previous_state != current_obj_state
224
+ end
225
+
226
+ # Description:
227
+ # Checks to see if any values have changed or are missing for any of the keys in the state hashes.
228
+ # Returns:
229
+ # A hash containing the changed keys and their checksums.
230
+ # Parameters:
231
+ # previous_state_hash: A ruby hash containing all the checksums of the previous states.
232
+ # This should be the full hash at the root level, not the actual previous state of the specified state_path.
233
+ # default: If no hash is specified, it will assume filesystem storage is being used and will look for the file and read it.
234
+ # new_state_hash: A ruby hash containing all the checksums of the previous states.
235
+ # This should be the full hash at the root level, not the actual previous state of the specified state_path.
236
+ # default: If no hash is specified, it will assume filesystem storage is being used and will look for the file and read it.
237
+ def changed_keys(previous_state_hash, new_state_hash)
238
+ left_diff = Hashly.deep_diff(previous_state_hash, new_state_hash)
239
+ right_diff = Hashly.deep_diff(new_state_hash, previous_state_hash)
240
+ Hashly.deep_merge(left_diff, right_diff)
241
+ end
242
+ end
metadata ADDED
@@ -0,0 +1,104 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: easy_state
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Alex Munoz
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: hashly
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: easy_io
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: json
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3'
69
+ description:
70
+ email:
71
+ - amunoz951@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - LICENSE
77
+ - lib/easy_state.rb
78
+ - lib/easy_state/config.rb
79
+ - lib/easy_state/state.rb
80
+ homepage: https://github.com/amunoz951/easy_state
81
+ licenses:
82
+ - Apache-2.0
83
+ metadata: {}
84
+ post_install_message:
85
+ rdoc_options: []
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ requirements:
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: '2.3'
93
+ required_rubygems_version: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - ">="
96
+ - !ruby/object:Gem::Version
97
+ version: '0'
98
+ requirements: []
99
+ rubyforge_project:
100
+ rubygems_version: 2.5.2.3
101
+ signing_key:
102
+ specification_version: 4
103
+ summary: Ruby library for easily extracting and creating 7zip and zip archives idempotently.
104
+ test_files: []