ssm_config 1.1.0 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +35 -12
- data/lib/ssm_config/migration_helper.rb +61 -0
- data/lib/ssm_config.rb +3 -2
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 89d121720c2b6cfaf01bc67c184f32be74f0eaa3ad217a667d760047680b6491
|
4
|
+
data.tar.gz: bc4eda9aa3c1b28475e3aee34fa6b7453a3992d27fc46eab06e6ce8f69314849
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 05afa978a2d8c57db17e1dbf66d9cf443096a2de2b6caed961d69dffafefd6fdda0434eba7d7046ec95346f9bfffd240f794359045e9890153c17994411815f6
|
7
|
+
data.tar.gz: df7370fa1b13356b249f5726b0dd203c246323d41b39d73711f0284e3f6151269e30f0261c24eac1c5945f6122480a3ca6a968a2c5e538793a9f6ad4d3f4857e
|
data/README.md
CHANGED
@@ -31,9 +31,11 @@ To utilize ActiveRecords, create the following model:
|
|
31
31
|
rails generate model SsmConfigRecord file:string:index accessor_keys:string value:string datatype:string
|
32
32
|
```
|
33
33
|
|
34
|
-
The supported datatypes are `[string, integer, boolean, float]`. The field `datatype` should
|
34
|
+
The supported datatypes are `[string, integer, boolean, float]`. The first character entered in the field `datatype` should be the character that corresponds to the first character of the datatype (so one of `[s, i, b, f]`). This field is not case-sensitive.
|
35
35
|
|
36
|
-
|
36
|
+
Booleans should also be one of `[t, f]`, corresponding to `true` and `false`. Similarly, this is not case-sensitive and only the first character of the value entered (given the datatype is a boolean) will be checked.
|
37
|
+
|
38
|
+
An invalid `datatype` or boolean entry will throw an exception.
|
37
39
|
|
38
40
|
When migrating a file to the ActiveRecord, it is important to correctly input the accessor keys. The field `accessor_keys` represents a hashkey corresponding to a value in the hash: for the sequence of keys used to access a value, the corresponding accessor key will be the keys concatentated with a comma delimiter. For example, if `hash[:key1][:key2][:key3] = value`, the corresponding accessor key would be the string `"key1,key2,key3"`. In the case that there is an array, we include the index embraced by brackets. Consider the following hash:
|
39
41
|
|
@@ -51,19 +53,20 @@ The accessor keys for `value1`, `value2`, and `value3` would be `"build,docker,[
|
|
51
53
|
⚠️ **NOTE:** ⚠️ There should be no file name called `cache`, as this will call a method of the `SsmConfig` class. Also, all values read from the table will be strings.
|
52
54
|
## Usage
|
53
55
|
|
54
|
-
|
55
56
|
Given the following rows in `SsmConfigRecord`:
|
56
57
|
|
57
|
-
| file | accessor_keys | value |
|
58
|
-
| :---: | :------------: | :---: |
|
59
|
-
| eft | days_to_enter_bank_account,default | 3 |
|
60
|
-
| eft | days_to_enter_bank_account,company1,[0] | 2 |
|
61
|
-
| eft | days_to_enter_bank_account,company2 |
|
58
|
+
| file | accessor_keys | value | datatype |
|
59
|
+
| :---: | :------------: | :---: | :---: |
|
60
|
+
| eft | days_to_enter_bank_account,default | 3 | 'i' |
|
61
|
+
| eft | days_to_enter_bank_account,company1,[0] | 2 | 'Integer'
|
62
|
+
| eft | days_to_enter_bank_account,company2 | true| 'boolean'
|
62
63
|
|
63
64
|
```ruby
|
64
65
|
SsmConfig.eft
|
65
|
-
=> {"days_to_enter_bank_account"=>{"default"=>3, "company1"=>[2], "company2"=>
|
66
|
+
=> {"days_to_enter_bank_account"=>{"default"=>3, "company1"=>[2], "company2"=>true}}
|
66
67
|
```
|
68
|
+
To reiterate, only the first character of the datatype is processed, and it is not case sensitive.
|
69
|
+
|
67
70
|
`SsmConfig` will always reconstruct the hash using all the rows with the corresponding file name. In the case that no such row exists, `SsmConfig` will look for `config/foo.yml`. For example, given `config/eft.yml`,
|
68
71
|
|
69
72
|
```yml
|
@@ -72,14 +75,34 @@ any:
|
|
72
75
|
default: 3
|
73
76
|
company1:
|
74
77
|
- 2
|
75
|
-
company2:
|
78
|
+
company2: true
|
76
79
|
```
|
77
80
|
```ruby
|
78
81
|
SsmConfig.eft
|
79
|
-
=> {"days_to_enter_bank_account"=>{"default"=>3, "company1"=>[2], "company2"=>
|
82
|
+
=> {"days_to_enter_bank_account"=>{"default"=>3, "company1"=>[2], "company2"=>true}}
|
80
83
|
```
|
81
|
-
|
82
84
|
This search will be exclusive: i.e., if any row exists in the table then the gem will not look in `config`.
|
85
|
+
|
86
|
+
## Migrations
|
87
|
+
|
88
|
+
To migrate a YAML file in the `config` directory into `SsmConfigRecord`, the class `SsmConfig::MigrationHelper` can be used. `MigrationHelper` takes in the file name, and has `up` and `down` methods.
|
89
|
+
|
90
|
+
The `up` method will migrate the file into the table: if any validations are violated, then all rows that were added in the current call will be deleted, returning the table to the initial state.
|
91
|
+
|
92
|
+
The `down` method will remove _all_ rows in the table that match the file name. A sample migration is as follows:
|
93
|
+
|
94
|
+
```ruby
|
95
|
+
class AddFileToSsmconfigrecord < ActiveRecord::Migration[5.2]
|
96
|
+
def up
|
97
|
+
SsmConfig::MigrationHelper.new('file').up
|
98
|
+
end
|
99
|
+
|
100
|
+
def down
|
101
|
+
SsmConfig::MigrationHelper.new('file').down
|
102
|
+
end
|
103
|
+
end
|
104
|
+
```
|
105
|
+
|
83
106
|
## Development
|
84
107
|
|
85
108
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
@@ -0,0 +1,61 @@
|
|
1
|
+
module SsmConfig
|
2
|
+
class MigrationHelper
|
3
|
+
def initialize(file_name)
|
4
|
+
@file_name = file_name
|
5
|
+
@model_name = SsmConfig::SsmStorage::Db::ACTIVE_RECORD_MODEL
|
6
|
+
end
|
7
|
+
|
8
|
+
def up
|
9
|
+
added = []
|
10
|
+
keys_hash = accessor_key_hash(hash) # starting layer is always hash
|
11
|
+
last = nil
|
12
|
+
keys_hash.each do |accessor_key, value|
|
13
|
+
last = accessor_key
|
14
|
+
added.push(@model_name.constantize.create!(:file => @file_name, :accessor_keys => accessor_key, :value => value.to_s, :datatype => determine_class(value)))
|
15
|
+
end
|
16
|
+
rescue ActiveRecord::RecordInvalid => e
|
17
|
+
Rails.logger.error("#{e.message} was raised because of faulty data with accessor_key #{last}")
|
18
|
+
added.each(&:delete)
|
19
|
+
end
|
20
|
+
|
21
|
+
def down
|
22
|
+
@model_name.constantize.where(:file => @file_name).destroy_all
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def accessor_key_recurse(value, curr, res)
|
28
|
+
case value
|
29
|
+
when Hash
|
30
|
+
res.merge!(accessor_key_hash(value, curr))
|
31
|
+
when Array
|
32
|
+
res.merge!(accessor_key_array(value, curr))
|
33
|
+
else
|
34
|
+
res[curr[1..-1]] = value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
def accessor_key_hash(hash, curr = '')
|
39
|
+
hash.each_with_object({}) do |(key, value), res|
|
40
|
+
updated_hash = "#{curr},#{key}"
|
41
|
+
accessor_key_recurse(value, updated_hash, res)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def accessor_key_array(arr, curr = '')
|
46
|
+
arr.each_with_object({}).with_index do |(value, res), index|
|
47
|
+
updated_hash = "#{curr},[#{index}]"
|
48
|
+
accessor_key_recurse(value, updated_hash, res)
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def determine_class(value)
|
53
|
+
return 'boolean' if (value == false) || (value == true)
|
54
|
+
return value.class
|
55
|
+
end
|
56
|
+
|
57
|
+
def hash
|
58
|
+
SsmConfig::SsmStorage::Yml.new(@file_name).hash
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/lib/ssm_config.rb
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
require './lib/ssm_config/ssm_storage/db.rb'
|
2
2
|
require './lib/ssm_config/ssm_storage/yml.rb'
|
3
3
|
require './lib/ssm_config/ssm_storage/empty.rb'
|
4
|
+
require './lib/ssm_config/errors.rb'
|
5
|
+
require './lib/ssm_config/migration_helper.rb'
|
4
6
|
require 'active_support/core_ext/hash/indifferent_access'
|
5
7
|
require 'active_support/time'
|
6
|
-
require './lib/ssm_config/errors.rb'
|
7
8
|
|
8
9
|
module SsmConfig
|
9
|
-
VERSION = '1.
|
10
|
+
VERSION = '1.2.0'.freeze
|
10
11
|
REFRESH_TIME = (30.minutes).freeze
|
11
12
|
|
12
13
|
class << self
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ssm_config
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Santiago Herrera
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-07-
|
11
|
+
date: 2023-07-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -121,6 +121,7 @@ files:
|
|
121
121
|
- bin/setup
|
122
122
|
- lib/ssm_config.rb
|
123
123
|
- lib/ssm_config/errors.rb
|
124
|
+
- lib/ssm_config/migration_helper.rb
|
124
125
|
- lib/ssm_config/ssm_storage/db.rb
|
125
126
|
- lib/ssm_config/ssm_storage/empty.rb
|
126
127
|
- lib/ssm_config/ssm_storage/yml.rb
|