sprig-reap 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d9274dc514b03b7804de43aba385e8cc68d954af
4
- data.tar.gz: f9c7b62a38c72b9745ef5a982b7ae26fc43ced61
3
+ metadata.gz: aefd592ca49ff880defe79c87d202f9309d19155
4
+ data.tar.gz: 87c0a8f35ae8fc00915194443bc6e63d6d2b41f0
5
5
  SHA512:
6
- metadata.gz: d0f68d527c8ae07b07a1fb7b6e3f2874343f3b62f4f340383a2dd90c354914d13a821ce2898c6cc364cf8b3b7b4e805b208104465198498cb5e3ee97b750ef6c
7
- data.tar.gz: 50f0b95641ecf1de92a336f302b6bd33657e7f7f15db17527ef888aaf7fd28f031a8d63ca60bbea01ae37fbf8eb7a3f33c2e1eb319bc454f1ba589fdfbe8a6a1
6
+ metadata.gz: a8f4110f827135c3674aff16b1b621db501bbd945c20619ec109ce2ccd3c3692462731e027961a578bd14e8309ae2db0059b189bc154f719bbb7cfe8b31022f3
7
+ data.tar.gz: 5dfb7babbff4b81964232a81110ad19a0f52b231ad1b69d3914f469f9d82e33a23f70920b6a4b65b94755389559eb953fd54150e97b0f94c26fe92f9dd0074d2
data/README.md CHANGED
@@ -1,10 +1,21 @@
1
1
  # Sprig::Reap
2
2
 
3
- Automatic capture and output of your application's data state to Sprig-formatted seed files.
3
+ [![Code Climate](https://codeclimate.com/github/vigetlabs/sprig-reap.png)](https://codeclimate.com/github/vigetlabs/sprig-reap) [![Build Status](https://travis-ci.org/vigetlabs/sprig-reap.png?branch=master)](https://travis-ci.org/vigetlabs/sprig-reap) [![Gem Version](https://badge.fury.io/rb/sprig-reap.png)](http://badge.fury.io/rb/sprig-reap)
4
4
 
5
- ## Populate Seed Files from Database
5
+ Don't want to write Sprig seed files from scratch? No problem! Sprig::Reap can create them for
6
+ you. Sprig::Reap enables automatic capture and output of your application's data state to
7
+ Sprig-formatted seed files.
6
8
 
7
- Don't want to write Sprig seed files from scratch? Well, Sprig::Reap can create them for you!
9
+ ## Installation
10
+ ```
11
+ # Command Line
12
+ gem install sprig-reap
13
+
14
+ # Gemfile
15
+ gem 'sprig-reap'
16
+ ```
17
+
18
+ ## Usage
8
19
 
9
20
  Via a rake task:
10
21
  ```
@@ -24,15 +35,17 @@ after the STI base model. STI sub-type records will all be written to that file
24
35
 
25
36
  ### Additional Configuration
26
37
 
27
- Don't like the defaults when reaping Sprig::Reap records? You may specify the environment (`db/seeds`
28
- target folder) or models (`ActiveRecord::Base.subclasses`-only) you want seed files for.
38
+ Don't like the defaults when reaping Sprig::Reap records? You may specify the target environment
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.
29
41
 
30
42
  ```
31
43
  # Rake Task
32
- rake db:seed:reap ENV=integration MODELS=User,Post
44
+ rake db:seed:reap TARGET_ENV=integration MODELS=User,Post IGNORED_ATTRS=created_at,updated_at
33
45
 
34
46
  # Rails Console
35
- Sprig::Reap.reap(env: 'integration', models: [User, Post])
47
+ Sprig::Reap.reap(target_env: 'integration', models: [User, Post], ignored_attrs: [:created_at,
48
+ :updated_at])
36
49
  ```
37
50
 
38
51
  ### Adding to Existing Seed Files (`.yaml` only)
@@ -2,13 +2,13 @@ module Sprig::Reap
2
2
  class Configuration
3
3
  VALID_CLASSES = ActiveRecord::Base.subclasses
4
4
 
5
- def env
6
- @env ||= Rails.env
5
+ def target_env
6
+ @target_env ||= Rails.env
7
7
  end
8
8
 
9
- def env=(given_env)
10
- parse_valid_env_from given_env do |environment|
11
- @env = environment
9
+ def target_env=(given_env)
10
+ parse_valid_env_from given_env do |target_environment|
11
+ @target_env = target_environment
12
12
  end
13
13
  end
14
14
 
@@ -22,17 +22,25 @@ module Sprig::Reap
22
22
  end
23
23
  end
24
24
 
25
+ def ignored_attrs
26
+ @ignored_attrs ||= []
27
+ end
28
+
29
+ def ignored_attrs=(input)
30
+ @ignored_attrs = parse_ignored_attrs_from(input)
31
+ end
32
+
25
33
  private
26
34
 
27
35
  def parse_valid_env_from(input)
28
36
  return if input.nil?
29
- environment = input.strip.downcase
30
- create_seeds_folder(environment)
31
- yield environment
37
+ target_environment = input.strip.downcase
38
+ create_seeds_folder(target_environment)
39
+ yield target_environment
32
40
  end
33
41
 
34
- def create_seeds_folder(env)
35
- folder = Rails.root.join('db', 'seeds', env)
42
+ def create_seeds_folder(target_env)
43
+ folder = Rails.root.join('db', 'seeds', target_env)
36
44
  FileUtils.mkdir_p(folder) unless File.directory? folder
37
45
  end
38
46
 
@@ -57,5 +65,15 @@ module Sprig::Reap
57
65
  end
58
66
  end
59
67
  end
68
+
69
+ def parse_ignored_attrs_from(input)
70
+ return if input.nil?
71
+
72
+ if input.is_a? String
73
+ input.split(',').map(&:strip)
74
+ else
75
+ input.map(&:to_s).map(&:strip)
76
+ end
77
+ end
60
78
  end
61
79
  end
@@ -22,7 +22,7 @@ module Sprig::Reap
22
22
  end
23
23
 
24
24
  def attributes
25
- klass.column_names
25
+ klass.column_names - Sprig::Reap.ignored_attrs
26
26
  end
27
27
 
28
28
  def dependencies
@@ -11,7 +11,7 @@ module Sprig::Reap
11
11
  end
12
12
 
13
13
  def path
14
- Rails.root.join('db', 'seeds', Sprig::Reap.env, "#{model.to_s.tableize.gsub('/', '_')}.yml")
14
+ Rails.root.join('db', 'seeds', Sprig::Reap.target_env, "#{model.to_s.tableize.gsub('/', '_')}.yml")
15
15
  end
16
16
 
17
17
  def exists?
@@ -1,5 +1,5 @@
1
1
  module Sprig
2
2
  module Reap
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/lib/sprig/reap.rb CHANGED
@@ -11,8 +11,9 @@ module Sprig::Reap
11
11
  class << self
12
12
  def reap(options = {})
13
13
  configure do |config|
14
- config.env = options[:env] || options['ENV']
15
- config.classes = options[:models] || options['MODELS']
14
+ config.target_env = options[:target_env] || options['TARGET_ENV']
15
+ config.classes = options[:models] || options['MODELS']
16
+ config.ignored_attrs = options[:ignored_attrs] || options['IGNORED_ATTRS']
16
17
  end
17
18
 
18
19
  Model.all.each { |model| SeedFile.new(model).write }
@@ -22,7 +23,10 @@ module Sprig::Reap
22
23
 
23
24
  cattr_reader :configuration
24
25
 
25
- delegate :env, :classes, to: :configuration
26
+ delegate :target_env,
27
+ :classes,
28
+ :ignored_attrs,
29
+ to: :configuration
26
30
 
27
31
  def configuration
28
32
  @@configuration ||= Configuration.new
Binary file
@@ -7,28 +7,28 @@ describe Sprig::Reap::Configuration do
7
7
  stub_rails_root
8
8
  end
9
9
 
10
- describe "#env" do
10
+ describe "#target_env" do
11
11
  context "from a fresh configuration" do
12
- its(:env) { should == Rails.env }
12
+ its(:target_env) { should == Rails.env }
13
13
  end
14
14
  end
15
15
 
16
- describe "#env=" do
16
+ describe "#target_env=" do
17
17
  context "when given nil" do
18
- it "does not change the env" do
19
- subject.env = nil
18
+ it "does not change the target_env" do
19
+ subject.target_env = nil
20
20
 
21
- subject.env.should_not == nil
21
+ subject.target_env.should_not == nil
22
22
  end
23
23
  end
24
24
 
25
25
  context "given a non-nil value" do
26
26
  let(:input) { ' ShaBOOSH' }
27
27
 
28
- it "formats the given value and then sets the environment" do
29
- subject.env = input
28
+ it "formats the given value and then sets the target environment" do
29
+ subject.target_env = input
30
30
 
31
- subject.env.should == 'shaboosh'
31
+ subject.target_env.should == 'shaboosh'
32
32
  end
33
33
 
34
34
  context "and the corresponding seeds folder does not yet exist" do
@@ -37,7 +37,7 @@ describe Sprig::Reap::Configuration do
37
37
  end
38
38
 
39
39
  it "creates the seeds folder" do
40
- subject.env = input
40
+ subject.target_env = input
41
41
 
42
42
  File.directory?('./spec/fixtures/db/seeds/shaboosh').should == true
43
43
  end
@@ -96,4 +96,30 @@ describe Sprig::Reap::Configuration do
96
96
  end
97
97
  end
98
98
  end
99
+
100
+ describe "#ignored_attrs" do
101
+ context "from a fresh configuration" do
102
+ its(:ignored_attrs) { should == [] }
103
+ end
104
+ end
105
+
106
+ describe "#ignored_attrs=" do
107
+ context "when given nil" do
108
+ before { subject.classes = nil }
109
+
110
+ its(:ignored_attrs) { should == [] }
111
+ end
112
+
113
+ context "when given an array of ignored_attrs" do
114
+ before { subject.ignored_attrs = [:shaka, ' laka '] }
115
+
116
+ its(:ignored_attrs) { should == ['shaka', 'laka'] }
117
+ end
118
+
119
+ context "when given a string" do
120
+ before { subject.ignored_attrs = ' shaka, laka' }
121
+
122
+ its(:ignored_attrs) { should == ['shaka', 'laka'] }
123
+ end
124
+ end
99
125
  end
@@ -39,9 +39,25 @@ describe Sprig::Reap::Model do
39
39
  end
40
40
 
41
41
  describe "#attributes" do
42
+ let(:attrs) { %w(boom shaka laka) }
43
+
42
44
  subject { described_class.new(User) }
43
45
 
44
- its(:attributes) { should == User.column_names }
46
+ before do
47
+ User.stub(:column_names).and_return(attrs)
48
+ end
49
+
50
+ context "when there are no ignored attributes" do
51
+ its(:attributes) { should == attrs }
52
+ end
53
+
54
+ context "when there are ignored attributes" do
55
+ before do
56
+ Sprig::Reap.stub(:ignored_attrs).and_return(['laka'])
57
+ end
58
+
59
+ its(:attributes) { should == ['boom', 'shaka'] }
60
+ end
45
61
  end
46
62
 
47
63
  describe "#dependencies" do
@@ -7,7 +7,7 @@ describe Sprig::Reap::SeedFile do
7
7
 
8
8
  before do
9
9
  stub_rails_root
10
- Sprig::Reap.stub(:env).and_return('dreamland')
10
+ Sprig::Reap.stub(:target_env).and_return('dreamland')
11
11
  end
12
12
 
13
13
  describe "#initialize" do
@@ -21,17 +21,17 @@ describe Sprig::Reap do
21
21
  end
22
22
 
23
23
  context "when passed an environment in the options hash" do
24
- context "in :env" do
24
+ context "in :target_env" do
25
25
  it "sets the environment" do
26
- subject.reap(:env => 'dreamland')
27
- subject.env.should == 'dreamland'
26
+ subject.reap(:target_env => 'dreamland')
27
+ subject.target_env.should == 'dreamland'
28
28
  end
29
29
  end
30
30
 
31
- context "in 'ENV'" do
31
+ context "in 'TARGET_ENV'" do
32
32
  it "sets the environment" do
33
33
  subject.reap('ENV' => ' Dreamland')
34
- subject.env.should == 'dreamland'
34
+ subject.target_env.should == 'dreamland'
35
35
  end
36
36
  end
37
37
  end
@@ -51,5 +51,21 @@ describe Sprig::Reap do
51
51
  end
52
52
  end
53
53
  end
54
+
55
+ context "when passed a list of ignored attributes in the options hash" do
56
+ context "in :ignored_attrs" do
57
+ it "sets ignored attributes" do
58
+ subject.reap(:ignored_attrs => [:willy, :nilly])
59
+ subject.ignored_attrs.should == ['willy', 'nilly']
60
+ end
61
+ end
62
+
63
+ context "in IGNORED_ATTRS" do
64
+ it "sets ignored attributes" do
65
+ subject.reap('IGNORED_ATTRS' => ' willy, nilly')
66
+ subject.ignored_attrs.should == ['willy', 'nilly']
67
+ end
68
+ end
69
+ end
54
70
  end
55
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sprig-reap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Stenberg
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-06-10 00:00:00.000000000 Z
11
+ date: 2014-06-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails