dibber 0.5.0 → 0.7.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: df49d47736e3cf6e58a22a2b84d77e76912c8d32
4
- data.tar.gz: 03277a6b8d5f532f6ba343bfba8dc71a789785ee
2
+ SHA256:
3
+ metadata.gz: 7ac217e07668aaa17e034576c170a3e7736ea6bdedc5776bc05c197ad0604f1a
4
+ data.tar.gz: 6b07f8f169bd0e2e9f3cfd0aa9a9c86d4f4a10dba5f75ebeda44ef4c8b507fd0
5
5
  SHA512:
6
- metadata.gz: 330909b6d4fe39385438b0eda1984c7f1f9571cae69eeba9732bd0927ae9d027ea0f4a6b7fa6324991d446158f89373235bf34c3685f92097cf41e50e0eb4f0f
7
- data.tar.gz: 627b1582b890c6721a35f969c15c338372b1f96aa445e48af4ff4f3a12f9d56690a088557a5034c1c2ba44a32c5f0e7da6a59dbb9dbcffe9ce8235f5fcfd7619
6
+ metadata.gz: 25af64ca3571627ccead7c5c873dd72548d6b58997311b22ad3cd459bbd28e78d4238cb17e8dbaf77e2b144dea0b9ecd5a0fae6b4c31519535a794277fc0616a
7
+ data.tar.gz: abaedc7100af6b56597d4faedced8e17704cddc5212c0e2842ffd293642b357f1653e03d89605636b0917788893cf77d55e40aec677d7c6ae899a3fa86c53bc9
data/README.rdoc CHANGED
@@ -48,6 +48,39 @@ You'll then be able to do this:
48
48
  thing = Thing.find_by(name: 'foo')
49
49
  thing.colour ---> 'red'
50
50
 
51
+ === Pass the name of the class to seed
52
+
53
+ All of these are equivalent commands
54
+
55
+ Dibber::Seeder.seed Thing
56
+ Dibber::Seeder.seed :thing
57
+ Dibber::Seeder.seed 'Thing'
58
+
59
+ === Dynamic content
60
+
61
+ Seed content can also have dynamic content added via ERB. For example, if
62
+ you want the colour of the `foo` thing to be either 'red' or 'yellow', change
63
+ `db/seeds/things.yml` to
64
+
65
+ foo:
66
+ colour: <%= ['red', 'yellow'].sample %>
67
+ size: large
68
+
69
+ Then when we run `Dibber::Seeder.seed :thing` a new `Thing` will be created
70
+ with the colour set to either 'red' or 'yellow'.
71
+
72
+ === Using blocks with Seeder
73
+
74
+ If you pass a block to `.seed` the block will be called on each object being
75
+ built as it is created or updated.
76
+
77
+ So for example, if Thing has attributes `size` and `big` the following is
78
+ possible:
79
+
80
+ Dibber::Seeder.seed(:thing) do |thing|
81
+ thing.big = thing.attributes['size'] > 10
82
+ end
83
+
51
84
  == Report
52
85
 
53
86
  Seeder.report outputs a report detailing start and end time, and a log of how
@@ -94,9 +127,3 @@ If you clone this app, you can run this example at the project root:
94
127
  There is also an example of process log usage:
95
128
 
96
129
  ruby test/examples/process_logs.rb
97
-
98
-
99
-
100
-
101
-
102
-
data/lib/dibber/seeder.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  require 'active_support/inflector'
2
2
  require 'yaml'
3
+ require 'erb'
3
4
 
4
5
  module Dibber
5
6
  class Seeder
@@ -7,16 +8,16 @@ module Dibber
7
8
 
8
9
  class << self
9
10
 
10
- def seed(klass, args = {})
11
+ def seed(klass, args = {}, &block)
11
12
  if klass.kind_of?(String) || klass.kind_of?(Symbol)
12
- name = klass.to_s
13
+ name = klass.to_s.underscore
13
14
  class_name = klass.to_s.strip.classify
14
15
  klass = Kernel.const_get(class_name)
15
16
  else
16
17
  name = klass.to_s.underscore
17
18
  end
18
19
  new_file = "#{name.pluralize}.yml"
19
- new(klass, new_file, args).build
20
+ new(klass, new_file, args).build(&block)
20
21
  end
21
22
 
22
23
  def process_log
@@ -39,7 +40,15 @@ module Dibber
39
40
  end
40
41
 
41
42
  def objects_from(file)
42
- YAML.load_file("#{seeds_path}#{file}")
43
+ YAML.load erb_processed(file_content(file))
44
+ end
45
+
46
+ def erb_processed(content)
47
+ ERB.new(content).result
48
+ end
49
+
50
+ def file_content(file)
51
+ File.read File.join(seeds_path, file)
43
52
  end
44
53
 
45
54
  def seeds_path
@@ -63,14 +72,16 @@ module Dibber
63
72
  def build
64
73
  check_objects_exist
65
74
  start_log
66
- objects.each do |name, attributes|
75
+ objects.map do |name, attributes|
67
76
  object = find_or_initialize_by(name)
68
77
  if overwrite or object.new_record?
69
78
  object.send("#{attribute_method}=", attributes)
79
+ yield(object) if block_given?
70
80
  unless object.save
71
81
  self.class.errors << object.errors
72
82
  end
73
83
  end
84
+ object
74
85
  end
75
86
  end
76
87
 
@@ -130,4 +141,4 @@ module Dibber
130
141
  end
131
142
 
132
143
  end
133
- end
144
+ end
@@ -1,9 +1,14 @@
1
1
  module Dibber
2
- VERSION = "0.5.0"
2
+ VERSION = "0.7.0"
3
3
  end
4
4
 
5
5
  # History
6
6
  # =======
7
+ # 0.7.0 Adds ability to pass blocks into Seeder.seed and Seed#build
8
+ #
9
+ # 0.6.0 ERB parse the source files before passing it to YAML. Allows dynamic
10
+ # content to be used.
11
+ #
7
12
  # 0.5.0 Allow the `Seeder.seed` method to take a Class or the string/symbol
8
13
  # representation of the class name.
9
14
  #
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+ require_relative 'thing'
3
+
4
+ module Dibber
5
+ class DynamicThingTest < Minitest::Test
6
+
7
+ def setup
8
+ Seeder.seeds_path = File.join(File.dirname(__FILE__), 'seeds')
9
+ end
10
+
11
+
12
+ def test_objects_from
13
+ hash = Seeder.objects_from('dynamic_things.yml')
14
+ assert_equal 'something', hash['one']['title']
15
+ end
16
+
17
+ end
18
+ end
@@ -38,7 +38,7 @@ module Dibber
38
38
  def test_clear_process_log
39
39
  test_monitor
40
40
  Seeder.clear_process_log
41
- assert_equal(nil, Seeder.process_log.raw[:things])
41
+ assert_nil Seeder.process_log.raw[:things], "Log should be empty"
42
42
  end
43
43
 
44
44
  def test_seeds_path
@@ -71,12 +71,20 @@ module Dibber
71
71
  assert_equal(0, Thing.count)
72
72
  thing_seeder.build
73
73
  assert_equal(2, Thing.count)
74
- foo = Thing.find_or_initialize_by(name: :foo)
74
+ foo = Thing.find_or_initialize_by(name: :foo)
75
75
  bar = Thing.find_or_initialize_by(name: :bar)
76
76
  assert_equal([foo, bar], Thing.saved)
77
77
  assert_equal({'title' => 'one'}, foo.attributes)
78
78
  end
79
79
 
80
+ def test_build_with_block
81
+ other = 'other'
82
+ thing_seeder.build { |t| t.other_method = other }
83
+ foo = Thing.find_or_initialize_by(name: :foo)
84
+ bar = Thing.find_or_initialize_by(name: :bar)
85
+ assert_equal([foo, bar], Thing.where(other_method: other))
86
+ end
87
+
80
88
  def test_rebuilding_does_not_overwrite
81
89
  test_build
82
90
  attributes = {'title' => 'something else'}
@@ -138,7 +146,22 @@ module Dibber
138
146
  assert_equal({'title' => 'one'}, foo.attributes)
139
147
  end
140
148
 
141
- def test_seed_with_alternative_name_method
149
+ def test_seed_with_block
150
+ other = 'other'
151
+ Seeder.seed(:things) { |thing| thing.other_method = other }
152
+ foo = Thing.find_or_initialize_by(name: :foo)
153
+ bar = Thing.find_or_initialize_by(name: :bar)
154
+ assert_equal([foo, bar], Thing.where(other_method: other))
155
+ end
156
+
157
+ def test_seed_with_block_using_attributes
158
+ other = 'other'
159
+ Seeder.seed(:things) { |thing| thing.other_method = thing.attributes['title'] }
160
+ foo = Thing.find_or_initialize_by(name: :foo)
161
+ assert_equal('one', foo.other_method)
162
+ end
163
+
164
+ def test_seed_with_alternative_name_method
142
165
  Seeder.seed(:things, :name_method => 'other_method')
143
166
  assert_equal(2, Thing.count)
144
167
  foo = Thing.find_or_initialize_by(other_method: :foo)
@@ -179,7 +202,17 @@ module Dibber
179
202
  assert_equal({'title' => 'one'}, foo.attributes)
180
203
  end
181
204
 
182
- def test_seed_with_class_with_alternative_name_method
205
+ def test_seed_with_class_name
206
+ assert_equal(0, Thing.count)
207
+ Seeder.seed('Thing')
208
+ assert_equal(2, Thing.count)
209
+ foo = Thing.find_or_initialize_by(name: :foo)
210
+ bar = Thing.find_or_initialize_by(name: :bar)
211
+ assert_equal([foo, bar], Thing.saved)
212
+ assert_equal({'title' => 'one'}, foo.attributes)
213
+ end
214
+
215
+ def test_seed_with_class_with_alternative_name_method
183
216
  Seeder.seed(Thing, :name_method => 'other_method')
184
217
  assert_equal(2, Thing.count)
185
218
  foo = Thing.find_or_initialize_by(other_method: :foo)
@@ -244,7 +277,7 @@ module Dibber
244
277
  end
245
278
 
246
279
  def thing_file_path
247
- File.join(File.dirname(__FILE__),'seeds', 'things.yml')
280
+ File.join(File.dirname(__FILE__), 'seeds', 'things.yml')
248
281
  end
249
282
  end
250
283
 
@@ -0,0 +1,2 @@
1
+ one:
2
+ title: <%= 'something' %>
@@ -7,7 +7,7 @@ class NotQuiteActiveRecord
7
7
  extract_attributes
8
8
  self.class.members << self
9
9
  end
10
-
10
+
11
11
  def extract_attributes
12
12
  attributes.each do |name, value|
13
13
  self.send("#{name}=", value.to_s) if self.respond_to?(name.to_sym)
@@ -22,11 +22,11 @@ class NotQuiteActiveRecord
22
22
  def new_record?
23
23
  !self.class.saved.include? self
24
24
  end
25
-
25
+
26
26
  def self.where(hash)
27
27
  members.select{|m| hash.collect{|k, v| m.send(k) == v.to_s}.uniq == [true]}
28
28
  end
29
-
29
+
30
30
  def self.find_or_initialize_by(hash)
31
31
  if exists?(hash)
32
32
  where(hash).first
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dibber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.5.0
4
+ version: 0.7.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Nichols
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-04-29 00:00:00.000000000 Z
11
+ date: 2022-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: minitest
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '5.0'
33
+ version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '5.0'
40
+ version: '0'
41
41
  description: Packages up code needed to pull data from YAML files when seeding, and
42
42
  adds a process log.
43
43
  email:
@@ -53,9 +53,11 @@ files:
53
53
  - lib/dibber/process_log.rb
54
54
  - lib/dibber/seeder.rb
55
55
  - lib/dibber/version.rb
56
+ - test/dibber/dynamic_thing_test.rb
56
57
  - test/dibber/foo/bar.rb
57
58
  - test/dibber/process_log_test.rb
58
59
  - test/dibber/seeder_test.rb
60
+ - test/dibber/seeds/dynamic_things.yml
59
61
  - test/dibber/seeds/empty.yml
60
62
  - test/dibber/seeds/foo/bars.yml
61
63
  - test/dibber/seeds/things.yml
@@ -76,7 +78,7 @@ files:
76
78
  - test/test_helper.rb
77
79
  homepage: https://github.com/reggieb/Dibber
78
80
  licenses:
79
- - MIT-LICENSE
81
+ - MIT
80
82
  metadata: {}
81
83
  post_install_message:
82
84
  rdoc_options: []
@@ -93,30 +95,31 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
95
  - !ruby/object:Gem::Version
94
96
  version: '0'
95
97
  requirements: []
96
- rubyforge_project:
97
- rubygems_version: 2.4.8
98
+ rubygems_version: 3.3.22
98
99
  signing_key:
99
100
  specification_version: 4
100
101
  summary: Tool for seeding database from YAML.
101
102
  test_files:
103
+ - test/dibber/dynamic_thing_test.rb
102
104
  - test/dibber/foo/bar.rb
103
- - test/dibber/seeder_test.rb
104
- - test/dibber/thing.rb
105
105
  - test/dibber/process_log_test.rb
106
+ - test/dibber/seeder_test.rb
107
+ - test/dibber/seeds/dynamic_things.yml
108
+ - test/dibber/seeds/empty.yml
106
109
  - test/dibber/seeds/foo/bars.yml
107
110
  - test/dibber/seeds/things.yml
108
- - test/dibber/seeds/empty.yml
111
+ - test/dibber/thing.rb
109
112
  - test/dibber/thing_test.rb
110
- - test/examples/seeds.rb
113
+ - test/examples/models/admin_user.rb
111
114
  - test/examples/models/borough.rb
112
- - test/examples/models/disclaimer.rb
113
115
  - test/examples/models/category.rb
116
+ - test/examples/models/disclaimer.rb
114
117
  - test/examples/models/fee.rb
115
- - test/examples/models/admin_user.rb
116
- - test/examples/seeds/fees.yml
118
+ - test/examples/process_logs.rb
119
+ - test/examples/seeds/boroughs.yml
117
120
  - test/examples/seeds/categories.yml
118
121
  - test/examples/seeds/disclaimer/documents.yml
119
- - test/examples/seeds/boroughs.yml
120
- - test/examples/process_logs.rb
122
+ - test/examples/seeds/fees.yml
123
+ - test/examples/seeds.rb
121
124
  - test/not_quite_active_record.rb
122
125
  - test/test_helper.rb