sprig-reap 0.0.6 → 0.0.7
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +48 -6
- data/lib/sprig/reap.rb +13 -3
- data/lib/sprig/reap/configuration.rb +12 -0
- data/lib/sprig/reap/logging.rb +35 -0
- data/lib/sprig/reap/model.rb +7 -0
- data/lib/sprig/reap/record.rb +7 -1
- data/lib/sprig/reap/seed_file.rb +6 -0
- data/lib/sprig/reap/version.rb +1 -1
- data/spec/db/activerecord.db +0 -0
- data/spec/lib/sprig/reap/configuration_spec.rb +35 -1
- data/spec/lib/sprig/reap/model_spec.rb +36 -0
- data/spec/lib/sprig/reap/record_spec.rb +14 -2
- data/spec/lib/sprig/reap/seed_file_spec.rb +18 -0
- data/spec/lib/sprig/reap_spec.rb +23 -0
- data/spec/spec_helper.rb +2 -0
- data/spec/support/colored_text.rb +17 -0
- data/spec/support/logger_mock.rb +21 -0
- metadata +7 -2
data/README.md
CHANGED
@@ -35,17 +35,59 @@ after the STI base model. STI sub-type records will all be written to that file
|
|
35
35
|
|
36
36
|
### Additional Configuration
|
37
37
|
|
38
|
-
Don't like the defaults when reaping Sprig::Reap records?
|
39
|
-
(`db/seeds` target folder), models (`ActiveRecord::Base.subclasses`-only) you want seed files for,
|
40
|
-
or any ignored attributes you don't want to show up in any of the seed files.
|
38
|
+
Don't like the defaults when reaping Sprig::Reap records? Change 'em!
|
41
39
|
|
40
|
+
#### Target Environment
|
41
|
+
You may specify the target environment (`db/seeds` target folder):
|
42
42
|
```
|
43
43
|
# Rake Task
|
44
|
-
rake db:seed:reap TARGET_ENV=integration
|
44
|
+
rake db:seed:reap TARGET_ENV=integration
|
45
45
|
|
46
46
|
# Rails Console
|
47
|
-
Sprig.reap(target_env: 'integration'
|
48
|
-
|
47
|
+
Sprig.reap(target_env: 'integration')
|
48
|
+
```
|
49
|
+
|
50
|
+
#### Model List
|
51
|
+
If you only want to `reap` a subset of your models, you may provide a list of models
|
52
|
+
(`ActiveRecord::Base.subclasses`-only) you want seed files for:
|
53
|
+
```
|
54
|
+
# Rake Task
|
55
|
+
rake db:seed:reap MODELS=User,Post
|
56
|
+
|
57
|
+
# Rails Console
|
58
|
+
Sprig.reap(models: [User, Post])
|
59
|
+
```
|
60
|
+
|
61
|
+
#### Ignored Attributes
|
62
|
+
If there are any ignored attributes you don't want to show up in any of the seed files, let `reap`
|
63
|
+
know:
|
64
|
+
```
|
65
|
+
# Rake Task
|
66
|
+
rake db:seed:reap IGNORED_ATTRS=created_at,updated_at
|
67
|
+
|
68
|
+
# Rails Console
|
69
|
+
Sprig.reap(ignored_attrs: [:created_at, :updated_at])
|
70
|
+
```
|
71
|
+
|
72
|
+
#### Omitting Empty Attributes
|
73
|
+
If you have models with lots of attributes that could potentially be `nil`/empty, the resulting seed
|
74
|
+
files could get cluttered with all the `nil` values. Remove them from your seed files with:
|
75
|
+
```
|
76
|
+
# Rake Task
|
77
|
+
rake db:seed:reap OMIT_EMPTY_ATTRS=true
|
78
|
+
|
79
|
+
# Rails Console
|
80
|
+
Sprig.reap(omit_empty_attrs: true)
|
81
|
+
```
|
82
|
+
|
83
|
+
#### Combine Them All!
|
84
|
+
You're free to take or leave as many options as you'd like:
|
85
|
+
```
|
86
|
+
# Rake Task
|
87
|
+
rake db:seed:reap TARGET_ENV=integration MODELS=User,Post IGNORED_ATTRS=created_at,updated_at OMIT_EMPTY_ATTRS=true
|
88
|
+
|
89
|
+
# Rails Console
|
90
|
+
Sprig.reap(target_env: 'integration', models: [User, Post], ignored_attrs: [:created_at, :updated_at], omit_empty_attrs: true)
|
49
91
|
```
|
50
92
|
|
51
93
|
### Adding to Existing Seed Files (`.yaml` only)
|
data/lib/sprig/reap.rb
CHANGED
@@ -9,18 +9,26 @@ module Sprig::Reap
|
|
9
9
|
autoload :Record, 'sprig/reap/record'
|
10
10
|
autoload :SeedFile, 'sprig/reap/seed_file'
|
11
11
|
autoload :FileAttribute, 'sprig/reap/file_attribute'
|
12
|
+
autoload :Logging, 'sprig/reap/logging'
|
13
|
+
|
14
|
+
extend Logging
|
12
15
|
|
13
16
|
class << self
|
14
17
|
def reap(input = {})
|
15
18
|
options = input.to_hash
|
16
19
|
|
17
20
|
configure do |config|
|
18
|
-
config.target_env
|
19
|
-
config.classes
|
20
|
-
config.ignored_attrs
|
21
|
+
config.target_env = options[:target_env] || options['TARGET_ENV']
|
22
|
+
config.classes = options[:models] || options['MODELS']
|
23
|
+
config.ignored_attrs = options[:ignored_attrs] || options['IGNORED_ATTRS']
|
24
|
+
config.omit_empty_attrs = options[:omit_empty_attrs] || options['OMIT_EMPTY_ATTRS']
|
21
25
|
end
|
22
26
|
|
27
|
+
log_debug "Reaping records from the database...\r"
|
28
|
+
|
23
29
|
Model.all.each { |model| SeedFile.new(model).write }
|
30
|
+
|
31
|
+
log_debug "Finished reaping!"
|
24
32
|
end
|
25
33
|
|
26
34
|
def clear_config
|
@@ -34,6 +42,8 @@ module Sprig::Reap
|
|
34
42
|
delegate :target_env,
|
35
43
|
:classes,
|
36
44
|
:ignored_attrs,
|
45
|
+
:logger,
|
46
|
+
:omit_empty_attrs,
|
37
47
|
to: :configuration
|
38
48
|
|
39
49
|
def configuration
|
@@ -29,6 +29,18 @@ module Sprig::Reap
|
|
29
29
|
@ignored_attrs = parse_ignored_attrs_from(input)
|
30
30
|
end
|
31
31
|
|
32
|
+
def logger
|
33
|
+
@logger ||= Logger.new($stdout)
|
34
|
+
end
|
35
|
+
|
36
|
+
def omit_empty_attrs
|
37
|
+
@omit_empty_attrs ||= false
|
38
|
+
end
|
39
|
+
|
40
|
+
def omit_empty_attrs=(input)
|
41
|
+
@omit_empty_attrs = true if String(input).strip.downcase == 'true'
|
42
|
+
end
|
43
|
+
|
32
44
|
private
|
33
45
|
|
34
46
|
def valid_classes
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Sprig::Reap
|
2
|
+
module Logging
|
3
|
+
LOG_LEVELS = {
|
4
|
+
debug: :blue,
|
5
|
+
info: :green,
|
6
|
+
warn: :orange,
|
7
|
+
error: :red
|
8
|
+
}
|
9
|
+
|
10
|
+
LOG_COLORS = {
|
11
|
+
blue: 34,
|
12
|
+
green: 32,
|
13
|
+
orange: 33,
|
14
|
+
red: 31
|
15
|
+
}
|
16
|
+
|
17
|
+
LOG_LEVELS.each do |level, color|
|
18
|
+
define_method("log_#{level}") do |message|
|
19
|
+
Sprig::Reap.logger.send(level, send(color, message.to_s))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def colorize(message, color_code)
|
26
|
+
"\e[#{color_code}m#{message}\e[0m"
|
27
|
+
end
|
28
|
+
|
29
|
+
LOG_COLORS.each do |name, color_code|
|
30
|
+
define_method(name) do |message|
|
31
|
+
colorize(message, color_code)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/sprig/reap/model.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Sprig::Reap
|
2
2
|
class Model
|
3
|
+
include Logging
|
4
|
+
|
3
5
|
def self.all
|
4
6
|
@@all ||= begin
|
5
7
|
models = Sprig::Reap.classes.map { |klass| new(klass) }
|
@@ -52,6 +54,8 @@ module Sprig::Reap
|
|
52
54
|
end
|
53
55
|
|
54
56
|
def to_yaml(options = {})
|
57
|
+
return if records.empty?
|
58
|
+
|
55
59
|
namespace = options[:namespace]
|
56
60
|
formatted_records = records.map(&:to_hash)
|
57
61
|
|
@@ -66,6 +70,9 @@ module Sprig::Reap
|
|
66
70
|
|
67
71
|
def records
|
68
72
|
@records ||= klass.all.map { |record| Record.new(record, self) }
|
73
|
+
rescue
|
74
|
+
log_error "Encountered an error when pulling the database records for #{to_s}...\r"
|
75
|
+
[]
|
69
76
|
end
|
70
77
|
|
71
78
|
private
|
data/lib/sprig/reap/record.rb
CHANGED
@@ -16,7 +16,13 @@ module Sprig::Reap
|
|
16
16
|
|
17
17
|
def to_hash
|
18
18
|
attributes.reduce({"sprig_id" => sprig_id}) do |hash, attr|
|
19
|
-
|
19
|
+
value = get_value_for(attr)
|
20
|
+
|
21
|
+
if Sprig::Reap.omit_empty_attrs && value.nil?
|
22
|
+
hash
|
23
|
+
else
|
24
|
+
hash.merge(attr => value)
|
25
|
+
end
|
20
26
|
end
|
21
27
|
end
|
22
28
|
|
data/lib/sprig/reap/seed_file.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
module Sprig::Reap
|
2
2
|
class SeedFile
|
3
|
+
include Logging
|
4
|
+
|
3
5
|
DEFAULT_NAMESPACE = 'records'
|
4
6
|
|
5
7
|
attr_reader :model
|
@@ -21,6 +23,7 @@ module Sprig::Reap
|
|
21
23
|
def write
|
22
24
|
initialize_file do |file, namespace|
|
23
25
|
file.write model.to_yaml(:namespace => namespace)
|
26
|
+
log_info "Successfully reaped records for #{model}...\r"
|
24
27
|
end
|
25
28
|
end
|
26
29
|
|
@@ -41,6 +44,9 @@ module Sprig::Reap
|
|
41
44
|
|
42
45
|
yield file, namespace
|
43
46
|
end
|
47
|
+
|
48
|
+
rescue
|
49
|
+
log_error "There was an issue writing to the file for #{model}...\r"
|
44
50
|
end
|
45
51
|
|
46
52
|
def existing_sprig_ids(yaml)
|
data/lib/sprig/reap/version.rb
CHANGED
data/spec/db/activerecord.db
CHANGED
Binary file
|
@@ -127,7 +127,7 @@ describe Sprig::Reap::Configuration do
|
|
127
127
|
|
128
128
|
describe "#ignored_attrs=" do
|
129
129
|
context "when given nil" do
|
130
|
-
before { subject.
|
130
|
+
before { subject.ignored_attrs = nil }
|
131
131
|
|
132
132
|
its(:ignored_attrs) { should == [] }
|
133
133
|
end
|
@@ -144,4 +144,38 @@ describe Sprig::Reap::Configuration do
|
|
144
144
|
its(:ignored_attrs) { should == ['shaka', 'laka'] }
|
145
145
|
end
|
146
146
|
end
|
147
|
+
|
148
|
+
<<<<<<< HEAD
|
149
|
+
describe "#logger" do
|
150
|
+
it "initializes a new logger" do
|
151
|
+
Logger.should_receive(:new)
|
152
|
+
|
153
|
+
subject.logger
|
154
|
+
=======
|
155
|
+
describe "#omit_empty_attrs" do
|
156
|
+
context "from a fresh configuration" do
|
157
|
+
its(:omit_empty_attrs) { should == false }
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
describe "#omit_empty_attrs" do
|
162
|
+
context "when given nil" do
|
163
|
+
before { subject.omit_empty_attrs = nil }
|
164
|
+
|
165
|
+
its(:omit_empty_attrs) { should == false }
|
166
|
+
end
|
167
|
+
|
168
|
+
context "when given a word other than true" do
|
169
|
+
before { subject.omit_empty_attrs = ' Shaboosh' }
|
170
|
+
|
171
|
+
its(:omit_empty_attrs) { should == false }
|
172
|
+
end
|
173
|
+
|
174
|
+
context "when given true" do
|
175
|
+
before { subject.omit_empty_attrs = 'True' }
|
176
|
+
|
177
|
+
its(:omit_empty_attrs) { should == true }
|
178
|
+
>>>>>>> Add an option to allow users to omit empty attributes from seed files (fix #9)
|
179
|
+
end
|
180
|
+
end
|
147
181
|
end
|
@@ -168,6 +168,42 @@ describe Sprig::Reap::Model do
|
|
168
168
|
subject.to_yaml.should == yaml_from_file('polymorphic_vote_record.yml')
|
169
169
|
end
|
170
170
|
end
|
171
|
+
|
172
|
+
context "when there are no records for a given model" do
|
173
|
+
subject { described_class.new(Vote) }
|
174
|
+
|
175
|
+
its(:to_yaml) { should == nil }
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
describe "#records" do
|
180
|
+
let!(:post1) { Post.create }
|
181
|
+
let!(:post2) { Post.create }
|
182
|
+
let(:record) { double('Sprig::Reap::Record') }
|
183
|
+
|
184
|
+
subject { described_class.new(Post) }
|
185
|
+
|
186
|
+
before do
|
187
|
+
Sprig::Reap::Record.stub(:new).and_return(record)
|
188
|
+
end
|
189
|
+
|
190
|
+
its(:records) { should == [record, record] }
|
191
|
+
|
192
|
+
context "when there's an error accessing the database table for a given model" do
|
193
|
+
before do
|
194
|
+
Post.stub(:all).and_raise(StandardError)
|
195
|
+
end
|
196
|
+
|
197
|
+
it "logs an error message" do
|
198
|
+
log_should_receive :error, :with => "Encountered an error when pulling the database records for Post...\r"
|
199
|
+
|
200
|
+
subject.records
|
201
|
+
end
|
202
|
+
|
203
|
+
it "sets records to an empty array" do
|
204
|
+
subject.records.should == []
|
205
|
+
end
|
206
|
+
end
|
171
207
|
end
|
172
208
|
|
173
209
|
def yaml_from_file(basename)
|
@@ -100,9 +100,21 @@ describe Sprig::Reap::Record do
|
|
100
100
|
}
|
101
101
|
end
|
102
102
|
end
|
103
|
-
end
|
104
103
|
|
105
|
-
|
104
|
+
context "when Sprig::Reap is configured to omit empty attributes" do
|
105
|
+
subject { described_class.new(posterless_post, model) }
|
106
|
+
|
107
|
+
before { Sprig::Reap.stub(:omit_empty_attrs).and_return(true) }
|
108
|
+
|
109
|
+
it "removes empty attributes from the resulting hash" do
|
110
|
+
subject.to_hash.should == {
|
111
|
+
'sprig_id' => posterless_post.id,
|
112
|
+
'title' => 'Wow Title',
|
113
|
+
'content' => 'Much Content',
|
114
|
+
'published' => false
|
115
|
+
}
|
116
|
+
end
|
117
|
+
end
|
106
118
|
end
|
107
119
|
|
108
120
|
describe "#sprig_id" do
|
@@ -59,6 +59,12 @@ describe Sprig::Reap::SeedFile do
|
|
59
59
|
setup_seed_folder('./spec/fixtures/db/seeds/dreamland', &example)
|
60
60
|
end
|
61
61
|
|
62
|
+
it "logs the successful reap of database records for the given model" do
|
63
|
+
log_should_receive :info, :with => "Successfully reaped records for #{model}...\r"
|
64
|
+
|
65
|
+
subject.write
|
66
|
+
end
|
67
|
+
|
62
68
|
context "when the seed file already exists" do
|
63
69
|
before do
|
64
70
|
yaml = File.read('./spec/fixtures/yaml/comment_seeds.yml')
|
@@ -125,5 +131,17 @@ describe Sprig::Reap::SeedFile do
|
|
125
131
|
File.size?(subject.path).should > 0
|
126
132
|
end
|
127
133
|
end
|
134
|
+
|
135
|
+
context "when there are errors writing to the file" do
|
136
|
+
before do
|
137
|
+
File.stub(:open).and_raise(StandardError)
|
138
|
+
end
|
139
|
+
|
140
|
+
it "logs an error for the given model" do
|
141
|
+
log_should_receive :error, :with => "There was an issue writing to the file for Comment...\r"
|
142
|
+
|
143
|
+
subject.write
|
144
|
+
end
|
145
|
+
end
|
128
146
|
end
|
129
147
|
end
|
data/spec/lib/sprig/reap_spec.rb
CHANGED
@@ -18,6 +18,13 @@ describe Sprig::Reap do
|
|
18
18
|
subject.clear_config
|
19
19
|
end
|
20
20
|
|
21
|
+
it "outputs log messages when starting and on completion" do
|
22
|
+
log_should_receive :debug, :with => "Reaping records from the database...\r"
|
23
|
+
log_should_receive :debug, :with => "Finished reaping!"
|
24
|
+
|
25
|
+
subject.reap
|
26
|
+
end
|
27
|
+
|
21
28
|
it "generates a seed file for each class" do
|
22
29
|
count = Sprig::Reap.classes.count
|
23
30
|
|
@@ -73,6 +80,22 @@ describe Sprig::Reap do
|
|
73
80
|
end
|
74
81
|
end
|
75
82
|
end
|
83
|
+
|
84
|
+
context "when passed a value for omitting empty attributes in the options hash" do
|
85
|
+
context "in :omit_empty_attrs" do
|
86
|
+
it "sets the flag to omit empty attributes" do
|
87
|
+
subject.reap(:omit_empty_attrs => true)
|
88
|
+
subject.omit_empty_attrs.should == true
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
context "in OMIT_EMPTY_ATTRS" do
|
93
|
+
it "sets the flag to omit empty attributes" do
|
94
|
+
subject.reap('OMIT_EMPTY_ATTRS' => ' TRUE ')
|
95
|
+
subject.omit_empty_attrs.should == true
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
76
99
|
end
|
77
100
|
|
78
101
|
describe ".clear_config" do
|
data/spec/spec_helper.rb
CHANGED
@@ -0,0 +1,21 @@
|
|
1
|
+
module LoggerMock
|
2
|
+
def log_should_receive(level, options)
|
3
|
+
Sprig::Reap.logger.should_receive(level).with(send("log_#{level}_text", options.fetch(:with)))
|
4
|
+
end
|
5
|
+
|
6
|
+
def log_debug_text(text)
|
7
|
+
blue_text(text)
|
8
|
+
end
|
9
|
+
|
10
|
+
def log_info_text(text)
|
11
|
+
green_text(text)
|
12
|
+
end
|
13
|
+
|
14
|
+
def log_warn_text(text)
|
15
|
+
orange_text(text)
|
16
|
+
end
|
17
|
+
|
18
|
+
def log_error_text(text)
|
19
|
+
red_text(text)
|
20
|
+
end
|
21
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sprig-reap
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2014-
|
12
|
+
date: 2014-08-15 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -134,6 +134,7 @@ files:
|
|
134
134
|
- lib/sprig/reap/association.rb
|
135
135
|
- lib/sprig/reap/configuration.rb
|
136
136
|
- lib/sprig/reap/file_attribute.rb
|
137
|
+
- lib/sprig/reap/logging.rb
|
137
138
|
- lib/sprig/reap/model.rb
|
138
139
|
- lib/sprig/reap/railtie.rb
|
139
140
|
- lib/sprig/reap/record.rb
|
@@ -168,7 +169,9 @@ files:
|
|
168
169
|
- spec/lib/sprig/reap_spec.rb
|
169
170
|
- spec/lib/sprig_spec.rb
|
170
171
|
- spec/spec_helper.rb
|
172
|
+
- spec/support/colored_text.rb
|
171
173
|
- spec/support/file_setup.rb
|
174
|
+
- spec/support/logger_mock.rb
|
172
175
|
- spec/support/matchers/be_same_file_as.rb
|
173
176
|
- spec/support/rails_stubs.rb
|
174
177
|
homepage: http://www.github.com/vigetlabs/sprig-reap
|
@@ -219,6 +222,8 @@ test_files:
|
|
219
222
|
- spec/lib/sprig/reap_spec.rb
|
220
223
|
- spec/lib/sprig_spec.rb
|
221
224
|
- spec/spec_helper.rb
|
225
|
+
- spec/support/colored_text.rb
|
222
226
|
- spec/support/file_setup.rb
|
227
|
+
- spec/support/logger_mock.rb
|
223
228
|
- spec/support/matchers/be_same_file_as.rb
|
224
229
|
- spec/support/rails_stubs.rb
|