dibber 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODFiZTE4ZmRiMjBkNmU1M2NlNDA5NTczMzRmMDZmNWQ3YTljNjZkZQ==
5
+ data.tar.gz: !binary |-
6
+ ZmFkYWQ1NzkxM2NjMDVmMGExYTJhMTBkOTJmZTZkNTljODAwYzdjZA==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MWI4OGUyZjMxNTU2NTQzZTNhOGU3ZDM5OTY0Yjk0ZjEwOWMzNTYzOTQ5N2U1
10
+ MjIwNTc3MzUxMmZjZTM2ZTlhOWUxZGJkY2U5MmU4Mjc4OWFkY2MxMTVmNjU4
11
+ ZGMzNDFkNThjYTgwYzBkZmE0YmNiMjBkNzNjMDJmNjM0ZmQ2N2Y=
12
+ data.tar.gz: !binary |-
13
+ NmJmYTM5NjM0ZWYzNmM1MDBmMGFlMmY3ZGUwY2UyYmFhMWM2OTc1ZTFhYzU4
14
+ OGNlYjJhMDViMDhlNDdjY2RhYzYwODZiODRmYjExYWEyZjBjOGFlOTJlOWMy
15
+ YWZiYWIyOTZiMmFiMzZhMmE2MWZhNzIyYTJjMjZjOGExN2RhNDI=
@@ -71,7 +71,7 @@ Seeder#build will not overwrite existing data unless directed to do so.
71
71
  Seeder.seed :thing
72
72
  thing.reload.colour ----> 'black'
73
73
 
74
- Seeder.seed(:thing, :overwrite => true).build
74
+ Seeder.seed(:thing, :overwrite => true)
75
75
  thing.reload.colour ----> 'red'
76
76
 
77
77
 
@@ -58,7 +58,7 @@ module Dibber
58
58
  check_objects_exist
59
59
  start_log
60
60
  objects.each do |name, attributes|
61
- object = klass.send(retrieval_method, name)
61
+ object = find_or_initialize_by(name)
62
62
  if overwrite or object.new_record?
63
63
  object.send("#{attribute_method}=", attributes)
64
64
  unless object.save
@@ -94,8 +94,16 @@ module Dibber
94
94
  raise "No objects returned from file: #{self.class.seeds_path}#{file}" unless objects
95
95
  end
96
96
 
97
- def retrieval_method
98
- "find_or_initialize_by_#{name_method}"
97
+ def find_or_initialize_by(name)
98
+ if klass.exists?(name_method_sym => name)
99
+ klass.where(name_method_sym => name).first
100
+ else
101
+ klass.new(name_method_sym => name)
102
+ end
103
+ end
104
+
105
+ def name_method_sym
106
+ name_method.to_sym
99
107
  end
100
108
 
101
109
  def self.try_to_guess_seeds_path
@@ -1,9 +1,12 @@
1
1
  module Dibber
2
- VERSION = "0.3.1"
2
+ VERSION = "0.4.0"
3
3
  end
4
4
 
5
5
  # History
6
6
  # =======
7
+ # 0.4.0 Removes dependency on find_or_initialize_by_name type ActiveRecord
8
+ # dynamic methods that are no longer supported in Rails
9
+ #
7
10
  # 0.3.1 Adds error messages when object save fails
8
11
  #
9
12
  # 0.3.0 Adds seed method
@@ -7,117 +7,117 @@ require_relative 'thing'
7
7
  module Dibber
8
8
 
9
9
  class SeederTest < Test::Unit::TestCase
10
-
10
+
11
11
  def setup
12
12
  Seeder.seeds_path = File.join(File.dirname(__FILE__), 'seeds')
13
13
  end
14
-
14
+
15
15
  def teardown
16
16
  Thing.clear_all
17
17
  end
18
-
18
+
19
19
  def test_process_log
20
20
  assert_kind_of(ProcessLog, Seeder.process_log)
21
21
  end
22
-
22
+
23
23
  def test_process_log_start
24
24
  @log = Seeder.process_log
25
25
  assert(@log.raw.keys.include?(:time), 'time should be logged')
26
26
  end
27
-
27
+
28
28
  def test_report
29
29
  test_process_log_start
30
30
  assert_equal(@log.report, Seeder.report)
31
31
  end
32
-
32
+
33
33
  def test_monitor
34
34
  Seeder.monitor(Thing)
35
35
  assert_equal({:command => 'Thing.count', :start => Thing.count}, Seeder.process_log.raw[:things])
36
36
  end
37
-
37
+
38
38
  def test_seeds_path
39
39
  assert_match(/test\/dibber\/seeds\/$/, Seeder.seeds_path)
40
40
  end
41
-
41
+
42
42
  def test_objects_from
43
43
  content = YAML.load_file(thing_file_path)
44
44
  assert_equal(content, Seeder.objects_from('things.yml'))
45
45
  end
46
-
46
+
47
47
  def test_start_log
48
48
  thing_seeder.start_log
49
49
  assert(Seeder.process_log.raw.keys.include?(:things), 'Things should be logged')
50
50
  end
51
-
51
+
52
52
  def test_objects
53
53
  content = YAML.load_file(thing_file_path)
54
54
  assert_equal(content, thing_seeder.objects)
55
55
  end
56
-
56
+
57
57
  def test_build_with_no_objects
58
58
  thing_seeder = Seeder.new(Thing, 'empty.yml')
59
59
  assert_raise RuntimeError do
60
60
  thing_seeder.build
61
61
  end
62
62
  end
63
-
63
+
64
64
  def test_build
65
65
  assert_equal(0, Thing.count)
66
66
  thing_seeder.build
67
67
  assert_equal(2, Thing.count)
68
- foo = Thing.find_or_initialize_by_name(:foo)
69
- bar = Thing.find_or_initialize_by_name(:bar)
68
+ foo = Thing.find_or_initialize_by(name: :foo)
69
+ bar = Thing.find_or_initialize_by(name: :bar)
70
70
  assert_equal([foo, bar], Thing.saved)
71
71
  assert_equal({'title' => 'one'}, foo.attributes)
72
72
  end
73
-
73
+
74
74
  def test_rebuilding_does_not_overwrite
75
75
  test_build
76
76
  attributes = {'title' => 'something else'}
77
- foo = Thing.find_or_initialize_by_name(:foo)
77
+ foo = Thing.find_or_initialize_by(name: :foo)
78
78
  foo.attributes = attributes
79
79
  foo.save
80
80
  thing_seeder.build
81
81
  assert_equal(2, Thing.count)
82
- foo = Thing.find_or_initialize_by_name(:foo)
82
+ foo = Thing.find_or_initialize_by(name: :foo)
83
83
  assert_equal(attributes, foo.attributes)
84
84
  end
85
-
85
+
86
86
  def test_rebuilding_does_overwrite_if_set_to
87
87
  test_build
88
88
  attributes = {'title' => 'something else'}
89
- foo = Thing.find_or_initialize_by_name(:foo)
89
+ foo = Thing.find_or_initialize_by(name: :foo)
90
90
  foo.attributes = attributes
91
91
  foo.save
92
92
  thing_seeder(:overwrite => true).build
93
93
  assert_equal(2, Thing.count)
94
- foo = Thing.find_or_initialize_by_name(:foo)
94
+ foo = Thing.find_or_initialize_by(name: :foo)
95
95
  assert_equal({'title' => 'one'}, foo.attributes)
96
- end
97
-
96
+ end
97
+
98
98
  def test_other_method_instead_of_attributes
99
99
  thing_seeder('other_method').build
100
- foo = Thing.find_or_initialize_by_name(:foo)
101
- bar = Thing.find_or_initialize_by_name(:bar)
100
+ foo = Thing.find_or_initialize_by(name: :foo)
101
+ bar = Thing.find_or_initialize_by(name: :bar)
102
102
  assert_equal([foo, bar], Thing.saved)
103
- assert_nil(foo.attributes)
103
+ assert_equal({:name=>"foo"}, foo.attributes)
104
104
  assert_equal({'title' => 'one'}, foo.other_method)
105
105
  end
106
-
106
+
107
107
  def test_other_method_replacing_attributes_via_args
108
108
  thing_seeder(:attributes_method => 'other_method').build
109
- foo = Thing.find_or_initialize_by_name(:foo)
110
- bar = Thing.find_or_initialize_by_name(:bar)
109
+ foo = Thing.find_or_initialize_by(name: :foo)
110
+ bar = Thing.find_or_initialize_by(name: :bar)
111
111
  assert_equal([foo, bar], Thing.saved)
112
- assert_nil(foo.attributes)
112
+ assert_equal({:name=>"foo"}, foo.attributes)
113
113
  assert_equal({'title' => 'one'}, foo.other_method)
114
114
  end
115
-
115
+
116
116
  def test_alternative_name_method
117
117
  thing_seeder(:name_method => 'other_method').build
118
118
  assert_equal(2, Thing.count)
119
- foo = Thing.find_or_initialize_by_other_method(:foo)
120
- bar = Thing.find_or_initialize_by_other_method(:bar)
119
+ foo = Thing.find_or_initialize_by(other_method: :foo)
120
+ bar = Thing.find_or_initialize_by(other_method: :bar)
121
121
  assert_equal([foo, bar], Thing.saved)
122
122
  assert_equal({'title' => 'one'}, foo.attributes)
123
123
  end
@@ -126,8 +126,8 @@ module Dibber
126
126
  assert_equal(0, Thing.count)
127
127
  Seeder.seed(:things)
128
128
  assert_equal(2, Thing.count)
129
- foo = Thing.find_or_initialize_by_name(:foo)
130
- bar = Thing.find_or_initialize_by_name(:bar)
129
+ foo = Thing.find_or_initialize_by(name: :foo)
130
+ bar = Thing.find_or_initialize_by(name: :bar)
131
131
  assert_equal([foo, bar], Thing.saved)
132
132
  assert_equal({'title' => 'one'}, foo.attributes)
133
133
  end
@@ -135,8 +135,8 @@ module Dibber
135
135
  def test_seed_with_alternative_name_method
136
136
  Seeder.seed(:things, :name_method => 'other_method')
137
137
  assert_equal(2, Thing.count)
138
- foo = Thing.find_or_initialize_by_other_method(:foo)
139
- bar = Thing.find_or_initialize_by_other_method(:bar)
138
+ foo = Thing.find_or_initialize_by(other_method: :foo)
139
+ bar = Thing.find_or_initialize_by(other_method: :bar)
140
140
  assert_equal([foo, bar], Thing.saved)
141
141
  assert_equal({'title' => 'one'}, foo.attributes)
142
142
  end
@@ -180,16 +180,16 @@ module Dibber
180
180
  assert_equal '/some/path/db/seeds/', Seeder.seeds_path
181
181
  Dibber.send(:remove_const, :Rails)
182
182
  end
183
-
184
-
183
+
184
+
185
185
  private
186
186
  def thing_seeder(method = 'attributes')
187
187
  @thing_seeder = Seeder.new(Thing, 'things.yml', method)
188
188
  end
189
-
189
+
190
190
  def thing_file_path
191
191
  File.join(File.dirname(__FILE__),'seeds', 'things.yml')
192
192
  end
193
193
  end
194
-
194
+
195
195
  end
@@ -13,14 +13,14 @@ module Dibber
13
13
 
14
14
  def test_find_or_initialize_by_name
15
15
  @name = 'foo'
16
- @thing = Thing.find_or_initialize_by_name(@name)
16
+ @thing = Thing.find_or_initialize_by(name: @name)
17
17
  assert_equal(@name, @thing.name)
18
18
  end
19
19
 
20
20
  def test_find_or_initialize_by_name_with_symbol
21
21
  test_find_or_initialize_by_name
22
22
  assert_equal(1, Thing.count)
23
- thing = Thing.find_or_initialize_by_name(:foo)
23
+ thing = Thing.find_or_initialize_by(name: :foo)
24
24
  assert_equal(1, Thing.count)
25
25
  assert_equal('foo', thing.name)
26
26
  end
@@ -33,7 +33,7 @@ module Dibber
33
33
 
34
34
  def test_count
35
35
  assert_equal(0, Thing.count)
36
- Thing.find_or_initialize_by_name(:thing_for_count_test)
36
+ Thing.find_or_initialize_by(name: :thing_for_count_test)
37
37
  assert_equal(1, Thing.count)
38
38
  end
39
39
 
@@ -52,7 +52,7 @@ module Dibber
52
52
  end
53
53
 
54
54
  def test_save_and_saved
55
- thing = Thing.find_or_initialize_by_name(:thing_for_save_test)
55
+ thing = Thing.find_or_initialize_by(name: :thing_for_save_test)
56
56
  assert !Thing.saved.include?(thing), 'saved things should not include thing'
57
57
  assert thing.save, 'should return true on save'
58
58
  assert Thing.saved.include?(thing), 'saved things should include thing'
@@ -71,7 +71,7 @@ module Dibber
71
71
  end
72
72
 
73
73
  def test_find_or_initialize_by_other_method
74
- thing = Thing.find_or_initialize_by_other_method(:something)
74
+ thing = Thing.find_or_initialize_by(other_method: :something)
75
75
  assert_nil(thing.name)
76
76
  assert_equal('something', thing.other_method)
77
77
  end
@@ -1,12 +1,18 @@
1
1
  # Mock ActiveRecord::Base
2
2
  class NotQuiteActiveRecord
3
- attr_reader :name
4
- attr_accessor :attributes
3
+ attr_accessor :name, :title, :other_method, :attributes
5
4
 
6
- def initialize(name = nil)
7
- @name = name.to_s if name
5
+ def initialize(args = {})
6
+ @attributes = args
7
+ extract_attributes
8
8
  self.class.members << self
9
9
  end
10
+
11
+ def extract_attributes
12
+ attributes.each do |name, value|
13
+ self.send("#{name}=", value.to_s) if self.respond_to?(name.to_sym)
14
+ end
15
+ end
10
16
 
11
17
  def save
12
18
  self.class.saved << self if new_record?
@@ -16,39 +22,16 @@ class NotQuiteActiveRecord
16
22
  def new_record?
17
23
  !self.class.saved.include? self
18
24
  end
19
-
20
- def self.find_or_initialize_by_name(name)
21
- existing = members.select{|m| m.name == name.to_s}
22
- if existing.empty?
23
- new(name)
24
- else
25
- existing.first
26
- end
27
- end
28
-
29
- def self.find_or_initialize_by_title(title)
30
- existing = members.select{|m| m.title == title.to_s}
31
- if existing.empty?
32
- fee = new
33
- fee.title = title
34
- fee
35
- else
36
- existing.first
37
- end
25
+
26
+ def self.where(hash)
27
+ members.select{|m| hash.collect{|k, v| m.send(k) == v.to_s}.uniq == [true]}
38
28
  end
39
-
40
- def self.find_or_create_by_name(name)
41
- find_or_initialize_by_name(name).save
42
- end
43
-
44
- def self.find_or_initialize_by_other_method(data)
45
- existing = members.select{|m| m.other_method == data.to_s}
46
- if existing.empty?
47
- thing = new
48
- thing.other_method = data.to_s
49
- thing
29
+
30
+ def self.find_or_initialize_by(hash)
31
+ if exists?(hash)
32
+ where(hash).first
50
33
  else
51
- existing.first
34
+ new(hash)
52
35
  end
53
36
  end
54
37
 
metadata CHANGED
@@ -1,20 +1,18 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dibber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
5
- prerelease:
4
+ version: 0.4.0
6
5
  platform: ruby
7
6
  authors:
8
7
  - Rob Nichols
9
8
  autorequire:
10
9
  bindir: bin
11
10
  cert_chain: []
12
- date: 2013-09-25 00:00:00.000000000 Z
11
+ date: 2014-10-10 00:00:00.000000000 Z
13
12
  dependencies:
14
13
  - !ruby/object:Gem::Dependency
15
14
  name: active_support
16
15
  requirement: !ruby/object:Gem::Requirement
17
- none: false
18
16
  requirements:
19
17
  - - ! '>='
20
18
  - !ruby/object:Gem::Version
@@ -22,7 +20,6 @@ dependencies:
22
20
  type: :development
23
21
  prerelease: false
24
22
  version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
23
  requirements:
27
24
  - - ! '>='
28
25
  - !ruby/object:Gem::Version
@@ -35,72 +32,71 @@ executables: []
35
32
  extensions: []
36
33
  extra_rdoc_files: []
37
34
  files:
35
+ - MIT-LICENSE
36
+ - README.rdoc
37
+ - Rakefile
38
38
  - lib/dibber.rb
39
- - lib/dibber/version.rb
40
39
  - lib/dibber/process_log.rb
41
40
  - lib/dibber/seeder.rb
42
- - MIT-LICENSE
43
- - Rakefile
44
- - README.rdoc
45
- - test/examples/process_logs.rb
41
+ - lib/dibber/version.rb
42
+ - test/dibber/process_log_test.rb
43
+ - test/dibber/seeder_test.rb
44
+ - test/dibber/seeds/empty.yml
45
+ - test/dibber/seeds/things.yml
46
+ - test/dibber/thing.rb
47
+ - test/dibber/thing_test.rb
46
48
  - test/examples/models/admin_user.rb
47
- - test/examples/models/disclaimer.rb
48
- - test/examples/models/category.rb
49
49
  - test/examples/models/borough.rb
50
+ - test/examples/models/category.rb
51
+ - test/examples/models/disclaimer.rb
50
52
  - test/examples/models/fee.rb
53
+ - test/examples/process_logs.rb
54
+ - test/examples/seeds.rb
51
55
  - test/examples/seeds/boroughs.yml
56
+ - test/examples/seeds/categories.yml
52
57
  - test/examples/seeds/disclaimer/documents.yml
53
58
  - test/examples/seeds/fees.yml
54
- - test/examples/seeds/categories.yml
55
- - test/examples/seeds.rb
56
59
  - test/not_quite_active_record.rb
57
- - test/dibber/seeder_test.rb
58
- - test/dibber/thing.rb
59
- - test/dibber/process_log_test.rb
60
- - test/dibber/seeds/empty.yml
61
- - test/dibber/seeds/things.yml
62
- - test/dibber/thing_test.rb
63
60
  homepage: https://github.com/reggieb/Dibber
64
61
  licenses:
65
62
  - MIT-LICENSE
63
+ metadata: {}
66
64
  post_install_message:
67
65
  rdoc_options: []
68
66
  require_paths:
69
67
  - lib
70
68
  required_ruby_version: !ruby/object:Gem::Requirement
71
- none: false
72
69
  requirements:
73
70
  - - ! '>='
74
71
  - !ruby/object:Gem::Version
75
72
  version: '0'
76
73
  required_rubygems_version: !ruby/object:Gem::Requirement
77
- none: false
78
74
  requirements:
79
75
  - - ! '>='
80
76
  - !ruby/object:Gem::Version
81
77
  version: '0'
82
78
  requirements: []
83
79
  rubyforge_project:
84
- rubygems_version: 1.8.25
80
+ rubygems_version: 2.2.2
85
81
  signing_key:
86
- specification_version: 3
82
+ specification_version: 4
87
83
  summary: Tool for seeding database from YAML.
88
84
  test_files:
89
- - test/examples/process_logs.rb
90
85
  - test/examples/models/admin_user.rb
91
86
  - test/examples/models/disclaimer.rb
87
+ - test/examples/models/fee.rb
92
88
  - test/examples/models/category.rb
93
89
  - test/examples/models/borough.rb
94
- - test/examples/models/fee.rb
95
- - test/examples/seeds/boroughs.yml
96
- - test/examples/seeds/disclaimer/documents.yml
97
- - test/examples/seeds/fees.yml
98
90
  - test/examples/seeds/categories.yml
91
+ - test/examples/seeds/fees.yml
92
+ - test/examples/seeds/disclaimer/documents.yml
93
+ - test/examples/seeds/boroughs.yml
99
94
  - test/examples/seeds.rb
95
+ - test/examples/process_logs.rb
100
96
  - test/not_quite_active_record.rb
101
97
  - test/dibber/seeder_test.rb
102
- - test/dibber/thing.rb
103
- - test/dibber/process_log_test.rb
98
+ - test/dibber/thing_test.rb
104
99
  - test/dibber/seeds/empty.yml
105
100
  - test/dibber/seeds/things.yml
106
- - test/dibber/thing_test.rb
101
+ - test/dibber/thing.rb
102
+ - test/dibber/process_log_test.rb