dibber 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.rdoc CHANGED
@@ -38,20 +38,33 @@ Add this to your 'db/seeds.rb'
38
38
  Seeder = Dibber::Seeder
39
39
  Seeder.seeds_path = "#{Rails.root}/db/seeds"
40
40
  Seeder.new(Thing, 'things.yml').build
41
+ puts Seeder.report
41
42
 
42
43
  Then run 'rake db:seed'
43
44
 
44
- Seeder will create two new things (unless things with the names 'foo' or
45
- 'bar' already exist, in which case it will update those things with the
46
- values in the YAML file).
45
+ Seeder will create two new things.
47
46
 
48
47
  You'll then be able to do this:
49
48
 
50
49
  thing = Thing.find_by_name(:foo)
51
50
  thing.colour ---> 'red'
52
-
51
+
52
+ == Report
53
+
53
54
  Each time seeds.rb is run, Seeder will output a report detailing start and
54
55
  end time, and a log of how the number of things has changed.
55
56
 
57
+ == Overwriting existing entries
58
+
59
+ As of version 0.2.0, Seeder#build will not overwrite existing data unless
60
+ directed to do so.
61
+
62
+ thing.update_attribute(:colour, 'black')
63
+ Seeder.new(Thing, 'things.yml').build
64
+ thing.reload.colour ----> 'black'
65
+
66
+ Seeder.new(Thing, 'things.yml', :overwrite => true).build
67
+ thing.reload.colour ----> 'red'
68
+
56
69
  Have a look at the lib/examples folder, for a more detailed guide to how
57
70
  Dibber is used.
data/lib/dibber/seeder.rb CHANGED
@@ -3,7 +3,7 @@ require 'yaml'
3
3
 
4
4
  module Dibber
5
5
  class Seeder
6
- attr_accessor :klass, :file, :attribute_method, :name_method
6
+ attr_accessor :klass, :file, :attribute_method, :name_method, :overwrite
7
7
 
8
8
  def self.process_log
9
9
  @process_log || start_process_log
@@ -43,6 +43,7 @@ module Dibber
43
43
  args = {:attributes_method => args} unless args.kind_of?(Hash)
44
44
  @attribute_method = args[:attributes_method] || 'attributes'
45
45
  @name_method = args[:name_method] || 'name'
46
+ @overwrite = args[:overwrite]
46
47
  end
47
48
 
48
49
  def build
@@ -50,8 +51,10 @@ module Dibber
50
51
  check_objects_exist
51
52
  objects.each do |name, attributes|
52
53
  object = klass.send(retrieval_method, name)
53
- object.send("#{attribute_method}=", attributes)
54
- object.save
54
+ if overwrite or !object.send("#{attribute_method}")
55
+ object.send("#{attribute_method}=", attributes)
56
+ object.save
57
+ end
55
58
  end
56
59
  end
57
60
 
@@ -1,3 +1,13 @@
1
1
  module Dibber
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
4
+
5
+ # History
6
+ # =======
7
+ # 0.2.0 Stops overwriting existing entries unless explicitly directed to
8
+ # No need to specify :overwrite => true when creating a new Seeder if
9
+ # existing entries are to be overwritten.
10
+ #
11
+ # 0.1.1 Working version
12
+ # No History before this point
13
+ #
@@ -1,6 +1,6 @@
1
- require 'process_log'
1
+ require_relative '../dibber/process_log'
2
2
 
3
- process_log = ProcessLog.new
3
+ process_log = Dibber::ProcessLog.new
4
4
  process_log.start :time_one, 'Time.now'
5
5
  sleep(2)
6
6
  process_log.finish :time_one
@@ -40,8 +40,8 @@ Seeder.new(Category, 'categories.yml', 'description').build
40
40
  Seeder.new(
41
41
  Category,
42
42
  'categories.yml',
43
- :name_field => :title,
44
- :attributes_field => :description
43
+ :name_method => :title,
44
+ :attributes_method => :description
45
45
  ).build
46
46
 
47
47
  # You can also access Seeders attached process log, and set up a custom log
@@ -2,7 +2,7 @@ $:.unshift File.join(File.dirname(__FILE__),'../..','lib')
2
2
 
3
3
  require 'test/unit'
4
4
  require 'dibber'
5
- require 'dibber/thing'
5
+ require_relative 'thing'
6
6
 
7
7
  module Dibber
8
8
 
@@ -71,7 +71,31 @@ module Dibber
71
71
  assert_equal({'title' => 'one'}, foo.attributes)
72
72
  end
73
73
 
74
- def test_other_method_replacing_attributes
74
+ def test_rebuilding_does_not_overwrite
75
+ test_build
76
+ attributes = {'title' => 'something else'}
77
+ foo = Thing.find_or_initialize_by_name(:foo)
78
+ foo.attributes = attributes
79
+ foo.save
80
+ thing_seeder.build
81
+ assert_equal(2, Thing.count)
82
+ foo = Thing.find_or_initialize_by_name(:foo)
83
+ assert_equal(attributes, foo.attributes)
84
+ end
85
+
86
+ def test_rebuilding_does_overwrite_if_set_to
87
+ test_build
88
+ attributes = {'title' => 'something else'}
89
+ foo = Thing.find_or_initialize_by_name(:foo)
90
+ foo.attributes = attributes
91
+ foo.save
92
+ thing_seeder(:overwrite => true).build
93
+ assert_equal(2, Thing.count)
94
+ foo = Thing.find_or_initialize_by_name(:foo)
95
+ assert_equal({'title' => 'one'}, foo.attributes)
96
+ end
97
+
98
+ def test_other_method_instead_of_attributes
75
99
  thing_seeder('other_method').build
76
100
  foo = Thing.find_or_initialize_by_name(:foo)
77
101
  bar = Thing.find_or_initialize_by_name(:bar)
File without changes
@@ -1,7 +1,7 @@
1
1
  $:.unshift File.join(File.dirname(__FILE__),'../..','lib')
2
2
 
3
3
  require 'test/unit'
4
- require 'dibber/thing'
4
+ require_relative 'thing'
5
5
 
6
6
  module Dibber
7
7
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dibber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
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: 2013-01-03 00:00:00.000000000 Z
12
+ date: 2013-04-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: active_support
@@ -43,13 +43,13 @@ files:
43
43
  - lib/examples/seeds.rb
44
44
  - lib/dibber.rb
45
45
  - lib/dibber/version.rb
46
- - lib/dibber/thing.rb
47
46
  - lib/dibber/process_log.rb
48
47
  - lib/dibber/seeder.rb
49
48
  - MIT-LICENSE
50
49
  - Rakefile
51
50
  - README.rdoc
52
51
  - test/dibber/seeder_test.rb
52
+ - test/dibber/thing.rb
53
53
  - test/dibber/process_log_test.rb
54
54
  - test/dibber/seeds/empty.yml
55
55
  - test/dibber/seeds/things.yml
@@ -80,6 +80,7 @@ specification_version: 3
80
80
  summary: Tool for seeding database from YAML.
81
81
  test_files:
82
82
  - test/dibber/seeder_test.rb
83
+ - test/dibber/thing.rb
83
84
  - test/dibber/process_log_test.rb
84
85
  - test/dibber/seeds/empty.yml
85
86
  - test/dibber/seeds/things.yml