dibber 0.6.0 → 0.7.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 4e8e76d2b2715019b02b55446e6445a4f12fd16c
4
- data.tar.gz: f4fd83aabdc6286b9c3a64b2fa50ce017eff50fe
2
+ SHA256:
3
+ metadata.gz: 7ac217e07668aaa17e034576c170a3e7736ea6bdedc5776bc05c197ad0604f1a
4
+ data.tar.gz: 6b07f8f169bd0e2e9f3cfd0aa9a9c86d4f4a10dba5f75ebeda44ef4c8b507fd0
5
5
  SHA512:
6
- metadata.gz: d8bfef835c9cf35d2f556e4c731b3e0a396faacc1e7a5e3061b3b9b8d6b9a10efb96ae1d00b1a54aa5c853548eabfa325bd778771ce6d41aa13c97a874ac582b
7
- data.tar.gz: 18dabf89066e0cf69f0763ebfee6ac333af2e27a932bb68003bb09f869911d3ec6e28b73a7f480298ba49e151555b44445f7ea86c3a19368504c35cf47823bf9
6
+ metadata.gz: 25af64ca3571627ccead7c5c873dd72548d6b58997311b22ad3cd459bbd28e78d4238cb17e8dbaf77e2b144dea0b9ecd5a0fae6b4c31519535a794277fc0616a
7
+ data.tar.gz: abaedc7100af6b56597d4faedced8e17704cddc5212c0e2842ffd293642b357f1653e03d89605636b0917788893cf77d55e40aec677d7c6ae899a3fa86c53bc9
data/README.rdoc CHANGED
@@ -69,6 +69,18 @@ you want the colour of the `foo` thing to be either 'red' or 'yellow', change
69
69
  Then when we run `Dibber::Seeder.seed :thing` a new `Thing` will be created
70
70
  with the colour set to either 'red' or 'yellow'.
71
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
+
72
84
  == Report
73
85
 
74
86
  Seeder.report outputs a report detailing start and end time, and a log of how
@@ -115,9 +127,3 @@ If you clone this app, you can run this example at the project root:
115
127
  There is also an example of process log usage:
116
128
 
117
129
  ruby test/examples/process_logs.rb
118
-
119
-
120
-
121
-
122
-
123
-
data/lib/dibber/seeder.rb CHANGED
@@ -8,7 +8,7 @@ module Dibber
8
8
 
9
9
  class << self
10
10
 
11
- def seed(klass, args = {})
11
+ def seed(klass, args = {}, &block)
12
12
  if klass.kind_of?(String) || klass.kind_of?(Symbol)
13
13
  name = klass.to_s.underscore
14
14
  class_name = klass.to_s.strip.classify
@@ -17,7 +17,7 @@ module Dibber
17
17
  name = klass.to_s.underscore
18
18
  end
19
19
  new_file = "#{name.pluralize}.yml"
20
- new(klass, new_file, args).build
20
+ new(klass, new_file, args).build(&block)
21
21
  end
22
22
 
23
23
  def process_log
@@ -72,14 +72,16 @@ module Dibber
72
72
  def build
73
73
  check_objects_exist
74
74
  start_log
75
- objects.each do |name, attributes|
75
+ objects.map do |name, attributes|
76
76
  object = find_or_initialize_by(name)
77
77
  if overwrite or object.new_record?
78
78
  object.send("#{attribute_method}=", attributes)
79
+ yield(object) if block_given?
79
80
  unless object.save
80
81
  self.class.errors << object.errors
81
82
  end
82
83
  end
84
+ object
83
85
  end
84
86
  end
85
87
 
@@ -1,9 +1,11 @@
1
1
  module Dibber
2
- VERSION = "0.6.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
+ #
7
9
  # 0.6.0 ERB parse the source files before passing it to YAML. Allows dynamic
8
10
  # content to be used.
9
11
  #
@@ -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)
@@ -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.6.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: 2017-03-28 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:
@@ -78,7 +78,7 @@ files:
78
78
  - test/test_helper.rb
79
79
  homepage: https://github.com/reggieb/Dibber
80
80
  licenses:
81
- - MIT-LICENSE
81
+ - MIT
82
82
  metadata: {}
83
83
  post_install_message:
84
84
  rdoc_options: []
@@ -95,32 +95,31 @@ required_rubygems_version: !ruby/object:Gem::Requirement
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
97
  requirements: []
98
- rubyforge_project:
99
- rubygems_version: 2.4.8
98
+ rubygems_version: 3.3.22
100
99
  signing_key:
101
100
  specification_version: 4
102
101
  summary: Tool for seeding database from YAML.
103
102
  test_files:
104
- - test/dibber/thing.rb
105
- - test/dibber/seeder_test.rb
106
103
  - test/dibber/dynamic_thing_test.rb
107
104
  - test/dibber/foo/bar.rb
108
105
  - test/dibber/process_log_test.rb
109
- - test/dibber/thing_test.rb
106
+ - test/dibber/seeder_test.rb
110
107
  - test/dibber/seeds/dynamic_things.yml
111
108
  - test/dibber/seeds/empty.yml
112
109
  - test/dibber/seeds/foo/bars.yml
113
110
  - test/dibber/seeds/things.yml
114
- - test/examples/process_logs.rb
115
- - test/examples/models/borough.rb
116
- - test/examples/models/fee.rb
117
- - test/examples/models/disclaimer.rb
111
+ - test/dibber/thing.rb
112
+ - test/dibber/thing_test.rb
118
113
  - test/examples/models/admin_user.rb
114
+ - test/examples/models/borough.rb
119
115
  - test/examples/models/category.rb
120
- - test/examples/seeds.rb
116
+ - test/examples/models/disclaimer.rb
117
+ - test/examples/models/fee.rb
118
+ - test/examples/process_logs.rb
121
119
  - test/examples/seeds/boroughs.yml
122
- - test/examples/seeds/fees.yml
123
120
  - test/examples/seeds/categories.yml
124
121
  - test/examples/seeds/disclaimer/documents.yml
125
- - test/test_helper.rb
122
+ - test/examples/seeds/fees.yml
123
+ - test/examples/seeds.rb
126
124
  - test/not_quite_active_record.rb
125
+ - test/test_helper.rb