ssm_config 1.1.0 → 1.2.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a7509632dfae2aa08f269331ae52ec1e645da5c3777a772721866147f4e25c46
4
- data.tar.gz: 146883e2ee8cde2946de40d3ee49ba2ab1dc0f9ad529425a2970bf6bb0baf8ec
3
+ metadata.gz: fc2240e68acd1bb76a701e1101dfb2a457e33fdbad0e444a134284746e9fd2fa
4
+ data.tar.gz: a993b5e09f224a107e44dc5a612bf49fcd6455e604d9a1613d6485f589a6271e
5
5
  SHA512:
6
- metadata.gz: c18bf507eda6a9f668325376d7ac0a28d0ceee717ef73909019493cb8b91aac3fc0c51e4eae8b60077674565b25fbd0505bab97f32588a5a6ed456bd8da395a9
7
- data.tar.gz: 91bd9725e9ca01cc512c434db801998887fe248ed8720eeaab7e4cafde50a1d3cb2d8873700e2f5411c2e2c1f3a76457a74db801f05f22824451bc4ec7b056ef
6
+ metadata.gz: 1fe7b998ecc23dffdba9c74ecd7c8ac9b3d54f02e8a7efec6d8692e2ef9dbab56b904d320fce5b284c2fa2fb8f5a95a5be81d1fdab99367390bac22500b428d7
7
+ data.tar.gz: 952104e797a51ad680b80f0f4f5745f5f946583cbca450041d1fbfd58188c9d0bc9bc8f188795ebf068ac62b8549d7f34d621eb27a8a5191b399a139bd5c6e18
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 contain the character that corresponds to the first character of the datatype (so one of `[s, i, b, f]`). This field is not case-sensitive (the gem also only checks the first character of `datatype`). 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 (given the datatype is a boolean) will be checked.
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
- An invalid entry will throw an exception (as well as an invalid boolean entry).
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 | 4|
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"=>4}}
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,45 @@ any:
72
75
  default: 3
73
76
  company1:
74
77
  - 2
75
- company2: 4
78
+ company2: true
76
79
  ```
77
80
  ```ruby
78
81
  SsmConfig.eft
79
- => {"days_to_enter_bank_account"=>{"default"=>3, "company1"=>[2], "company2"=>4}}
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. The following is a custom validation for datatype (which can be added in the corresponding model file):
91
+
92
+ ```ruby
93
+ class SsmConfigRecord < ApplicationRecord
94
+ validate :datatype_support
95
+
96
+ def datatype_support
97
+ errors.add(:datatype, "is not a valid datatype (#{datatype})") unless SsmConfig::SsmStorage::Db::VALID_DATATYPES.include? datatype.downcase[0]
98
+ end
99
+ end
100
+ ```
101
+
102
+
103
+ The `down` method will remove _all_ rows in the table that match the file name. A sample migration is as follows:
104
+
105
+ ```ruby
106
+ class AddFileToSsmconfigrecord < ActiveRecord::Migration[5.2]
107
+ def up
108
+ SsmConfig::MigrationHelper.new('file').up
109
+ end
110
+
111
+ def down
112
+ SsmConfig::MigrationHelper.new('file').down
113
+ end
114
+ end
115
+ ```
116
+
83
117
  ## Development
84
118
 
85
119
  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
@@ -3,6 +3,7 @@ module SsmConfig
3
3
  class Db
4
4
  TABLE_NAME = 'ssm_config_records'.freeze
5
5
  ACTIVE_RECORD_MODEL = 'SsmConfigRecord'.freeze
6
+ VALID_DATATYPES = ['s', 'i', 'b', 'f'].freeze
6
7
  def initialize(file_name)
7
8
  @file_name = file_name
8
9
  end
@@ -40,8 +41,7 @@ module SsmConfig
40
41
  end
41
42
 
42
43
  def transform_class(value, type)
43
- possible_types = ['s', 'i', 'b', 'f']
44
- raise SsmConfig::UnsupportedDatatype, 'Not a valid class: must be one of string, integer, boolean, or float' unless possible_types.include? type.to_s.downcase[0]
44
+ raise SsmConfig::UnsupportedDatatype, 'Not a valid class: must be one of string, integer, boolean, or float' unless VALID_DATATYPES.include? type.to_s.downcase[0]
45
45
  return value.send("to_#{type.to_s.downcase[0]}") unless type[0] == 'b'
46
46
  convert_boolean(value)
47
47
  end
data/lib/ssm_config.rb CHANGED
@@ -1,12 +1,13 @@
1
- require './lib/ssm_config/ssm_storage/db.rb'
2
- require './lib/ssm_config/ssm_storage/yml.rb'
3
- require './lib/ssm_config/ssm_storage/empty.rb'
1
+ require 'ssm_config/ssm_storage/db.rb'
2
+ require 'ssm_config/ssm_storage/yml.rb'
3
+ require 'ssm_config/ssm_storage/empty.rb'
4
+ require 'ssm_config/errors.rb'
5
+ require '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.1.0'.freeze
10
+ VERSION = '1.2.1'.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.1.0
4
+ version: 1.2.1
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-13 00:00:00.000000000 Z
11
+ date: 2023-07-18 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