rolemodel_sower 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +1 -0
- data/LICENSE +21 -0
- data/README.md +88 -0
- data/examples/rolemodel_sower_data/organizations.csv +2 -0
- data/examples/rolemodel_sower_data/organizations.json +5 -0
- data/examples/rolemodel_sower_data/organizations.tsv +2 -0
- data/examples/rolemodel_sower_data/organizations.yml +2 -0
- data/examples/rolemodel_sower_data/users.csv +4 -0
- data/examples/rolemodel_sower_data/users.json +20 -0
- data/examples/rolemodel_sower_data/users.tsv +4 -0
- data/examples/rolemodel_sower_data/users.yml +15 -0
- data/examples/seeds/rolemodel_sower/organization.rb +9 -0
- data/examples/seeds/rolemodel_sower/user.rb +18 -0
- data/lib/generators/rolemodel_sower/USAGE +3 -0
- data/lib/generators/rolemodel_sower/install_generator.rb +14 -0
- data/lib/generators/rolemodel_sower/templates/config/initializers/rolemodel_sower.rb +6 -0
- data/lib/rolemodel_sower/adapters/base.rb +30 -0
- data/lib/rolemodel_sower/adapters/csv.rb +17 -0
- data/lib/rolemodel_sower/adapters/json.rb +17 -0
- data/lib/rolemodel_sower/adapters/tsv.rb +17 -0
- data/lib/rolemodel_sower/adapters/yaml.rb +15 -0
- data/lib/rolemodel_sower/adapters.rb +13 -0
- data/lib/rolemodel_sower/base.rb +13 -0
- data/lib/rolemodel_sower/version.rb +5 -0
- data/lib/rolemodel_sower.rb +32 -0
- data/rolemodel_sower.gemspec +26 -0
- metadata +68 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 7a83f928ff615025f32fe7060888fbf45508825a44593de8752447324d9c3b53
|
4
|
+
data.tar.gz: cd767fdd458dfd25c62a87a247068d467e3b13bf4cfbbb612b8e6fd4f96356b6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 701c5bfc6fab930f3c0038420e3b24c8828a6677512af4314cea13dac80d808ea798773036c336911419526ea0c1c54ea9a505467ad88947dae2844278d2ef64
|
7
|
+
data.tar.gz: 26aef1405e5a63fdf383f5ea257de0e7ba9c218f70ec5b4e3f686ddecb8c982dbc264b55e9d57f9208e5fed090325232180dca65fc3acf2963b7488a4a8587e1
|
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
*.gem
|
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2023 RoleModel Software
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
# Sower
|
2
|
+
|
3
|
+
A Ruby gem to simplify Seed data creation for Rails applications.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add it to your Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'rolemodel_sower'
|
11
|
+
```
|
12
|
+
|
13
|
+
Run the following command to install it:
|
14
|
+
|
15
|
+
```console
|
16
|
+
bundle install
|
17
|
+
```
|
18
|
+
|
19
|
+
Run the generator:
|
20
|
+
|
21
|
+
```console
|
22
|
+
rails generate rolemodel_sower:install
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
### Creating Seed Data
|
28
|
+
|
29
|
+
Seed data is created by placing files in the `db/rolemodel_sower_data` directory. The files should be named after the model they are creating data for. For example, a file named `users.json` would create seed data for the `User` model.
|
30
|
+
|
31
|
+
`db/rolemodel_sower_data/users.json`
|
32
|
+
|
33
|
+
### Translating Seed Data
|
34
|
+
|
35
|
+
You will need to define a rolemodel_sower file to take the data parsed from your seed data and turn create the records in your database.
|
36
|
+
|
37
|
+
Sower files should be created in `app/seeds/rolemodel_sower`. The files should be named after the model they are creating data for. For example, a file named `user.rb` would create seed data for the `User` model.
|
38
|
+
|
39
|
+
In `db/rolemodel_sower_data/users.json`
|
40
|
+
```json
|
41
|
+
[
|
42
|
+
{
|
43
|
+
"first_name": "John",
|
44
|
+
"last_name": "Doe",
|
45
|
+
"email": "john.doe@example.com"
|
46
|
+
},
|
47
|
+
{
|
48
|
+
"first_name": "Jane",
|
49
|
+
"last_name": "Doe",
|
50
|
+
"email": "jane.doe@example.com"
|
51
|
+
}
|
52
|
+
]
|
53
|
+
```
|
54
|
+
|
55
|
+
In `app/seeds/rolemodel_sower/user.rb`
|
56
|
+
```ruby
|
57
|
+
# frozen_string_literal: true
|
58
|
+
|
59
|
+
module RolemodelSower
|
60
|
+
class User < Sower::Base
|
61
|
+
def load!
|
62
|
+
::User.find_or_create_by!(
|
63
|
+
first_name: @data[:first_name],
|
64
|
+
last_name: @data[:last_name],
|
65
|
+
email: @data[:last_name]
|
66
|
+
)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
```
|
71
|
+
|
72
|
+
### Loading Seed Data
|
73
|
+
|
74
|
+
Seeds can be generated by calling the `seed!` method on `RolemodelSower`.
|
75
|
+
|
76
|
+
It takes one or more symbol arguments that correspond to sets of seed data and runs then in the order passed.
|
77
|
+
|
78
|
+
`RolemodelSower.seed!(:users, :organizations, :facilities)`
|
79
|
+
|
80
|
+
## Adapters
|
81
|
+
|
82
|
+
RolemodelSower defaults to loading yml files, but can be configured to load json, csv, or tsv files. This can be configured in `config/initializers/rolemodel_sower.rb`. (Can be generated by running `rails generate rolemodel_sower:install`)
|
83
|
+
|
84
|
+
```ruby
|
85
|
+
RolemodelSower.setup do |config|
|
86
|
+
config.adapter = :json
|
87
|
+
end
|
88
|
+
```
|
@@ -0,0 +1,20 @@
|
|
1
|
+
[
|
2
|
+
{
|
3
|
+
"first_name": "First",
|
4
|
+
"last_name": "User",
|
5
|
+
"email": "first.user@example.com",
|
6
|
+
"password": "password"
|
7
|
+
},
|
8
|
+
{
|
9
|
+
"first_name": "Second",
|
10
|
+
"last_name": "User",
|
11
|
+
"email": "second.user@example.com",
|
12
|
+
"password": "password"
|
13
|
+
},
|
14
|
+
{
|
15
|
+
"first_name": "Third",
|
16
|
+
"last_name": "User",
|
17
|
+
"email": "third.user@example.com",
|
18
|
+
"password": "password"
|
19
|
+
}
|
20
|
+
]
|
@@ -0,0 +1,15 @@
|
|
1
|
+
-
|
2
|
+
first_name: 'First'
|
3
|
+
last_name: 'User'
|
4
|
+
email: 'first.user@example.com'
|
5
|
+
password: 'password'
|
6
|
+
-
|
7
|
+
first_name: 'Second'
|
8
|
+
last_name: 'User'
|
9
|
+
email: 'second.user@example.com'
|
10
|
+
password: 'password'
|
11
|
+
-
|
12
|
+
first_name: 'Third'
|
13
|
+
last_name: 'User'
|
14
|
+
email: 'third.user@example.com'
|
15
|
+
password: 'password'
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Sower
|
4
|
+
class User < Sower::Base
|
5
|
+
def load!
|
6
|
+
user = ::User.create_with(
|
7
|
+
first_name: @data[:first_name],
|
8
|
+
last_name: @data[:last_name],
|
9
|
+
password: @data[:password],
|
10
|
+
password_confirmation: @data[:password],
|
11
|
+
organization: ::Organization.first,
|
12
|
+
).find_or_create_by! email: @data[:email]
|
13
|
+
|
14
|
+
# Any models can be affected here.
|
15
|
+
::Profile.find_or_create_by!(user: user)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RolemodelSower
|
4
|
+
module Generators
|
5
|
+
class InstallGenerator < Rails::Generators::Base
|
6
|
+
desc 'Copy RolemodelSower default files'
|
7
|
+
source_root File.expand_path('templates', __dir__)
|
8
|
+
|
9
|
+
def copy_config
|
10
|
+
template 'config/initializers/rolemodel_sower.rb'
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RolemodelSower
|
4
|
+
module Adapters
|
5
|
+
class Base
|
6
|
+
def self.file_extension
|
7
|
+
raise NotImplementedError
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.load_data(seed_name)
|
11
|
+
raise NotImplementedError
|
12
|
+
end
|
13
|
+
|
14
|
+
def self.path(seed_name)
|
15
|
+
Rails.root.join("db/rolemodel_sower_data/#{seed_name.to_s.pluralize}#{file_extension}")
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.class_name(seed_name)
|
19
|
+
"RolemodelSower::#{seed_name.to_s.classify}".constantize
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.all(seed_name)
|
23
|
+
load_data(seed_name).map do |template|
|
24
|
+
data = ActiveSupport::HashWithIndifferentAccess.new(template)
|
25
|
+
class_name(seed_name).new(data)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
|
5
|
+
module RolemodelSower
|
6
|
+
module Adapters
|
7
|
+
class CSV < Base
|
8
|
+
def self.file_extension
|
9
|
+
'.csv'
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.load_data(seed_name)
|
13
|
+
::CSV.read(path(seed_name), headers: true)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module RolemodelSower
|
6
|
+
module Adapters
|
7
|
+
class JSON < Base
|
8
|
+
def self.file_extension
|
9
|
+
'.json'
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.load_data(seed_name)
|
13
|
+
::JSON.parse(path(seed_name).read)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'csv'
|
4
|
+
|
5
|
+
module RolemodelSower
|
6
|
+
module Adapters
|
7
|
+
class TSV < Base
|
8
|
+
def self.file_extension
|
9
|
+
'.tsv'
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.load_data(seed_name)
|
13
|
+
::CSV.read(path(seed_name), headers: true, col_sep: "\t")
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,15 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RolemodelSower
|
4
|
+
module Adapters
|
5
|
+
class YAML < Base
|
6
|
+
def self.file_extension
|
7
|
+
'.yml'
|
8
|
+
end
|
9
|
+
|
10
|
+
def self.load_data(seed_name)
|
11
|
+
::YAML.safe_load(ERB.new(path(seed_name).read).result)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RolemodelSower
|
4
|
+
extend ActiveSupport::Autoload
|
5
|
+
|
6
|
+
eager_autoload do
|
7
|
+
autoload :Adapters
|
8
|
+
autoload :Base
|
9
|
+
end
|
10
|
+
|
11
|
+
# CONFIGURATION OPTIONS
|
12
|
+
|
13
|
+
# Available adapters: :yaml, :csv, :tsv, :json
|
14
|
+
mattr_accessor :adapter
|
15
|
+
@@adapter = :yaml
|
16
|
+
|
17
|
+
def self.setup
|
18
|
+
yield self
|
19
|
+
end
|
20
|
+
|
21
|
+
# END CONFIGURATION OPTIONS
|
22
|
+
|
23
|
+
def self.seed!(*seed_names)
|
24
|
+
seed_names.each do |seed_name|
|
25
|
+
logger = Logger.new($stdout)
|
26
|
+
pluralized_name = seed_name.to_s.classify
|
27
|
+
logger.info "Seeding #{pluralized_name}..." if Rails.env.development?
|
28
|
+
"RolemodelSower::Adapters::#{adapter.to_s.upcase}".constantize.all(seed_name).each(&:load!)
|
29
|
+
logger.info "Seeding #{pluralized_name}... done" if Rails.env.development?
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'rolemodel_sower/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = 'rolemodel_sower'
|
9
|
+
s.version = RolemodelSower::VERSION.dup
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.summary = 'A Ruby gem to simplify Seed data creation for Rails applications.'
|
12
|
+
s.email = 'consult@rolemodelsoftware.com'
|
13
|
+
s.homepage = 'https://github.com/RoleModel/sower'
|
14
|
+
s.description = 'A Ruby gem to simplify Seed data creation for Rails applications.'
|
15
|
+
s.authors = ['Jeremy Walton']
|
16
|
+
s.license = 'MIT'
|
17
|
+
|
18
|
+
# Specify which files should be added to the gem when it is released.
|
19
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
20
|
+
s.files = Dir.chdir(File.expand_path(__dir__)) do
|
21
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
22
|
+
end
|
23
|
+
s.require_paths = ['lib']
|
24
|
+
|
25
|
+
s.required_ruby_version = '>= 2.6.0'
|
26
|
+
end
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rolemodel_sower
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jeremy Walton
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2023-07-19 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: A Ruby gem to simplify Seed data creation for Rails applications.
|
14
|
+
email: consult@rolemodelsoftware.com
|
15
|
+
executables: []
|
16
|
+
extensions: []
|
17
|
+
extra_rdoc_files: []
|
18
|
+
files:
|
19
|
+
- ".gitignore"
|
20
|
+
- LICENSE
|
21
|
+
- README.md
|
22
|
+
- examples/rolemodel_sower_data/organizations.csv
|
23
|
+
- examples/rolemodel_sower_data/organizations.json
|
24
|
+
- examples/rolemodel_sower_data/organizations.tsv
|
25
|
+
- examples/rolemodel_sower_data/organizations.yml
|
26
|
+
- examples/rolemodel_sower_data/users.csv
|
27
|
+
- examples/rolemodel_sower_data/users.json
|
28
|
+
- examples/rolemodel_sower_data/users.tsv
|
29
|
+
- examples/rolemodel_sower_data/users.yml
|
30
|
+
- examples/seeds/rolemodel_sower/organization.rb
|
31
|
+
- examples/seeds/rolemodel_sower/user.rb
|
32
|
+
- lib/generators/rolemodel_sower/USAGE
|
33
|
+
- lib/generators/rolemodel_sower/install_generator.rb
|
34
|
+
- lib/generators/rolemodel_sower/templates/config/initializers/rolemodel_sower.rb
|
35
|
+
- lib/rolemodel_sower.rb
|
36
|
+
- lib/rolemodel_sower/adapters.rb
|
37
|
+
- lib/rolemodel_sower/adapters/base.rb
|
38
|
+
- lib/rolemodel_sower/adapters/csv.rb
|
39
|
+
- lib/rolemodel_sower/adapters/json.rb
|
40
|
+
- lib/rolemodel_sower/adapters/tsv.rb
|
41
|
+
- lib/rolemodel_sower/adapters/yaml.rb
|
42
|
+
- lib/rolemodel_sower/base.rb
|
43
|
+
- lib/rolemodel_sower/version.rb
|
44
|
+
- rolemodel_sower.gemspec
|
45
|
+
homepage: https://github.com/RoleModel/sower
|
46
|
+
licenses:
|
47
|
+
- MIT
|
48
|
+
metadata: {}
|
49
|
+
post_install_message:
|
50
|
+
rdoc_options: []
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - ">="
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: 2.6.0
|
58
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
|
+
requirements:
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
63
|
+
requirements: []
|
64
|
+
rubygems_version: 3.2.32
|
65
|
+
signing_key:
|
66
|
+
specification_version: 4
|
67
|
+
summary: A Ruby gem to simplify Seed data creation for Rails applications.
|
68
|
+
test_files: []
|