support_table_data 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +7 -0
- data/README.md +18 -3
- data/VERSION +1 -1
- data/lib/support_table_data.rb +75 -18
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: db981a2e7549dc34a3e7f3f7d02f1f441776064619b1b6390bbdd866a62f1872
|
4
|
+
data.tar.gz: 7022a030e3007f37408e2596b813268455e786dc8b92c4c8c7d75b00c06947b0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 14610fa5a3e91cec068aa0ed041d1763e28e75b3446237018ca7c11fd536744800f348fb2a9c89d1ef5c7b0933b182792aebe458032675716388f9ba3ca0bfdd
|
7
|
+
data.tar.gz: d28172a9344033c946aca044af36398be5232a0ad893c9bc2b19d863eff671df8773c5d99f4601dce0c0d5c64def0771bae364b0df28a1aba1b7a747cdafa45c
|
data/CHANGELOG.md
CHANGED
@@ -4,7 +4,14 @@ All notable changes to this project will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
+
## 1.1.0
|
8
|
+
|
9
|
+
### Added
|
10
|
+
|
11
|
+
- Helper methods can defined on the class to expose attributes for named instances without requiring a database connection.
|
12
|
+
|
7
13
|
## 1.0.0
|
8
14
|
|
9
15
|
### Added
|
16
|
+
|
10
17
|
- Add SupportTableData concern to enable automatic syncing of data on support tables.
|
data/README.md
CHANGED
@@ -68,17 +68,17 @@ Here is an example data file using named instances:
|
|
68
68
|
pending:
|
69
69
|
id: 1
|
70
70
|
name: Pending
|
71
|
-
icon:
|
71
|
+
icon: clock
|
72
72
|
|
73
73
|
in_progress:
|
74
74
|
id: 2
|
75
75
|
name: In Progress
|
76
|
-
icon:
|
76
|
+
icon: construction
|
77
77
|
|
78
78
|
completed:
|
79
79
|
id: 3
|
80
80
|
name: Completed
|
81
|
-
icon:
|
81
|
+
icon: heavy_check_mark
|
82
82
|
|
83
83
|
_others:
|
84
84
|
- id: 4
|
@@ -104,6 +104,21 @@ status.completed? # status.id == 3
|
|
104
104
|
|
105
105
|
Helper methods will not override already defined methods on a model class. If a method is already defined, an `ArgumentError` will be raised.
|
106
106
|
|
107
|
+
You can also define helper methods for named instance attributes. These helper methods will return the hard coded values from the data file. Calling these methods does not require a database connection.
|
108
|
+
|
109
|
+
|
110
|
+
```ruby
|
111
|
+
class Status < ApplicationRecord
|
112
|
+
include SupportTableData
|
113
|
+
|
114
|
+
named_instance_attribute_helpers :id
|
115
|
+
end
|
116
|
+
|
117
|
+
Status.pending_id # => 1
|
118
|
+
Status.in_progress_id # => 2
|
119
|
+
Status.completed_id # => 3
|
120
|
+
```
|
121
|
+
|
107
122
|
### Caching
|
108
123
|
|
109
124
|
You can use the companion [support_table_cache gem](https://github.com/bdurand/support_table_cache) to add caching support to your models. That way your application won't need to constantly query the database for records that will never change.
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
1.
|
1
|
+
1.1.0
|
data/lib/support_table_data.rb
CHANGED
@@ -64,6 +64,30 @@ module SupportTableData
|
|
64
64
|
define_support_table_named_instances
|
65
65
|
end
|
66
66
|
|
67
|
+
# Add class methods to get attributes for named instances. The methods will be named
|
68
|
+
# like `#{instance_name}_#{attribute_name}`. For example, if the name is "active" and the
|
69
|
+
# attribute is "id", then the method will be "active_id" and you can call
|
70
|
+
# `Model.active_id` to get the value.
|
71
|
+
#
|
72
|
+
# @param attributes [String, Symbol] The names of the attributes to add helper methods for.
|
73
|
+
# @return [void]
|
74
|
+
def named_instance_attribute_helpers(*attributes)
|
75
|
+
@support_table_attribute_helpers ||= {}
|
76
|
+
attributes.flatten.collect(&:to_s).each do |attribute|
|
77
|
+
@support_table_attribute_helpers[attribute] = []
|
78
|
+
end
|
79
|
+
define_support_table_named_instances
|
80
|
+
end
|
81
|
+
|
82
|
+
# Get the names of any named instance attribute helpers that have been defined
|
83
|
+
# with `named_instance_attribute_helpers`.
|
84
|
+
#
|
85
|
+
# @return [Array<String>] List of attribute names.
|
86
|
+
def support_table_attribute_helpers
|
87
|
+
@support_table_attribute_helpers ||= {}
|
88
|
+
@support_table_attribute_helpers.keys
|
89
|
+
end
|
90
|
+
|
67
91
|
# Get the data for the support table from the data files.
|
68
92
|
#
|
69
93
|
# @return [Array<Hash>] List of attributes for all records in the data files.
|
@@ -135,35 +159,52 @@ module SupportTableData
|
|
135
159
|
def define_support_table_named_instances
|
136
160
|
@support_table_data_files ||= []
|
137
161
|
@support_table_instance_names ||= Set.new
|
138
|
-
key_attribute = (support_table_key_attribute || primary_key).to_s
|
139
162
|
|
140
163
|
@support_table_data_files.each do |file_path|
|
141
164
|
data = support_table_parse_data_file(file_path)
|
142
|
-
|
143
|
-
data.each do |key, attributes|
|
144
|
-
method_name = key.to_s.freeze
|
145
|
-
next if method_name.start_with?("_")
|
165
|
+
next unless data.is_a?(Hash)
|
146
166
|
|
147
|
-
|
148
|
-
|
149
|
-
|
167
|
+
data.each do |name, attributes|
|
168
|
+
define_support_table_named_instance_methods(name, attributes)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
150
172
|
|
151
|
-
|
152
|
-
|
153
|
-
|
173
|
+
def define_support_table_named_instance_methods(name, attributes)
|
174
|
+
method_name = name.to_s.freeze
|
175
|
+
return if method_name.start_with?("_")
|
154
176
|
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
177
|
+
unless attributes.is_a?(Hash)
|
178
|
+
raise ArgumentError.new("Cannot define named instance #{method_name} on #{name}; value must be a Hash")
|
179
|
+
end
|
180
|
+
|
181
|
+
unless method_name.match?(/\A[a-z][a-z0-9_]+\z/)
|
182
|
+
raise ArgumentError.new("Cannot define named instance #{method_name} on #{name}; name contains illegal characters")
|
183
|
+
end
|
184
|
+
|
185
|
+
key_attribute = (support_table_key_attribute || primary_key).to_s
|
186
|
+
key_value = attributes[key_attribute]
|
187
|
+
|
188
|
+
unless @support_table_instance_names.include?(method_name)
|
189
|
+
define_support_table_instance_helper(method_name, key_attribute, key_value)
|
190
|
+
define_support_table_predicates_helper("#{method_name}?", key_attribute, key_value)
|
191
|
+
@support_table_instance_names << method_name
|
192
|
+
end
|
193
|
+
|
194
|
+
if defined?(@support_table_attribute_helpers)
|
195
|
+
@support_table_attribute_helpers.each do |attribute_name, defined_methods|
|
196
|
+
attribute_method_name = "#{method_name}_#{attribute_name}"
|
197
|
+
next if defined_methods.include?(attribute_method_name)
|
198
|
+
|
199
|
+
define_support_table_instance_attribute_helper(attribute_method_name, attributes[attribute_name])
|
200
|
+
defined_methods << attribute_method_name
|
162
201
|
end
|
163
202
|
end
|
164
203
|
end
|
165
204
|
|
166
205
|
def define_support_table_instance_helper(method_name, attribute_name, attribute_value)
|
206
|
+
return if @support_table_instance_names.include?("self.#{method_name}")
|
207
|
+
|
167
208
|
if respond_to?(method_name, true)
|
168
209
|
raise ArgumentError.new("Could not define support table helper method #{name}.#{method_name} because it is already a defined method")
|
169
210
|
end
|
@@ -175,7 +216,23 @@ module SupportTableData
|
|
175
216
|
RUBY
|
176
217
|
end
|
177
218
|
|
219
|
+
def define_support_table_instance_attribute_helper(method_name, attribute_value)
|
220
|
+
return if @support_table_instance_names.include?("self.#{method_name}")
|
221
|
+
|
222
|
+
if respond_to?(method_name, true)
|
223
|
+
raise ArgumentError.new("Could not define support table helper method #{name}.#{method_name} because it is already a defined method")
|
224
|
+
end
|
225
|
+
|
226
|
+
class_eval <<~RUBY, __FILE__, __LINE__ + 1
|
227
|
+
def self.#{method_name}
|
228
|
+
#{attribute_value.inspect}
|
229
|
+
end
|
230
|
+
RUBY
|
231
|
+
end
|
232
|
+
|
178
233
|
def define_support_table_predicates_helper(method_name, attribute_name, attribute_value)
|
234
|
+
return if @support_table_instance_names.include?(method_name)
|
235
|
+
|
179
236
|
if method_defined?(method_name) || private_method_defined?(method_name)
|
180
237
|
raise ArgumentError.new("Could not define support table helper method #{name}##{method_name} because it is already a defined method")
|
181
238
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: support_table_data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Brian Durand
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2023-04-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -38,7 +38,7 @@ dependencies:
|
|
38
38
|
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
|
-
description:
|
41
|
+
description:
|
42
42
|
email:
|
43
43
|
- bbdurand@gmail.com
|
44
44
|
executables: []
|
@@ -57,7 +57,7 @@ homepage: https://github.com/bdurand/support_table_data
|
|
57
57
|
licenses:
|
58
58
|
- MIT
|
59
59
|
metadata: {}
|
60
|
-
post_install_message:
|
60
|
+
post_install_message:
|
61
61
|
rdoc_options: []
|
62
62
|
require_paths:
|
63
63
|
- lib
|
@@ -72,8 +72,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
72
72
|
- !ruby/object:Gem::Version
|
73
73
|
version: '0'
|
74
74
|
requirements: []
|
75
|
-
rubygems_version: 3.
|
76
|
-
signing_key:
|
75
|
+
rubygems_version: 3.4.12
|
76
|
+
signing_key:
|
77
77
|
specification_version: 4
|
78
78
|
summary: Extension for ActiveRecord models to manage synchronizing data in support/lookup
|
79
79
|
tables across environments. Also provides the ability to directly reference and
|