iain-fill 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/Manifest CHANGED
@@ -1,7 +1,12 @@
1
1
  Manifest
2
2
  README.rdoc
3
3
  Rakefile
4
+ VERSION
5
+ fill.gemspec
4
6
  lib/fill.rb
5
7
  lib/fill/configure.rb
6
8
  lib/fill/presenter.rb
7
9
  lib/fill/procedure.rb
10
+ spec/fill_spec.rb
11
+ spec/spec.opts
12
+ spec/spec_helper.rb
@@ -27,6 +27,10 @@ long it took, resulting in this output:
27
27
  | 10 | 0 | Project | 0.018627 |
28
28
  +-------+--------+---------+----------+
29
29
 
30
+ You'll need to install hirb for this pretty output. Otherwise it won't print
31
+ out this pretty table. I recommend using Hirb anyway. Find it at
32
+ http://github.com/cldwalker/hirb
33
+
30
34
  == Why?
31
35
 
32
36
  At my company, we taught testers, customers and developers to use
@@ -40,17 +44,28 @@ database when they fucked it up.
40
44
 
41
45
  By specifying a block, do whatever you need to fill a model.
42
46
 
43
- Usage: db.produce :model1, :model2 do ... end
47
+ Example:
48
+
49
+ db.produce :users, :memberships do
50
+ 10.times do
51
+ user = Factory(:user)
52
+ membership = Factory(:membership, :user => user)
53
+ end
54
+ end
44
55
 
45
56
  Specify the models that you want. The models that you specified will be
46
57
  emptied, which is handy if you're building relational models, like users
47
58
  and their memberships.
48
59
 
60
+ The models are named *plural*.
61
+
49
62
  === db.fill
50
63
 
51
- A simple list of values.
64
+ Provide a simple list of values.
65
+
66
+ Example
52
67
 
53
- Usage: db.fill :model, :attribute, "value 1", "value 2", "value 3"
68
+ db.fill :projects, :name, "Foo", "Bar", "Baz", "etc"
54
69
 
55
70
  For simple models with only one distinct attribute, you can just specify
56
71
  the model, attribute and the values.
@@ -61,14 +76,47 @@ See also the iain/root_table plugin if you have many of these.
61
76
 
62
77
  Invoke a rake task.
63
78
 
64
- Usage: db.invoke "some:task", :model
79
+ db.invoke "some:task", :projects
80
+
81
+ I use a seperate rake task whenever I need to import files. Of course you
82
+ can do that in the +db.produce+, but that clutters your seeds.rb.
65
83
 
66
84
  === Global options
67
85
 
68
- * :needs specify one or many dependencies, tables to be filled before
69
- filling the ones you're specifying now.
70
- * :delete set to false if you don't want to delete all records before
71
- continuing
86
+ These options work on all above mentioned methods.
87
+
88
+ ==== :needs
89
+
90
+ Specify one or many dependencies, tables to be filled before filling the ones
91
+ you're specifying now.
92
+
93
+ Example
94
+
95
+ db.produce :memberships, :needs => :users do
96
+ User.all.each { |user| Factory(:membership, :user => user) }
97
+ end
98
+
99
+ ==== :delete
100
+
101
+ Set to false if you don't want to delete all records before filling it.
102
+
103
+ Example:
104
+
105
+ db.invoke "import:zipcodes", :zips, :delete => false
106
+
107
+ In this example, the zipcodes import takes a long time to complete, so it
108
+ doesn't insert them when the database is already filled.
109
+
110
+ ==== :name
111
+
112
+ The output uses Rails i18n methods to determine how the output calls the
113
+ models filled, but if you want to specify your own name, use this option.
114
+
115
+ Example:
116
+
117
+ db.produce :users, :name => "Accounts" do
118
+ ....
119
+ end
72
120
 
73
121
  == Installation
74
122
 
@@ -76,8 +124,8 @@ Add this to config/environment.rb:
76
124
 
77
125
  config.gem "iain-fill", :lib => "fill", :source => "http://gems.github.com"
78
126
 
79
- And run "rake gems:install" and you're done.
127
+ And run "+rake gems:install+" and you're done.
80
128
 
81
129
  ---
82
130
 
83
- Made by Iain, Released under the MIT License
131
+ Made by Iain, http://iain.nl/, Released under the MIT License
data/Rakefile CHANGED
@@ -1,13 +1,24 @@
1
1
  require 'rake'
2
2
  require 'rake/rdoctask'
3
3
 
4
- require 'echoe'
5
- Echoe.new('fill', '0.0.1') do |p|
6
- p.description = "Fill your database"
7
- p.url = "http://iain.nl"
8
- p.author = "iain"
9
- p.email = "iain@iain.nl"
10
- p.ignore_pattern = []
11
- p.development_dependencies = []
12
- p.runtime_dependencies = []
4
+ require 'spec/rake/spectask'
5
+ desc "Run all specs in spec directory"
6
+ Spec::Rake::SpecTask.new(:spec) do |t|
7
+ t.spec_opts = ['--options spec/spec.opts']
8
+ t.spec_files = FileList['spec/**/*_spec.rb']
9
+ end
10
+
11
+
12
+ begin
13
+ require 'echoe'
14
+ Echoe.new('fill', File.read('VERSION').chomp) do |p|
15
+ p.description = "Fill your database"
16
+ p.url = "http://iain.nl"
17
+ p.author = "iain"
18
+ p.email = "iain@iain.nl"
19
+ p.ignore_pattern = []
20
+ p.development_dependencies = []
21
+ p.runtime_dependencies = []
22
+ end
23
+ rescue LoadError
13
24
  end
@@ -2,15 +2,15 @@
2
2
 
3
3
  Gem::Specification.new do |s|
4
4
  s.name = %q{fill}
5
- s.version = "0.0.1"
5
+ s.version = "0.0.2"
6
6
 
7
7
  s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
8
8
  s.authors = ["iain"]
9
- s.date = %q{2009-09-21}
9
+ s.date = %q{2009-09-25}
10
10
  s.description = %q{Fill your database}
11
11
  s.email = %q{iain@iain.nl}
12
12
  s.extra_rdoc_files = ["README.rdoc", "lib/fill.rb", "lib/fill/configure.rb", "lib/fill/presenter.rb", "lib/fill/procedure.rb"]
13
- s.files = ["README.rdoc", "Rakefile", "lib/fill.rb", "lib/fill/configure.rb", "lib/fill/presenter.rb", "lib/fill/procedure.rb", "Manifest", "fill.gemspec"]
13
+ s.files = ["Manifest", "README.rdoc", "Rakefile", "lib/fill.rb", "lib/fill/configure.rb", "lib/fill/presenter.rb", "lib/fill/procedure.rb", "fill.gemspec"]
14
14
  s.homepage = %q{http://iain.nl}
15
15
  s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Fill", "--main", "README.rdoc"]
16
16
  s.require_paths = ["lib"]
@@ -1,18 +1,31 @@
1
1
  require 'activesupport'
2
- require 'fill/configure'
3
- require 'fill/presenter'
4
- require 'fill/procedure'
2
+ require File.dirname(__FILE__) + '/fill/configure'
3
+ require File.dirname(__FILE__) + '/fill/presenter'
4
+ require File.dirname(__FILE__) + '/fill/procedure'
5
5
 
6
6
  module Fill
7
7
 
8
+ VERSION = File.read(File.dirname(__FILE__) + '/../VERSION').chomp
9
+
8
10
  class << self
9
11
 
12
+
13
+ attr_writer :out
14
+ def out
15
+ @out ||= STDOUT
16
+ end
17
+
10
18
  def database
11
19
  db = Configure.new
12
20
  yield db
13
- bm = time { db.perform! }
14
- puts Presenter
15
- puts "Database filled in %.2f seconds" % bm
21
+ perform!(db)
22
+ end
23
+
24
+ def perform!(configuration)
25
+ bm = time { configuration.perform! }
26
+ out.puts Presenter
27
+ out.puts "Database filled in %.2f seconds" % bm
28
+ Presenter.clear!
16
29
  end
17
30
 
18
31
  def time
@@ -14,15 +14,23 @@ module Fill
14
14
  presenter.to_s
15
15
  end
16
16
 
17
+ def self.hirb?
18
+ require 'hirb'
19
+ true
20
+ rescue LoadError
21
+ false
22
+ end
23
+
24
+ def self.clear!
25
+ @presenter = nil
26
+ end
27
+
17
28
  def add(data)
18
29
  presented.push(data) if data && !presented.include?(data)
19
30
  end
20
31
 
21
32
  def hirb?
22
- require 'hirb'
23
- true
24
- rescue LoadError
25
- false
33
+ self.class.hirb?
26
34
  end
27
35
 
28
36
  def presented
@@ -42,8 +50,15 @@ module Fill
42
50
  end
43
51
 
44
52
  def present_hash
45
- hash.map do |row|
46
- row.map { |key, value| "#{key}: #{value}" }.join(" - ")
53
+ presentable.map do |row|
54
+ format_row(row).join(" - ")
55
+ end
56
+ end
57
+
58
+ def format_row(row)
59
+ row.map do |key, value|
60
+ value = "%.2f" % value if key == "Time"
61
+ "#{key}: #{value}"
47
62
  end
48
63
  end
49
64
 
@@ -2,7 +2,7 @@ module Fill
2
2
 
3
3
  class Procedure
4
4
 
5
- attr_accessor :block
5
+ attr_accessor :block, :options
6
6
 
7
7
  def initialize(models, options = {}, &block)
8
8
  @block = block
@@ -19,12 +19,19 @@ module Fill
19
19
  end
20
20
 
21
21
  def human_models
22
- @options[:name] ||
23
- models.map { |model| model.respond_to?(:human_name) ? model.human_name : model.to_s }.join(', ')
22
+ @human_models ||= (options[:name] || humanize_models)
23
+ end
24
+
25
+ def humanize_models
26
+ models.map { |model| i18n_name(model) }.join(', ')
27
+ end
28
+
29
+ def i18n_name(model)
30
+ model.respond_to?(:human_name) ? model.human_name : model.to_s
24
31
  end
25
32
 
26
33
  def delete_all
27
- models.each { |model| model.delete_all } if @options[:delete]
34
+ models.map { |model| model.delete_all }
28
35
  end
29
36
 
30
37
  def models
@@ -36,8 +43,8 @@ module Fill
36
43
  end
37
44
 
38
45
  def perform
39
- @before = count
40
- @time = Fill.time { self.delete_all; block.call }
46
+ @before = options[:delete] ? delete_all : count
47
+ @time = Fill.time { block.call }
41
48
  @after = count
42
49
  Presenter.present self
43
50
  true
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iain-fill
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
  - iain
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-21 00:00:00 -07:00
12
+ date: 2009-09-25 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
@@ -26,13 +26,13 @@ extra_rdoc_files:
26
26
  - lib/fill/presenter.rb
27
27
  - lib/fill/procedure.rb
28
28
  files:
29
+ - Manifest
29
30
  - README.rdoc
30
31
  - Rakefile
31
32
  - lib/fill.rb
32
33
  - lib/fill/configure.rb
33
34
  - lib/fill/presenter.rb
34
35
  - lib/fill/procedure.rb
35
- - Manifest
36
36
  - fill.gemspec
37
37
  has_rdoc: false
38
38
  homepage: http://iain.nl