application_seeds 0.4.3 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ v0.5.0, 2014-02-28
2
+ ------------------
3
+
4
+ * Added support for config values.
5
+
6
+
1
7
  v0.4.3, 2014-02-22
2
8
  ------------------
3
9
 
data/README.md CHANGED
@@ -322,6 +322,56 @@ specified.
322
322
  See `spec/seed_data/test_data_set` for more examples of seed data files.
323
323
 
324
324
 
325
+ ## Config Values
326
+
327
+ Since the YAML files are first run through ERB, you are able to sprinkle
328
+ ruby code throughout your seed data files. This allows you to do some
329
+ interesting things:
330
+
331
+ ```ruby
332
+ <% 10.times do |x| %>
333
+ company_<%= x %>:
334
+ name: Company_<%= x %>
335
+ <% end %>
336
+ ```
337
+
338
+ But `10` here is a magic number. It would be better if we had a
339
+ variable that better communicated its use.
340
+
341
+ `ApplicationSeeds` allows you to place a `_config.yml` file in each
342
+ dataset directory. The data in this file is loaded, and made available
343
+ via the `ApplicationSeeds.config_value` API.
344
+
345
+ Take the following `_config.yml`:
346
+
347
+ ```ruby
348
+ num_companies: 5
349
+ num_people: 1
350
+ num_departments: 3
351
+ ```
352
+
353
+ You can fetch these values by calling `ApplicationSeeds.config_value`:
354
+
355
+ ```ruby
356
+ ApplicationSeeds.config_value(:num_companies)
357
+ => 5
358
+
359
+ ApplicationSeeds.config_value(:num_people)
360
+ => 1
361
+
362
+ ApplicationSeeds.config_value(:num_departments)
363
+ => 3
364
+ ```
365
+
366
+ ### Merging config value files
367
+
368
+ If you are using nested datasets, then all of the appropriate
369
+ `_config.yml` files will be loaded, and all data in those files
370
+ will be available. Config values defined in the lower levels
371
+ are given precedence if there is a naming conflict, allowing the
372
+ lower levels to override values specified in the upper levels.
373
+
374
+
325
375
  ## Configuration
326
376
 
327
377
  The `ApplicationSeeds` module can generate integer or UUID ids. You can
@@ -504,6 +554,17 @@ are unable to insert new data into the databse after your dataset has
504
554
  been imported, then this should correct them.
505
555
 
506
556
 
557
+ ### Fetch data from the `_config.yml` files
558
+
559
+ ```ruby
560
+ ApplicationSeeds.config_value(:foo)
561
+ ```
562
+
563
+ Fetch the value for the key named `foo` that is defined in the
564
+ `_config.yml` config values files. Will return nil if no config value
565
+ could be found by that name.
566
+
567
+
507
568
  ## The Problem
508
569
 
509
570
  Applications in a service oriented architecture (SOA) are often
@@ -1,3 +1,3 @@
1
1
  module ApplicationSeeds
2
- VERSION = "0.4.3"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -10,74 +10,10 @@ require "application_seeds/database"
10
10
  require "application_seeds/version"
11
11
  require "application_seeds/attributes"
12
12
 
13
- # A library for managing a standardized set of seed data for applications in a non-production environment.
14
- #
15
- # == The API
16
- #
17
- # === Fetching all seeds of a given type
18
- #
19
- # ApplicationSeeds.campaigns # where "campaigns" is the name of the seed file
20
- #
21
- # This call returns a hash with one or more entries (depending on the contentes of the seed file).
22
- # The IDs of the object are the keys, and a hash containing the object's attributes are the values.
23
- # An exception is raised if no seed data could be with the given name.
24
- #
25
- # === Fetching seed data by label
26
- #
27
- # ApplicationSeeds.campaigns(:label) # where "campaigns" is the name of the seed file, and :label is the label of the campaign
28
- #
29
- # This call returns a hash containing the object's attributes. An exception is raised if no
30
- # seed data could be found with the given label.
31
- #
32
- # === Fetching seed data by id
33
- #
34
- # ApplicationSeeds.campaigns(12345) # where "campaigns" is the name of the seed file, and 12345 is the id of the campaign
35
- #
36
- # This call returns a hash containing the object's attributes. An exception is raised if no
37
- # seed data could be found with the given id.
38
- #
39
- # === Fetching seed data by some other attribute
40
- #
41
- # ApplicationSeeds.campaigns(foo: 'bar', name: 'John') # where "campaigns" is the name of the seed file
42
- #
43
- # This call returns the seed data that contains the specified attributes,
44
- # and the specified attribute values. It returns a hash with zero or more
45
- # entries. The IDs of the object are the keys of the hash, and a hash
46
- # containing the object's attributes are the values. Any empty hash will
47
- # be returned if no seed data could be found with the given attribute names
48
- # and values.
49
- #
50
- # === Creating an object
51
- #
52
- # ApplicationSeeds.create_object!(Campaign, id, attributes)
53
- #
54
- # This call will create a new instance of the <tt>Campaign</tt> class, with the
55
- # specified id and attributes.
56
- #
57
- # === Rejecting specific attributes
58
13
  #
59
- # ApplicationSeeds.create_object!(Campaign, id, attributes.reject_attributes(:unused_attribute))
60
- #
61
- # This call will create a new instance of the <tt>Campaign</tt> class without the
62
- # <tt>unused_attribute</tt> attribute.
63
- #
64
- # === Selecting specific attributes
65
- #
66
- # ApplicationSeeds.create_object!(Campaign, id, attributes.select_attributes(:attribute1, :attribute2))
67
- #
68
- # This call will create a new instance of the <tt>Campaign</tt> class with only the
69
- # <tt>attribute1</tt> and <tt>attribute2</tt> attributes.
70
- #
71
- # === Mapping attribute names
72
- #
73
- # ApplicationSeeds.create_object!(Campaign, id, attributes.map_attributes(
74
- # :old_name1 => :new_name1, :old_name2 => :new_name2))
14
+ # A library for managing a standardized set of seed data for applications in a non-production environment.
75
15
  #
76
- # This call will create a new instance of the <tt>Campaign</tt> class, using the
77
- # seed data for old_name1 as the attribute value for new_name1, and the
78
- # seed data for old_name2 as the attribute value for new_name2. This
79
- # method let's you easly account for slight differences is attribute names
80
- # across applications.
16
+ # See README.md for API documentation.
81
17
  #
82
18
  module ApplicationSeeds
83
19
  class << self
@@ -97,6 +33,13 @@ module ApplicationSeeds
97
33
  @_config ||= { :id_type => :integer }
98
34
  end
99
35
 
36
+ #
37
+ # Fetch data from the _config.yml files.
38
+ #
39
+ def config_value(key)
40
+ config_values[key.to_s]
41
+ end
42
+
100
43
  #
101
44
  # Specify the name of the gem that contains the application seed data.
102
45
  #
@@ -199,7 +142,7 @@ module ApplicationSeeds
199
142
  # been imported, then this should correct them.
200
143
  #
201
144
  def reset_sequence_numbers
202
- result = Database.connection.exec("SELECT table_name FROM information_schema.tables WHERE table_schema = 'public';")
145
+ result = Database.connection.exec("SELECT table_name FROM information_schema.tables WHERE table_schema NOT IN ('pg_catalog', 'information_schema')")
203
146
  table_names = result.map { |row| row.values_at('table_name')[0] }
204
147
 
205
148
  table_names_with_id_column = table_names.select do |table_name|
@@ -244,7 +187,8 @@ module ApplicationSeeds
244
187
  @seed_data_files = []
245
188
  path = dataset_path(@dataset)
246
189
  while (seed_data_path != path) do
247
- @seed_data_files.concat(Dir[File.join(path, "*.yml")])
190
+ files = Dir[File.join(path, "*.yml")].reject { |file| file =~ /\/_config.yml$/ }
191
+ @seed_data_files.concat(files)
248
192
  path.sub!(/\/[^\/]+$/, "")
249
193
  end
250
194
  @seed_data_files
@@ -263,6 +207,20 @@ module ApplicationSeeds
263
207
  @raw_seed_data
264
208
  end
265
209
 
210
+ def config_values
211
+ return @config_values unless @config_values.nil?
212
+
213
+ @config_values = {}
214
+ path = dataset_path(@dataset)
215
+ while (seed_data_path != path) do
216
+ config_file = Dir[File.join(path, "_config.yml")].first
217
+ values = config_file.nil? ? {} : YAML.load(ERB.new(File.read(config_file)).result)
218
+ @config_values = values.merge(@config_values)
219
+ path.sub!(/\/[^\/]+$/, "")
220
+ end
221
+ @config_values
222
+ end
223
+
266
224
  def seed_labels
267
225
  return @seed_labels unless @seed_labels.nil?
268
226
 
@@ -428,6 +386,7 @@ module ApplicationSeeds
428
386
  @processed_seed_data = nil
429
387
  @raw_seed_data = nil
430
388
  @seed_data_files = nil
389
+ @config_values = nil
431
390
  end
432
391
  end
433
392
  end
@@ -237,6 +237,16 @@ describe "ApplicationSeeds" do
237
237
  end
238
238
  end
239
239
  end
240
+
241
+ describe "config values" do
242
+ it "can fetch config values for the dataset" do
243
+ expect(ApplicationSeeds.config_value(:num_companies)).to eql(15)
244
+ expect(ApplicationSeeds.config_value(:num_people)).to eql(100)
245
+ end
246
+ it "returns nil if no config value could be found by that name" do
247
+ expect(ApplicationSeeds.config_value(:whaa)).to be_nil
248
+ end
249
+ end
240
250
  end
241
251
 
242
252
  describe "with a nested dataset" do
@@ -283,6 +293,16 @@ describe "ApplicationSeeds" do
283
293
  expect(person['first_name']).to eql("Ken")
284
294
  end
285
295
  end
296
+
297
+ describe "merging config values" do
298
+ it "can merge data from different levels" do
299
+ expect(ApplicationSeeds.config_value(:num_companies)).to eql(5)
300
+ expect(ApplicationSeeds.config_value(:num_departments)).to eql(3)
301
+ end
302
+ it "gives the data in lower levels precendence" do
303
+ expect(ApplicationSeeds.config_value(:num_people)).to eql(10)
304
+ end
305
+ end
286
306
  end
287
307
 
288
308
  describe "with UUIDs configured for all seed types" do
@@ -0,0 +1,2 @@
1
+ num_companies: 15
2
+ num_people: 100
@@ -0,0 +1,3 @@
1
+ num_companies: 5
2
+ num_people: 1
3
+ num_departments: 3
@@ -0,0 +1,2 @@
1
+ num_companies: 5
2
+ num_people: 10
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: application_seeds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.5.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: 2014-02-22 00:00:00.000000000 Z
12
+ date: 2014-02-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -98,11 +98,14 @@ files:
98
98
  - lib/application_seeds/version.rb
99
99
  - spec/application_seeds_spec.rb
100
100
  - spec/attributes_spec.rb
101
+ - spec/seed_data/test_data_set/_config.yml
101
102
  - spec/seed_data/test_data_set/companies.yml
102
103
  - spec/seed_data/test_data_set/departments.yml
103
104
  - spec/seed_data/test_data_set/people.yml
105
+ - spec/seed_data/test_inheritable_data_set/_config.yml
104
106
  - spec/seed_data/test_inheritable_data_set/companies.yml
105
107
  - spec/seed_data/test_inheritable_data_set/level_2/departments.yml
108
+ - spec/seed_data/test_inheritable_data_set/level_2/level_3/_config.yml
106
109
  - spec/seed_data/test_inheritable_data_set/level_2/level_3/people.yml
107
110
  - spec/seed_data/test_inheritable_data_set/level_2/people.yml
108
111
  homepage: https://github.com/centro/application_seeds
@@ -118,12 +121,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
118
121
  - - ! '>='
119
122
  - !ruby/object:Gem::Version
120
123
  version: '0'
124
+ segments:
125
+ - 0
126
+ hash: 632668571274566106
121
127
  required_rubygems_version: !ruby/object:Gem::Requirement
122
128
  none: false
123
129
  requirements:
124
130
  - - ! '>='
125
131
  - !ruby/object:Gem::Version
126
132
  version: '0'
133
+ segments:
134
+ - 0
135
+ hash: 632668571274566106
127
136
  requirements: []
128
137
  rubyforge_project:
129
138
  rubygems_version: 1.8.23
@@ -134,11 +143,13 @@ summary: A library for managing a standardized set of seed data for applications
134
143
  test_files:
135
144
  - spec/application_seeds_spec.rb
136
145
  - spec/attributes_spec.rb
146
+ - spec/seed_data/test_data_set/_config.yml
137
147
  - spec/seed_data/test_data_set/companies.yml
138
148
  - spec/seed_data/test_data_set/departments.yml
139
149
  - spec/seed_data/test_data_set/people.yml
150
+ - spec/seed_data/test_inheritable_data_set/_config.yml
140
151
  - spec/seed_data/test_inheritable_data_set/companies.yml
141
152
  - spec/seed_data/test_inheritable_data_set/level_2/departments.yml
153
+ - spec/seed_data/test_inheritable_data_set/level_2/level_3/_config.yml
142
154
  - spec/seed_data/test_inheritable_data_set/level_2/level_3/people.yml
143
155
  - spec/seed_data/test_inheritable_data_set/level_2/people.yml
144
- has_rdoc: