enum_kit 0.2.1 → 0.2.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6f5949293c0b433b8a014dd718bdb0db6f65b13293bc3c781d51296e13a3023d
4
- data.tar.gz: 5d8e7e994ec2f47c932fb120e1bb9373d6b9d64ab9380cbba98de58483377004
3
+ metadata.gz: a34de5e74d0221b789a2b6f032fbb9925ac5514d9543107346afdcaedb040c69
4
+ data.tar.gz: 0cce23fa1f546f4f23cdd7e7b124e7eb081a66ab599cbf2a16fe14295fe9cb49
5
5
  SHA512:
6
- metadata.gz: 281be07f6004fc567ceb889b1bf63574e08ba8a0e3f250a098b37ba19d9e035718e75993248cc9e4a9d80d1640a5453b71ee4c89848a9f125bae84db16ea046b
7
- data.tar.gz: 6ecf20f2cf802b611c6a5604dd5940962448b70bb3816668f596f81d6c0b46e5292ebf032eeb562c74cd6ea0a78d17623d39280e6e6b73bf25e4c42f38f90b99
6
+ metadata.gz: de6d95058b1386969164de088d9f12d5ec69798d87696bc1ef20e8c88289856e1a25a9266b9489dd6618d3c95296870ffc971feea44f0c0ed27f403f199dded3
7
+ data.tar.gz: 5d47f9663a4249c66e7624753c14d7ae46c77f7da38cb068809a74ac7f6c0bb6b36be6b6b604e3367a56b2cd732cbdf2f7fec4ed4462d0e61351ceefdcc433b1
@@ -27,10 +27,16 @@ module EnumKit
27
27
  ORDER BY pg_type.typname
28
28
  SQL
29
29
 
30
+ # Clear the cached enums to force a refresh the next time they are accessed.
31
+ #
32
+ def clear_enum_cache!
33
+ @enums = nil
34
+ end
35
+
30
36
  # @return [Hash] The enum types available in the database.
31
37
  #
32
38
  def enums
33
- select_all(ENUM_QUERY.tr("\n", ' ').strip).each_with_object({}) do |row, enums|
39
+ @enums ||= select_all(ENUM_QUERY.tr("\n", ' ').strip).each_with_object({}) do |row, enums|
34
40
  enums[row['typname'].to_sym] = row['values'].split("\t\t")
35
41
  end
36
42
  end
@@ -44,7 +50,7 @@ module EnumKit
44
50
  name = EnumKit.sanitize_name!(name)
45
51
  values = EnumKit.sanitize_values!(values)
46
52
 
47
- execute "CREATE TYPE #{name} AS ENUM #{EnumKit.sqlize(values)}"
53
+ enum_execute "CREATE TYPE #{name} AS ENUM #{EnumKit.sqlize(values)}"
48
54
  end
49
55
 
50
56
  # Rename an existing enum type.
@@ -56,7 +62,7 @@ module EnumKit
56
62
  current_name = EnumKit.sanitize_name!(current_name)
57
63
  new_name = EnumKit.sanitize_name!(new_name)
58
64
 
59
- execute "ALTER TYPE #{current_name} RENAME TO #{new_name}"
65
+ enum_execute "ALTER TYPE #{current_name} RENAME TO #{new_name}"
60
66
  end
61
67
 
62
68
  # Drop an existing enum type from the database.
@@ -66,7 +72,7 @@ module EnumKit
66
72
  def drop_enum(name)
67
73
  name = EnumKit.sanitize_name!(name)
68
74
 
69
- execute "DROP TYPE #{name}"
75
+ enum_execute "DROP TYPE #{name}"
70
76
  end
71
77
 
72
78
  # Add a new value to an enum type in the database.
@@ -89,7 +95,7 @@ module EnumKit
89
95
  statement += " AFTER #{EnumKit.sqlize(EnumKit.sanitize_value!(after))}" if after
90
96
  statement += " BEFORE #{EnumKit.sqlize(EnumKit.sanitize_value!(before))}" if before
91
97
 
92
- execute(statement)
98
+ enum_execute(statement)
93
99
  end
94
100
 
95
101
  # Rename a value within an enum type in the database.
@@ -105,7 +111,7 @@ module EnumKit
105
111
  current_name = EnumKit.sanitize_value!(current_name)
106
112
  new_name = EnumKit.sanitize_value!(new_name)
107
113
 
108
- execute "ALTER TYPE #{name} RENAME VALUE #{EnumKit.sqlize(current_name)} TO #{EnumKit.sqlize(new_name)}"
114
+ enum_execute "ALTER TYPE #{name} RENAME VALUE #{EnumKit.sqlize(current_name)} TO #{EnumKit.sqlize(new_name)}"
109
115
  end
110
116
 
111
117
  # :nodoc:
@@ -129,6 +135,14 @@ module EnumKit
129
135
 
130
136
  raise NotImplementedError, 'PostgreSQL 10.0+ is required to enable renaming of enum values.'
131
137
  end
138
+
139
+ # Execute a SQL statement, then clear the enum cache.
140
+ #
141
+ def enum_execute(*args, &block)
142
+ result = execute(*args, &block)
143
+ clear_enum_cache!
144
+ result
145
+ end
132
146
  end
133
147
  end
134
148
  end
@@ -3,5 +3,5 @@
3
3
  module EnumKit
4
4
  # @return [String] The gem's semantic version number.
5
5
  #
6
- VERSION = '0.2.1'
6
+ VERSION = '0.2.2'
7
7
  end
@@ -6,6 +6,9 @@ RSpec.describe ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, :unit do
6
6
  before do
7
7
  # Sanity check to ensure ActiveRecord is configured to use a PostgreSQL database.
8
8
  expect(connection).to be_a(described_class)
9
+
10
+ # Clear the cached enums before each test to ensure clean tests.
11
+ connection.clear_enum_cache!
9
12
  end
10
13
 
11
14
  %i[create_enum rename_enum drop_enum add_enum_value rename_enum_value].each do |method|
@@ -14,6 +17,44 @@ RSpec.describe ActiveRecord::ConnectionAdapters::PostgreSQLAdapter, :unit do
14
17
  end
15
18
  end
16
19
 
20
+ # TODO: Investigate possible alternatives as this test is a bit odd.
21
+ describe '#clear_enum_cache!' do
22
+ it 'sets @enums to nil' do
23
+ connection.instance_eval do
24
+ @enums = { sizes: ['small', 'medium', 'large', 'extra large'] }
25
+ end
26
+
27
+ connection.clear_enum_cache!
28
+
29
+ expect(connection.instance_eval { @enums }).to be nil
30
+ end
31
+ end
32
+
33
+ describe '#enums' do
34
+ before { connection.execute "CREATE TYPE sizes AS ENUM ('small', 'medium', 'large', 'extra large')" }
35
+ after { connection.execute 'DROP TYPE IF EXISTS sizes' }
36
+
37
+ subject { connection.enums }
38
+
39
+ it 'retrieves the enums' do
40
+ expect(subject).to include(sizes: ['small', 'medium', 'large', 'extra large'])
41
+ end
42
+ end
43
+
44
+ describe '#enum_execute' do
45
+ subject { connection.enum_execute('SELECT 1=1') }
46
+
47
+ it 'invokes #execute with the specified arguments' do
48
+ expect(connection).to receive(:execute).with('SELECT 1=1').once
49
+ subject
50
+ end
51
+
52
+ it 'invokes #clear_enum_cache!' do
53
+ expect(connection).to receive(:clear_enum_cache!).once
54
+ subject
55
+ end
56
+ end
57
+
17
58
  describe '#create_enum' do
18
59
  after { connection.execute 'DROP TYPE IF EXISTS sizes' }
19
60
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: enum_kit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nialto Services