record_loader 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/cops_and_specs.yml +2 -6
- data/.ruby-version +1 -1
- data/CHANGELOG.md +9 -1
- data/Gemfile.lock +2 -2
- data/README.md +16 -6
- data/lib/generators/record_loader/USAGE +1 -1
- data/lib/generators/record_loader/record_loader_generator.rb +31 -0
- data/lib/generators/record_loader/templates/config/default_records/%underscores%/{default_records.yml → default_records.yml.tt} +1 -6
- data/lib/generators/record_loader/templates/lib/record_loader/%underscore_loader%.rb.tt +2 -1
- data/lib/generators/record_loader/templates/spec/data/record_loader/%underscores%/{two_entry_example.yml → %underscores%_basic.yml.tt} +1 -3
- data/lib/generators/record_loader/templates/spec/lib/record_loader/%underscore_loader%_spec.rb.tt +14 -6
- data/lib/record_loader/attribute.rb +69 -0
- data/lib/record_loader/base.rb +5 -22
- data/lib/record_loader/filter/file_list.rb +31 -0
- data/lib/record_loader/filter/standard.rb +37 -0
- data/lib/record_loader/filter.rb +48 -0
- data/lib/record_loader/version.rb +1 -1
- data/lib/record_loader.rb +15 -0
- metadata +9 -6
- data/.travis.yml +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3124ed2c75924cff8ca8199b02b8325ce115569990611d2936581318f5a26ff6
|
4
|
+
data.tar.gz: 22d2213737d370b87e2b3f57f2ab6d79a682d4ca82947a44abf4611ee665f6f5
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ad50708c4c703d9ae0cc19a6049be3890d1a77c368439070d1647a17d122e0020814be9f510bec7d1a21de7995340a46eb69027ba621b7a6722583ea31f08ee3
|
7
|
+
data.tar.gz: 610ef99153702f568664ea03437c840e3d27670ba3f3ad227b36f796e2a7973c7c273d1d6da27114e952e0ad53d3476e5c781196eda53529c7513224627a7278
|
@@ -21,14 +21,10 @@ jobs:
|
|
21
21
|
steps:
|
22
22
|
- uses: actions/checkout@v2
|
23
23
|
- name: Set up Ruby
|
24
|
-
|
25
|
-
# change this to (see https://github.com/ruby/setup-ruby#versioning):
|
26
|
-
# uses: ruby/setup-ruby@v1
|
27
|
-
uses: ruby/setup-ruby@ec106b438a1ff6ff109590de34ddc62c540232e0
|
24
|
+
uses: ruby/setup-ruby@v1
|
28
25
|
with:
|
29
26
|
ruby-version: 2.5
|
30
|
-
|
31
|
-
run: bundle install
|
27
|
+
bundler-cache: true
|
32
28
|
- name: Run Rubocop
|
33
29
|
run: bundle exec rubocop
|
34
30
|
- name: Test & publish code coverage
|
data/.ruby-version
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
2.7.4
|
data/CHANGELOG.md
CHANGED
@@ -3,7 +3,15 @@
|
|
3
3
|
Keeps track of notable changes. Please remember to add new behaviours to the
|
4
4
|
Unreleased section to make new releases easy.
|
5
5
|
|
6
|
-
## [
|
6
|
+
## [0.2.0]
|
7
|
+
|
8
|
+
- [Added] `RecordLoader.export_attributes` for easy generation of yaml from
|
9
|
+
existing data
|
10
|
+
- [Added] Improved feedback if exceptions raised during record creation
|
11
|
+
- [Added] Improved templated yml files to use attributes from table
|
12
|
+
- [Changed] Update name of yaml files generated as part of tests.
|
13
|
+
No changes are required to existing loaders.
|
14
|
+
- [Fixed] Default yaml files correctly templated
|
7
15
|
|
8
16
|
## [0.1.1]
|
9
17
|
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -43,9 +43,18 @@ Or install it yourself as:
|
|
43
43
|
If you are using Rails, you do not need to make any further changes, and all necessary hooks will be installed when
|
44
44
|
generating your first record loader.
|
45
45
|
|
46
|
-
##
|
46
|
+
## How to run
|
47
|
+
|
48
|
+
To execute all record loaders created for your project you can use the command:
|
49
|
+
|
50
|
+
```bash
|
51
|
+
rake record_loader:all
|
52
|
+
```
|
53
|
+
|
54
|
+
## How to generate new records loaders for your project (Rails)
|
47
55
|
|
48
56
|
RecordLoader provides a generator to automatically build a loader, specs and the yaml files necessary to use it.
|
57
|
+
You may need to manually modify the contents of those files to adapt to the data type you are defining.
|
49
58
|
In addition, the first time you use it it will automatically install the necessary rake files and configuration.
|
50
59
|
You can access this by running:
|
51
60
|
|
@@ -62,12 +71,12 @@ Which will return the documentation:
|
|
62
71
|
Suppose you want to create a loader to maintain a selection of product types. You'll first use the generator:
|
63
72
|
|
64
73
|
```bash
|
65
|
-
$ bundle exec rails g record_loader
|
74
|
+
$ bundle exec rails g record_loader product_type
|
66
75
|
exist
|
67
76
|
create config/record_loader/product_types/default_records.yml
|
68
77
|
create lib/record_loader/product_type_loader.rb
|
69
78
|
create lib/record_loader/tasks/record_loader/product_type.rake
|
70
|
-
create spec/data/record_loader/product_types/
|
79
|
+
create spec/data/record_loader/product_types/product_types_basic.yml
|
71
80
|
create spec/lib/record_loader/product_type_loader_spec.rb
|
72
81
|
skip lib/record_loader/application_record_loader.rb
|
73
82
|
identical lib/tasks/record_loader.rake
|
@@ -123,7 +132,7 @@ with the first argument being the key, and the second argument being the value,
|
|
123
132
|
This contains the `record_loader:product_type` which will trigger the record loader, and also ensures that
|
124
133
|
`record_loader:product_type` will get invoked on calling `record_loader:all`.
|
125
134
|
|
126
|
-
#### `spec/data/record_loader/product_types/
|
135
|
+
#### `spec/data/record_loader/product_types/product_types_basic.yml`
|
127
136
|
|
128
137
|
A basic configuration for testing the loader. Tests use a separate directory to avoid coupling your specs to the data.
|
129
138
|
|
@@ -157,14 +166,15 @@ If you have an existing feature flag system you can use this instead by adding a
|
|
157
166
|
## RecordLoader Dependencies
|
158
167
|
|
159
168
|
Sometimes one loader will be dependent on the output of another. If this is the case, you can simply configure
|
160
|
-
its rake task to use the other as a pre-requisite.
|
169
|
+
its rake task to use the other as a pre-requisite. Rake's dependency handling is smart enough to ensure each
|
170
|
+
task only gets run once.
|
161
171
|
|
162
172
|
For example
|
163
173
|
|
164
174
|
```ruby
|
165
175
|
namespace :record_loader do
|
166
176
|
desc 'Automatically generate Dependent through DependentLoader'
|
167
|
-
task dependent: [:environment, :prerequisite] do
|
177
|
+
task dependent: [:environment, 'record_loader:prerequisite'] do
|
168
178
|
RecordLoader::DependentLoader.new.create!
|
169
179
|
end
|
170
180
|
end
|
@@ -32,5 +32,5 @@ Example:
|
|
32
32
|
config/record_loader/my_things/default_records.yml
|
33
33
|
lib/record_loader/my_thing_loader.rb
|
34
34
|
lib/record_loader/tasks/record_loader/my_thing.rake
|
35
|
-
spec/data/record_loader/my_things/
|
35
|
+
spec/data/record_loader/my_things/my_things_basic.yml
|
36
36
|
spec/lib/record_loader/my_thing_loader_spec.rb
|
@@ -1,8 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
+
require 'record_loader/attribute'
|
4
|
+
|
3
5
|
# Rails generator to automatically build record loaders
|
4
6
|
# @see lib/generators/record_loader/USAGE
|
5
7
|
class RecordLoaderGenerator < Rails::Generators::NamedBase
|
8
|
+
IGNORED_COLUMNS = %w[created_at updated_at].freeze
|
9
|
+
|
6
10
|
source_root File.expand_path('templates', __dir__)
|
7
11
|
|
8
12
|
class_option :record_class, type: :string, default: nil,
|
@@ -27,8 +31,35 @@ class RecordLoaderGenerator < Rails::Generators::NamedBase
|
|
27
31
|
skip: true
|
28
32
|
end
|
29
33
|
|
34
|
+
def build_example_hash(size = 2)
|
35
|
+
(1..size).each_with_object({}) do |iteration, store|
|
36
|
+
store["Unique #{record_key} #{iteration}"] = loader_attributes.each_with_object({}) do |attribute, config|
|
37
|
+
config[attribute.name] = attribute.value(iteration)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
30
42
|
private
|
31
43
|
|
44
|
+
def klass
|
45
|
+
@klass ||= record_class.constantize
|
46
|
+
end
|
47
|
+
|
48
|
+
def loader_attributes
|
49
|
+
klass.content_columns
|
50
|
+
.reject { |column| IGNORED_COLUMNS.include?(column.name) }
|
51
|
+
.map { |column| RecordLoader::Attribute.new(column.name, column.type, defaults[column.name]) }
|
52
|
+
end
|
53
|
+
|
54
|
+
# The defaults returned by content_columns are not coerced
|
55
|
+
def defaults
|
56
|
+
klass.column_defaults
|
57
|
+
end
|
58
|
+
|
59
|
+
def attributes_values(index)
|
60
|
+
loader_attributes.map { |a| "#{a.name}: #{a.ruby_value(index)}" }.join(', ')
|
61
|
+
end
|
62
|
+
|
32
63
|
def loader_class_name
|
33
64
|
"#{name.camelcase}Loader"
|
34
65
|
end
|
@@ -1,11 +1,6 @@
|
|
1
|
-
---
|
2
1
|
# Add entries to this file to generate them automatically upon running
|
3
2
|
# rake record_loader:all or rake record_loader:<%= underscore %>
|
4
3
|
# If you want entries which are only created in development mode then simply
|
5
4
|
# create a file in this directory ending in '.dev.yml'
|
6
5
|
# You can add as many additional .yml files to this directory as you wish.
|
7
|
-
#
|
8
|
-
# <%= record_key %> A:
|
9
|
-
# other_attribute: 'Value'
|
10
|
-
# <%= record_key %> B:
|
11
|
-
# other_attribute: 'Value'
|
6
|
+
<%= build_example_hash(2).to_yaml.gsub(/^/,'# ') %>
|
@@ -1,11 +1,12 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
|
+
|
2
3
|
# This file was automatically generated via `rails g record_loader`
|
3
4
|
|
4
5
|
# RecordLoader handles automatic population and updating of database records
|
5
6
|
# across different environments
|
6
7
|
# @see https://rubydoc.info/github/sanger/record_loader/
|
7
8
|
module RecordLoader
|
8
|
-
# Creates the specified
|
9
|
+
# Creates the specified <%= record_class %> if they are not present
|
9
10
|
class <%= loader_class_name %> < ApplicationRecordLoader
|
10
11
|
config_folder '<%= underscores %>'
|
11
12
|
|
data/lib/generators/record_loader/templates/spec/lib/record_loader/%underscore_loader%_spec.rb.tt
CHANGED
@@ -5,27 +5,35 @@ require 'record_loader/<%= underscore_loader %>'
|
|
5
5
|
|
6
6
|
# This file was initially generated via `rails g record_loader`
|
7
7
|
RSpec.describe RecordLoader::<%= loader_class_name %>, type: :model, loader: true do
|
8
|
-
|
8
|
+
def a_new_record_loader
|
9
9
|
described_class.new(directory: test_directory, files: selected_files)
|
10
10
|
end
|
11
11
|
|
12
|
+
subject(:record_loader) { a_new_record_loader }
|
13
|
+
|
12
14
|
# Tests use a separate directory to avoid coupling your specs to the data
|
13
15
|
let(:test_directory) { Rails.root.join('spec/data/record_loader/<%= underscores %>') }
|
14
16
|
|
15
|
-
context 'with
|
16
|
-
let(:selected_files) { '
|
17
|
+
context 'with <%= underscores %>_basic selected' do
|
18
|
+
let(:selected_files) { '<%= underscores %>_basic' }
|
17
19
|
|
18
20
|
it 'creates two records' do
|
19
|
-
expect { record_loader.create! }.to change
|
21
|
+
expect { record_loader.create! }.to change(<%= record_class %>, :count).by(2)
|
20
22
|
end
|
21
23
|
|
22
24
|
# It is important that multiple runs of a RecordLoader do not create additional
|
23
25
|
# copies of existing records.
|
24
26
|
it 'is idempotent' do
|
25
27
|
record_loader.create!
|
26
|
-
expect {
|
28
|
+
expect { a_new_record_loader }.not_to change(<%= record_class %>, :count)
|
27
29
|
end
|
28
30
|
|
29
|
-
it 'sets attributes on the created records'
|
31
|
+
it 'sets attributes on the created records' do
|
32
|
+
record_loader.create!
|
33
|
+
expect(<%= record_class %>.all).to include(
|
34
|
+
have_attributes(<%= attributes_values(0) %>),
|
35
|
+
have_attributes(<%= attributes_values(1) %>)
|
36
|
+
)
|
37
|
+
end
|
30
38
|
end
|
31
39
|
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'date'
|
4
|
+
|
5
|
+
module RecordLoader
|
6
|
+
# Used by the generator to help guide the generation of the initial
|
7
|
+
# yaml files
|
8
|
+
class Attribute
|
9
|
+
BASE_TIME = Time.parse('2021-10-08 13:00:40 +0100')
|
10
|
+
BASE_DATE = Date.parse('2021-10-08')
|
11
|
+
|
12
|
+
# @return [String] The name of the attribute, takes the column name
|
13
|
+
attr_reader :name
|
14
|
+
|
15
|
+
# @return [Symbol] The attribute type (eg. :string) as returned by the rails connection adapter
|
16
|
+
attr_reader :type
|
17
|
+
|
18
|
+
# @return [Object] The default of the attribute
|
19
|
+
attr_reader :default
|
20
|
+
|
21
|
+
#
|
22
|
+
# Create a new attribute
|
23
|
+
#
|
24
|
+
# @param name [String] The name of the attribute, takes the column name
|
25
|
+
# @param type [Symbol] The attribute type (eg. :string) as returned by the rails connection adapter
|
26
|
+
# @param default [Object] The default value of the column
|
27
|
+
#
|
28
|
+
def initialize(name, type, default)
|
29
|
+
@name = name
|
30
|
+
@type = type
|
31
|
+
@default = default
|
32
|
+
end
|
33
|
+
|
34
|
+
def value(index)
|
35
|
+
if @default.nil?
|
36
|
+
value_for_type(index)
|
37
|
+
else
|
38
|
+
@default
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def ruby_value(index)
|
43
|
+
case type
|
44
|
+
when :datetime, :timestamp then "Time.parse('#{value(index)}')"
|
45
|
+
when :date then "Date.parse('#{value(index)}')"
|
46
|
+
else
|
47
|
+
value(index).inspect
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
# If this gets much more complicated then consider separate classes for
|
54
|
+
# handling the type specific behaviour. Avoiding for now as I'm not sure it
|
55
|
+
# adds much
|
56
|
+
def value_for_type(index) # rubocop:disable Metrics/CyclomaticComplexity
|
57
|
+
case type
|
58
|
+
when :integer then index
|
59
|
+
when :datetime, :timestamp then BASE_TIME + index
|
60
|
+
when :date then BASE_DATE + index
|
61
|
+
when :boolean then false
|
62
|
+
when :float, :decimal then index.to_f
|
63
|
+
when :json then {}
|
64
|
+
else # Covers string and text, but also a fallback for other column types
|
65
|
+
"#{name.tr('_', ' ')} #{index}"
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
data/lib/record_loader/base.rb
CHANGED
@@ -65,9 +65,10 @@ module RecordLoader
|
|
65
65
|
#
|
66
66
|
def initialize(files: nil, directory: default_path, dev: adapter.development?)
|
67
67
|
@path = directory.is_a?(Pathname) ? directory : Pathname.new(directory)
|
68
|
-
|
68
|
+
|
69
|
+
list = Filter.create(files: files, dev: dev, wip_list: wip_list)
|
69
70
|
@files = @path.glob("*#{RecordFile::EXTENSION}")
|
70
|
-
.select { |child|
|
71
|
+
.select { |child| list.include?(RecordFile.new(child)) }
|
71
72
|
load_config
|
72
73
|
end
|
73
74
|
|
@@ -80,6 +81,8 @@ module RecordLoader
|
|
80
81
|
adapter.transaction do
|
81
82
|
@config.each do |key, config|
|
82
83
|
create_or_update!(key, config)
|
84
|
+
rescue StandardError => e
|
85
|
+
raise StandardError, "Failed to create #{key} due to: #{e.message}"
|
83
86
|
end
|
84
87
|
end
|
85
88
|
end
|
@@ -125,26 +128,6 @@ module RecordLoader
|
|
125
128
|
Pathname.pwd.join(*base_config_path, self.class.config_folder)
|
126
129
|
end
|
127
130
|
|
128
|
-
#
|
129
|
-
# Indicates that a file should be loaded
|
130
|
-
#
|
131
|
-
# @param [Array] list provides an array of files (minus extenstions) to load
|
132
|
-
# @param [Pathname] file The file to check
|
133
|
-
#
|
134
|
-
# @return [Boolean] returns true if the file should be loaded
|
135
|
-
#
|
136
|
-
def load_file?(list, file)
|
137
|
-
if list.nil?
|
138
|
-
return @dev if file.dev?
|
139
|
-
return wip_list.include?(file.basename) if file.wip?
|
140
|
-
|
141
|
-
true
|
142
|
-
else
|
143
|
-
# If we've provided a list, that's all that matters
|
144
|
-
list.include?(file.basename)
|
145
|
-
end
|
146
|
-
end
|
147
|
-
|
148
131
|
#
|
149
132
|
# Load the appropriate configuration files into @config
|
150
133
|
#
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RecordLoader
|
4
|
+
module Filter
|
5
|
+
# FileList Filters handle determining whether a file is loaded based on the
|
6
|
+
# Supplied lift of files. WIP and environment flags are ignored.
|
7
|
+
class FileList
|
8
|
+
#
|
9
|
+
# Create a new FileList filter
|
10
|
+
#
|
11
|
+
# @param files [Array<String>,NilClass] pass in an array of file names to load, or nil to load all files.
|
12
|
+
#
|
13
|
+
def initialize(file_list)
|
14
|
+
@file_list = file_list
|
15
|
+
end
|
16
|
+
|
17
|
+
#
|
18
|
+
# Indicates that a file should be loaded. Compares the files base_name
|
19
|
+
# (ie. the name excluding extensions or wip/environment flags) against the
|
20
|
+
# provided list.
|
21
|
+
#
|
22
|
+
# @param [RecordLoader::RecordFile] file The file to check
|
23
|
+
#
|
24
|
+
# @return [Boolean] returns true if the file should be loaded
|
25
|
+
#
|
26
|
+
def include?(file)
|
27
|
+
@file_list.include?(file.basename)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RecordLoader
|
4
|
+
module Filter
|
5
|
+
# Filters handle determining whether a file is loaded based on the environment
|
6
|
+
# and WIP flags.
|
7
|
+
class Standard
|
8
|
+
#
|
9
|
+
# Create a new standard filter
|
10
|
+
#
|
11
|
+
# @param dev [Boolean] Override the rails environment to generate (or not)
|
12
|
+
# data from dev.yml files.
|
13
|
+
# @param wip_list [Array<String>] An array of currently active WIP flags.
|
14
|
+
#
|
15
|
+
def initialize(dev:, wip_list:)
|
16
|
+
@dev = dev
|
17
|
+
@wip_list = wip_list
|
18
|
+
end
|
19
|
+
|
20
|
+
#
|
21
|
+
# Indicates that a file should be loaded. Checks the environment flags
|
22
|
+
# against the current environment, or if the file is flagged as WIP
|
23
|
+
# check it against the active wip flags.
|
24
|
+
#
|
25
|
+
# @param [RecordLoader::RecordFile] file The file to check
|
26
|
+
#
|
27
|
+
# @return [Boolean] returns true if the file should be loaded
|
28
|
+
#
|
29
|
+
def include?(file)
|
30
|
+
return @dev if file.dev?
|
31
|
+
return @wip_list.include?(file.basename) if file.wip?
|
32
|
+
|
33
|
+
true
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'filter/file_list'
|
4
|
+
require_relative 'filter/standard'
|
5
|
+
|
6
|
+
module RecordLoader
|
7
|
+
#
|
8
|
+
# {RecordLoader::Filter}s provide a means of determining if a file should be
|
9
|
+
# loaded. This is currently an internal mechanism, and no interface is
|
10
|
+
# provided for adding your own filters.
|
11
|
+
#
|
12
|
+
# = Existing Filters
|
13
|
+
# There are currently two adapters used
|
14
|
+
#
|
15
|
+
# - {RecordLoader::Filter::Standard}
|
16
|
+
# The default filter. Loads files based on environment and WIP flags. Used by
|
17
|
+
# {RecordLoader::Base} if no list of files is supplied.
|
18
|
+
#
|
19
|
+
# - {RecordLoader::Filter::FileList}
|
20
|
+
# Filters files solely based on a supplied list. Used by {RecordLoader::Base}
|
21
|
+
# if a list of files is supplied. Overides wip flag and environment specific
|
22
|
+
# behaviour.
|
23
|
+
#
|
24
|
+
# = Custom Filters
|
25
|
+
# There is currently no support for custom filters.
|
26
|
+
#
|
27
|
+
module Filter
|
28
|
+
# Returns the appropriate filter for the provided arguments:
|
29
|
+
# - {RecordLoader::Filter::FileList}
|
30
|
+
# If a list of files is supplied
|
31
|
+
# - {RecordLoader::Filter::Standard}
|
32
|
+
# Otherwise
|
33
|
+
#
|
34
|
+
# @param files [Array<String>,NilClass] pass in an array of file names to load, or nil to load all files.
|
35
|
+
# Dev and wip flags will be ignored for files passed in explicitly
|
36
|
+
# @param dev [Boolean] Override the rails environment to generate (or not)
|
37
|
+
# data from dev.yml files.
|
38
|
+
# @param, wip_list [Array<String>] An array of currently active WIP flags.
|
39
|
+
# @return [RecordLoader::Filter::Standard, RecordLoader::Filter::FileList] An appropriate filter
|
40
|
+
def self.create(files: nil, dev: false, wip_list: [])
|
41
|
+
if files.nil?
|
42
|
+
RecordLoader::Filter::Standard.new(dev: dev, wip_list: wip_list)
|
43
|
+
else
|
44
|
+
RecordLoader::Filter::FileList.new(files)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
data/lib/record_loader.rb
CHANGED
@@ -2,6 +2,7 @@
|
|
2
2
|
|
3
3
|
require 'record_loader/version'
|
4
4
|
require 'record_loader/adapter'
|
5
|
+
require 'record_loader/filter'
|
5
6
|
require 'record_loader/base'
|
6
7
|
require 'record_loader/record_file'
|
7
8
|
|
@@ -12,4 +13,18 @@ require 'record_loader/railtie' if defined?(Rails)
|
|
12
13
|
module RecordLoader
|
13
14
|
# Raised when it appears RecordLoader is improperly configured
|
14
15
|
ConfigurationError = Class.new(StandardError)
|
16
|
+
|
17
|
+
#
|
18
|
+
# Helper method to automatically generate config yaml for existing data.
|
19
|
+
# This method does not handle setting up associations as the best way of
|
20
|
+
# serializing that information is left up to the individual RecordLoader
|
21
|
+
#
|
22
|
+
# @param [ActiveRecord::Base] model_class A class to serialize
|
23
|
+
# @param [String] key The key to index by (eg. 'name')
|
24
|
+
# @return [String] Yaml listing the existing records
|
25
|
+
def self.export_attributes(model_class, key)
|
26
|
+
model_class.all.each_with_object({}) do |record, store|
|
27
|
+
store[record[key]] = record.attributes.except('id', 'updated_at', 'created_at', key)
|
28
|
+
end.to_yaml
|
29
|
+
end
|
15
30
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: record_loader
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Glover
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2022-08-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -136,7 +136,6 @@ files:
|
|
136
136
|
- ".rspec"
|
137
137
|
- ".rubocop.yml"
|
138
138
|
- ".ruby-version"
|
139
|
-
- ".travis.yml"
|
140
139
|
- ".yardopts"
|
141
140
|
- CHANGELOG.md
|
142
141
|
- Gemfile
|
@@ -150,16 +149,20 @@ files:
|
|
150
149
|
- lib/generators/record_loader/record_loader_generator.rb
|
151
150
|
- lib/generators/record_loader/static_files/application_record_loader.rb
|
152
151
|
- lib/generators/record_loader/static_files/record_loader.rake
|
153
|
-
- lib/generators/record_loader/templates/config/default_records/%underscores%/default_records.yml
|
152
|
+
- lib/generators/record_loader/templates/config/default_records/%underscores%/default_records.yml.tt
|
154
153
|
- lib/generators/record_loader/templates/lib/record_loader/%underscore_loader%.rb.tt
|
155
154
|
- lib/generators/record_loader/templates/lib/tasks/record_loader/%underscore%.rake.tt
|
156
|
-
- lib/generators/record_loader/templates/spec/data/record_loader/%underscores
|
155
|
+
- lib/generators/record_loader/templates/spec/data/record_loader/%underscores%/%underscores%_basic.yml.tt
|
157
156
|
- lib/generators/record_loader/templates/spec/lib/record_loader/%underscore_loader%_spec.rb.tt
|
158
157
|
- lib/record_loader.rb
|
159
158
|
- lib/record_loader/adapter.rb
|
160
159
|
- lib/record_loader/adapter/basic.rb
|
161
160
|
- lib/record_loader/adapter/rails.rb
|
161
|
+
- lib/record_loader/attribute.rb
|
162
162
|
- lib/record_loader/base.rb
|
163
|
+
- lib/record_loader/filter.rb
|
164
|
+
- lib/record_loader/filter/file_list.rb
|
165
|
+
- lib/record_loader/filter/standard.rb
|
163
166
|
- lib/record_loader/railtie.rb
|
164
167
|
- lib/record_loader/record_file.rb
|
165
168
|
- lib/record_loader/version.rb
|
@@ -185,7 +188,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
185
188
|
- !ruby/object:Gem::Version
|
186
189
|
version: '0'
|
187
190
|
requirements: []
|
188
|
-
rubygems_version: 3.
|
191
|
+
rubygems_version: 3.1.6
|
189
192
|
signing_key:
|
190
193
|
specification_version: 4
|
191
194
|
summary: Easily manage seeding and updating data from simple yml files
|