versioned_seeds 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --colour
data/Rakefile CHANGED
@@ -1,2 +1,6 @@
1
1
  #!/usr/bin/env rake
2
2
  require "bundler/gem_tasks"
3
+ require "rspec/core/rake_task"
4
+
5
+ RSpec::Core::RakeTask.new(:spec)
6
+ task :default => :spec
@@ -0,0 +1,22 @@
1
+ require 'rails/generators'
2
+
3
+ module VersionedSeeds
4
+ class SeedFileGenerator < Rails::Generators::NamedBase
5
+ # namespace 'vs'
6
+ source_root File.expand_path("../templates", __FILE__)
7
+
8
+ desc "Generates a seed file with a timestamped name"
9
+
10
+ def create_seed_file
11
+ # directory 'db/seeds'
12
+ template 'seed_file.rb', "db/seeds/#{script_name}.rb"
13
+ end
14
+
15
+ private
16
+
17
+ def script_name
18
+ ts = Time.now.utc.strftime("%Y%m%d%H%M%S")
19
+ "#{ts}_#{name}"
20
+ end
21
+ end
22
+ end
@@ -0,0 +1 @@
1
+ # Put your seeding script here
@@ -3,15 +3,15 @@ require 'versioned_seeds'
3
3
  namespace :vs do
4
4
  desc "Display current seeds version"
5
5
  task :status do
6
- puts "Last seeds: #{VersionedSeeds.current_version}"
6
+ puts "Last seeds: #{VersionedSeeds.last_loaded}"
7
7
  end
8
8
 
9
- desc "Task description"
9
+ desc "Load the next seeding script"
10
10
  task :next => :environment do
11
11
  VersionedSeeds.next
12
12
  end
13
13
 
14
- desc "Task description"
14
+ desc "Load all seeding scripts that haven't been loaded yet"
15
15
  task :all => :environment do
16
16
  VersionedSeeds.all
17
17
  end
@@ -4,51 +4,76 @@ require "versioned_seeds/version"
4
4
  module VersionedSeeds
5
5
  require "versioned_seeds/railtie" if defined?(Rails)
6
6
 
7
- class << self
8
- def next(version=current_version)
9
- load next_seeds(version)
7
+ module ClassMethods # :nodoc:
8
+ # Loads the next seeding script
9
+ def next(loaded=already_loaded)
10
+ load next_seed(loaded)
10
11
  end
11
12
 
12
- def all(version=current_version)
13
- load all_seeds(version)
13
+ # Loads all the seeding scripts that haven't been loaded yet
14
+ def all(loaded=already_loaded)
15
+ load all_seeds(loaded)
14
16
  end
15
17
 
18
+ # Returns the next seeding script to load
19
+ def next_seed(loaded=already_loaded)
20
+ all_seeds(loaded).first
21
+ end
22
+
23
+ # Returns all the seeding scripts to load
24
+ def all_seeds(loaded=already_loaded)
25
+ seeds.delete_if { |seed| loaded.include?(seed.version) }
26
+ end
27
+
28
+ # returns the last loaded seeding script
29
+ def last_loaded(loaded=already_loaded)
30
+ loaded.last
31
+ end
32
+
33
+ # Loads a list of seeding scripts and updates the .versioned_seeds file
16
34
  def load(seeds)
17
- last_version = seeds.inject(false) do |version, seed|
35
+ seeds = [*seeds]
36
+ loaded = seeds.inject([]) do |ary, seed|
18
37
  puts "Loading: #{File.basename seed.file}"
19
38
  require seed.file
20
- seed.version
39
+ ary << seed
40
+ ary
21
41
  end
22
- write_version last_version if last_version
23
- end
24
-
25
- def next_seeds(version=current_version)
26
- seeds.keep_if { |seed| seed.version == version.next }
27
- end
28
42
 
29
- def all_seeds(version=current_version)
30
- seeds.keep_if { |seed| seed.version > version }
43
+ write_loaded loaded
31
44
  end
32
45
 
46
+ # Returns the files located in the rails_root/db/seeds folder sorted by version
33
47
  def seeds
34
- Dir[Rails.root+'db/seeds/*.rb'].inject([]) { |list, file|
48
+ Dir[Rails.root + 'db/seeds/*.rb'].inject([]) { |list, file|
35
49
  filename = File.basename(file)
36
- version = filename.to_i
50
+ version = filename[/^(\d+)_/, 1]
51
+
52
+ next unless version
53
+
37
54
  list << OpenStruct.new(:file => file, :version => version)
38
55
  list
39
56
  }.sort { |seed1, seed2| seed1.version <=> seed2.version }
40
57
  end
41
58
 
42
- def write_version(version)
43
- File.open(Rails.root+'.versioned_seeds', 'w') do |f|
44
- f.puts version
59
+ # Writes the versions of the loaded script to the .versionned_seeds file
60
+ def write_loaded(loaded)
61
+ if loaded.any?
62
+ File.open(Rails.root + '.versioned_seeds', 'a') do |f|
63
+ loaded.each { |seed| f.puts seed.version }
64
+ end
65
+ else
66
+ puts "No seed has been loaded"
45
67
  end
46
68
  end
47
69
 
48
- def current_version
49
- file = Rails.root+'.versioned_seeds'
50
- return 0 unless File.exists?(file)
51
- File.read(file).to_i
70
+ # Returns the list of already loaded scripts
71
+ def already_loaded
72
+ file = Rails.root + '.versioned_seeds'
73
+ return [0] unless File.exists?(file)
74
+ File.read(file).split(/\r?\n/)
52
75
  end
53
76
  end
77
+
78
+ extend ClassMethods
54
79
  end
@@ -1,5 +1,5 @@
1
- require 'versioned_seeds'
2
1
  require 'rails'
2
+ require 'versioned_seeds'
3
3
 
4
4
  module VersionedSeeds
5
5
  class Railtie < Rails::Railtie
@@ -1,3 +1,3 @@
1
1
  module VersionedSeeds
2
- VERSION = "0.0.1"
2
+ VERSION = "0.1.0"
3
3
  end
@@ -0,0 +1,3 @@
1
+ 20111203000001
2
+ 20111203000002
3
+ 20111203000003
File without changes
File without changes
File without changes
@@ -0,0 +1,138 @@
1
+ require 'rails'
2
+ require 'ostruct'
3
+ require 'versioned_seeds'
4
+
5
+ $:.unshift File.join(File.dirname(__FILE__), 'rails_root', 'db', 'seeds')
6
+
7
+ module VersionedSeeds
8
+ class Tester
9
+ include ClassMethods
10
+ end
11
+
12
+ describe Tester do
13
+ def remove_seed_file
14
+ seed_file = Rails.root + '.versioned_seeds'
15
+ File.delete(seed_file) if File.exists?(seed_file)
16
+ seed_file
17
+ end
18
+
19
+ let(:rails_root) { Pathname.new(File.dirname __FILE__) + 'rails_root' }
20
+ let(:vs_folder) { rails_root + 'db/seeds' }
21
+ [1,2,3].each do |n|
22
+ let("file#{n}".to_sym) do
23
+ OpenStruct.new file: (vs_folder+"2011120300000#{n}_file#{n}.rb").to_s, version: "2011120300000#{n}"
24
+ end
25
+ end
26
+ let(:seed_files) { [ file1, file2, file3 ] }
27
+
28
+ before :each do
29
+ @tester = Tester.new
30
+ @tester.stub(:puts)
31
+ @tester.stub(:require)
32
+
33
+ ::Rails.stub(:root) { rails_root }
34
+ end
35
+
36
+ context do
37
+ before do
38
+ @tester.stub(:seeds).and_return(seed_files)
39
+ @tester.stub(:load)
40
+ end
41
+
42
+ describe 'next' do
43
+ it "should load the next seeding script" do
44
+ @tester.should_receive(:load).with(file2)
45
+ @tester.next([file1.version])
46
+ end
47
+ end
48
+
49
+ describe 'all' do
50
+ it "should load all the seeding scripts that haven't been loaded yet" do
51
+ @tester.should_receive(:load).with([file2, file3])
52
+ @tester.all([file1.version])
53
+ end
54
+ end
55
+
56
+ describe 'next_seed' do
57
+ it "should return the next seeding script to load" do
58
+ @tester.next_seed([file1.version]).should == file2
59
+ end
60
+ end
61
+
62
+ describe 'all_seeds' do
63
+ it "should return all the seeding scripts to load" do
64
+ @tester.all_seeds([file1.version]).should == [file2, file3]
65
+ end
66
+ end
67
+
68
+ describe 'last_loaded' do
69
+ it "should return the last loaded seeding script" do
70
+ @tester.last_loaded(seed_files).should == file3
71
+ end
72
+ end
73
+ end
74
+
75
+ describe 'load' do
76
+ before do
77
+ @tester.stub(:write_loaded)
78
+ end
79
+
80
+ it "should print the name of loaded files" do
81
+ @tester.should_receive(:puts).with("Loading: #{File.basename file1.file}")
82
+ @tester.load file1
83
+ end
84
+
85
+ it "should require the seeding scripts" do
86
+ @tester.should_receive(:require).with(file1.file)
87
+ @tester.load file1
88
+ end
89
+
90
+ it "should only require the specified seeding scripts" do
91
+ @tester.should_not_receive(:require).with(file1.file)
92
+ @tester.load file2
93
+ end
94
+
95
+ it "should be able to require multiple scripts" do
96
+ @tester.should_receive(:require).with(file1.file)
97
+ @tester.should_receive(:require).with(file2.file)
98
+ @tester.load [file1, file2]
99
+ end
100
+
101
+ it "should write the loaded scripts to .versioned_file" do
102
+ @tester.should_receive(:write_loaded)
103
+ @tester.load file1
104
+ end
105
+ end
106
+
107
+ describe 'seeds' do
108
+ it "should return the files located in the rails_root/db/seeds folder sorted by version" do
109
+ @tester.seeds.should == seed_files
110
+ end
111
+ end
112
+
113
+ describe 'write_loaded' do
114
+ it "should write the versions of the loaded scripts to the .versioned_seeds file" do
115
+ seed_file = remove_seed_file
116
+ file = mock('file')
117
+ File.should_receive(:open).with(seed_file, 'a').and_yield(file)
118
+ file.should_receive(:puts).with(file1.version.to_s)
119
+ @tester.write_loaded [file1]
120
+ end
121
+ end
122
+
123
+ describe 'already_loaded' do
124
+ it "should return [0] if the file does not exist" do
125
+ remove_seed_file
126
+ @tester.already_loaded.should == [0]
127
+ end
128
+
129
+ it "should return the list of already loaded scripts" do
130
+ File.open(Rails.root + '.versioned_seeds', 'w') do |f|
131
+ 3.times { |n| f.puts "2011120300000#{n+1}" }
132
+ end
133
+ @tester.already_loaded.should == %w(20111203000001 20111203000002 20111203000003)
134
+ end
135
+ end
136
+
137
+ end
138
+ end
@@ -14,4 +14,7 @@ Gem::Specification.new do |gem|
14
14
  gem.name = "versioned_seeds"
15
15
  gem.require_paths = ["lib"]
16
16
  gem.version = VersionedSeeds::VERSION
17
+
18
+ gem.add_development_dependency 'rake', '~> 0.9'
19
+ gem.add_development_dependency 'rspec', '>= 2'
17
20
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: versioned_seeds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,8 +9,30 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-11-27 00:00:00.000000000Z
13
- dependencies: []
12
+ date: 2011-12-05 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: &70330387431100 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '0.9'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: *70330387431100
25
+ - !ruby/object:Gem::Dependency
26
+ name: rspec
27
+ requirement: &70330387428960 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '2'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70330387428960
14
36
  description: Manage your seed scripts by versioning them
15
37
  email:
16
38
  - scourtois@cubyx.fr
@@ -19,13 +41,21 @@ extensions: []
19
41
  extra_rdoc_files: []
20
42
  files:
21
43
  - .gitignore
44
+ - .rspec
22
45
  - Gemfile
23
46
  - README.mdown
24
47
  - Rakefile
48
+ - lib/generators/versioned_seeds/seed_file_generator.rb
49
+ - lib/generators/versioned_seeds/templates/seed_file.rb
25
50
  - lib/tasks/versioned_seeds.rake
26
51
  - lib/versioned_seeds.rb
27
52
  - lib/versioned_seeds/railtie.rb
28
53
  - lib/versioned_seeds/version.rb
54
+ - spec/rails_root/.versioned_seeds
55
+ - spec/rails_root/db/seeds/20111203000001_file1.rb
56
+ - spec/rails_root/db/seeds/20111203000002_file2.rb
57
+ - spec/rails_root/db/seeds/20111203000003_file3.rb
58
+ - spec/versioned_seeds_spec.rb
29
59
  - versioned_seeds.gemspec
30
60
  homepage: http://github.com/simonc/versioned_seeds
31
61
  licenses: []
@@ -52,4 +82,9 @@ signing_key:
52
82
  specification_version: 3
53
83
  summary: Versioned Seeds provides a very simple way to manage your seed scripts by
54
84
  versioning them. It provides rake tasks to load them.
55
- test_files: []
85
+ test_files:
86
+ - spec/rails_root/.versioned_seeds
87
+ - spec/rails_root/db/seeds/20111203000001_file1.rb
88
+ - spec/rails_root/db/seeds/20111203000002_file2.rb
89
+ - spec/rails_root/db/seeds/20111203000003_file3.rb
90
+ - spec/versioned_seeds_spec.rb