oaken 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +24 -1
- data/lib/oaken/rspec_setup.rb +2 -3
- data/lib/oaken/seeds.rb +30 -3
- data/lib/oaken/stored/active_record.rb +1 -1
- data/lib/oaken/version.rb +1 -1
- data/lib/oaken.rb +4 -9
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 81f5e7a3ea1f5236b6e2726d1ecd24dee5698b59dc5e73f80ce618181b1dffe1
|
4
|
+
data.tar.gz: 6c4628800cdc44acd516a8e51a708fdee3d63ce506679fa5bc742b337da5c725
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5257b630ef4ac76db5b9fceb161df47d71e1df3481d1fdcd45fb1ac35319a72568d084f6e48a158cca3217512382890d5919111f329b08798f388e46d4ff5014
|
7
|
+
data.tar.gz: 2b91b6caeede127715691ecdeab3b8355ba273d9105b11f52579f7c4da2dcfefb7bfbc243ee4cc7b7de2af6401e95e195b4c377d31502ddd0162043e243c07aa
|
data/README.md
CHANGED
@@ -64,6 +64,23 @@ Oaken has some chosen directory conventions to help strengthen your understandin
|
|
64
64
|
- `db/seeds/data` for any data tables, like the plans a SaaS app has.
|
65
65
|
- `db/seeds/tests/cases` for any specific cases that are only used in some tests, like `pagination.rb`.
|
66
66
|
|
67
|
+
### Using default attributes
|
68
|
+
|
69
|
+
You can set up default attributes that's applied to created/inserted records at different levels, like this:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
Oaken.prepare do
|
73
|
+
# Assign broad global defaults for every type.
|
74
|
+
defaults name: -> { Faker::Name.name }, public_key: -> { SecureRandom.hex }
|
75
|
+
|
76
|
+
# Assign a more specific default on one type, which overrides the global default above.
|
77
|
+
accounts.defaults name: -> { Faker::Business.name }
|
78
|
+
end
|
79
|
+
```
|
80
|
+
|
81
|
+
> [!TIP]
|
82
|
+
> `defaults` are particularly well suited for assigning generated data with [Faker](https://github.com/faker-ruby/faker).
|
83
|
+
|
67
84
|
### Reusing data in tests
|
68
85
|
|
69
86
|
With the setup above, Oaken can reuse the same data in tests like this:
|
@@ -77,6 +94,12 @@ end
|
|
77
94
|
|
78
95
|
Now tests have access to `accounts.kaspers_donuts` and `users.kasper` etc. that were setup in the data scripts.
|
79
96
|
|
97
|
+
> [!NOTE]
|
98
|
+
> For RSpec, you can put this in `spec/rails_helper.rb`:
|
99
|
+
> ```ruby
|
100
|
+
> require "oaken/rspec_setup"
|
101
|
+
> ```
|
102
|
+
|
80
103
|
You can also load a specific seed, like this:
|
81
104
|
|
82
105
|
```ruby
|
@@ -113,7 +136,7 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
113
136
|
|
114
137
|
## Development
|
115
138
|
|
116
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `
|
139
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rails test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
117
140
|
|
118
141
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
119
142
|
|
data/lib/oaken/rspec_setup.rb
CHANGED
@@ -1,9 +1,8 @@
|
|
1
1
|
RSpec.configure do |config|
|
2
|
-
config.include
|
3
|
-
|
2
|
+
config.include Oaken::Seeds
|
4
3
|
config.use_transactional_fixtures = true
|
5
4
|
|
6
|
-
config.before
|
5
|
+
config.before :suite do
|
7
6
|
# Mimic fixtures by truncating before inserting.
|
8
7
|
ActiveRecord::Tasks::DatabaseTasks.truncate_all
|
9
8
|
Oaken.load_seed
|
data/lib/oaken/seeds.rb
CHANGED
@@ -1,16 +1,43 @@
|
|
1
1
|
module Oaken::Seeds
|
2
2
|
extend self
|
3
3
|
|
4
|
-
|
5
|
-
|
4
|
+
# Allow assigning defaults across different types.
|
5
|
+
def self.defaults(**defaults) = attributes.merge!(**defaults)
|
6
|
+
def self.defaults_for(*keys) = attributes.slice(*keys)
|
7
|
+
def self.attributes = @attributes ||= {}.with_indifferent_access
|
8
|
+
|
9
|
+
# Oaken's main auto-registering logic.
|
10
|
+
#
|
11
|
+
# So when you first call e.g. `accounts.create`, we'll hit `method_missing` here
|
12
|
+
# and automatically call `register Account`.
|
13
|
+
#
|
14
|
+
# We'll also match partial and full nested namespaces like in this order:
|
15
|
+
#
|
16
|
+
# accounts => Account
|
17
|
+
# account_jobs => AccountJob | Account::Job
|
18
|
+
# account_job_tasks => AccountJobTask | Account::JobTask | Account::Job::Task
|
19
|
+
#
|
20
|
+
# If you have classes that don't follow this naming convention, you must call `register` manually.
|
21
|
+
def self.method_missing(meth, ...)
|
22
|
+
name = meth.to_s.classify
|
23
|
+
name = name.sub!(/(?<=[a-z])(?=[A-Z])/, "::") until name.nil? or type = name.safe_constantize
|
24
|
+
|
25
|
+
if type
|
6
26
|
register type
|
7
|
-
public_send(
|
27
|
+
public_send(meth, ...)
|
8
28
|
else
|
9
29
|
super
|
10
30
|
end
|
11
31
|
end
|
12
32
|
def self.respond_to_missing?(name, ...) = name.to_s.classify.safe_constantize || super
|
13
33
|
|
34
|
+
# Register a model class to be accessible as an instance method via `include Oaken::Seeds`.
|
35
|
+
# Note: Oaken's auto-register via `method_missing` means it's less likely you need to call this manually.
|
36
|
+
#
|
37
|
+
# register Account, Account::Job, Account::Job::Task
|
38
|
+
#
|
39
|
+
# Oaken uses the `table_name` of the passed classes for the method names, e.g. here they'd be
|
40
|
+
# `accounts`, `account_jobs`, and `account_job_tasks`, respectively.
|
14
41
|
def self.register(*types)
|
15
42
|
types.each do |type|
|
16
43
|
stored = provider.new(type) and define_method(stored.key) { stored }
|
@@ -1,7 +1,7 @@
|
|
1
1
|
class Oaken::Stored::ActiveRecord
|
2
2
|
def initialize(type)
|
3
3
|
@type, @key = type, type.table_name
|
4
|
-
@attributes =
|
4
|
+
@attributes = Oaken::Seeds.defaults_for(*type.column_names)
|
5
5
|
end
|
6
6
|
attr_reader :type, :key
|
7
7
|
delegate :transaction, to: :type # For multi-db setups to help open a transaction on secondary connections.
|
data/lib/oaken/version.rb
CHANGED
data/lib/oaken.rb
CHANGED
@@ -18,17 +18,12 @@ module Oaken
|
|
18
18
|
|
19
19
|
class Loader
|
20
20
|
def initialize(path)
|
21
|
-
@entries = Pathname.glob("#{path}{,/**/*}.
|
21
|
+
@entries = Pathname.glob("#{path}{,/**/*}.rb").sort
|
22
22
|
end
|
23
23
|
|
24
|
-
def load_onto(seeds)
|
25
|
-
|
26
|
-
|
27
|
-
case path.extname
|
28
|
-
when ".rb" then seeds.class_eval path.read, path.to_s
|
29
|
-
when ".sql" then ActiveRecord::Base.connection.execute path.read
|
30
|
-
end
|
31
|
-
end
|
24
|
+
def load_onto(seeds) = @entries.each do |path|
|
25
|
+
ActiveRecord::Base.transaction do
|
26
|
+
seeds.class_eval path.read, path.to_s
|
32
27
|
end
|
33
28
|
end
|
34
29
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: oaken
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kasper Timm Hansen
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-06-30 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|
@@ -53,7 +53,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
55
|
requirements: []
|
56
|
-
rubygems_version: 3.5.
|
56
|
+
rubygems_version: 3.5.10
|
57
57
|
signing_key:
|
58
58
|
specification_version: 4
|
59
59
|
summary: Oaken aims to blend your Fixtures/Factories and levels up your database seeds.
|