built_in_data 0.0.2 → 0.1.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
@@ -12,6 +12,7 @@ It allows developers to deliver, update, and destroy data that is stored in the
12
12
  Add *built_in_key* to your model:
13
13
 
14
14
  ruby script/rails generate migration AddBuiltInKeyToYourModel built_in_key:string
15
+ rake db:migrate
15
16
 
16
17
  Include *BuiltInData* in your model:
17
18
 
@@ -22,19 +23,19 @@ Include *BuiltInData* in your model:
22
23
  Setup your data:
23
24
  There are two methods to load data
24
25
 
25
- * assign to *built_in_data_attributes*
26
- YourModel.built_in_data_attributes = {
26
+ * Pass as a hash to load_built_in_data!
27
+ YourModel.load_built_in_data!({
27
28
  :glacier => {
28
29
  :name => 'Glacier National Park',
29
30
  },
30
-
31
+
31
32
  :yellowstone => {
32
33
  :name => 'Yellowstone National Park',
33
34
  }
34
- }
35
+ })
35
36
 
36
37
 
37
- * OR create a yaml load file in *db/built_in_data* with the name of the model (ie. national_parks.yml)
38
+ * Create a yaml load file in *db/built_in_data* with the name of the model (ie. national_parks.yml), and load the data with `YourModel.load_built_in_data!` without any arguments.
38
39
 
39
40
  glacier:
40
41
  name: Glacier National Park
@@ -42,7 +43,3 @@ There are two methods to load data
42
43
  yellowstone:
43
44
  name: Yellowstone National Park
44
45
 
45
-
46
- Load the data with *load_built_in_data!*:
47
-
48
- YourModel.load_built_in_data!
data/Rakefile CHANGED
@@ -20,14 +20,12 @@ RDoc::Task.new(:rdoc) do |rdoc|
20
20
  rdoc.rdoc_files.include('lib/**/*.rb')
21
21
  end
22
22
 
23
-
24
-
25
-
26
23
  Bundler::GemHelper.install_tasks
27
24
 
28
25
  require 'rake/testtask'
29
26
 
30
27
  Rake::TestTask.new(:test) do |t|
28
+ system 'cd test/dummy; rake db:setup; rake db:test:prepare; cd ../../'
31
29
  t.libs << 'lib'
32
30
  t.libs << 'test'
33
31
  t.pattern = 'test/**/*_test.rb'
@@ -1,3 +1,3 @@
1
1
  module BuiltInData
2
- VERSION = "0.0.2"
2
+ VERSION = "0.1.0"
3
3
  end
data/lib/built_in_data.rb CHANGED
@@ -1,73 +1,44 @@
1
1
  module BuiltInData
2
- # add built_in_key to your model
3
- # generate migration AddBuiltInKeyToYourModel built_in_key:string
4
- #
5
- # require in your model
6
- # require BuiltInData
7
- #
8
- # there are two methods to load data
9
- # 1) assign built_in_data_attributes
10
- # YourModel.built_in_data_attributes = {
11
- # :glacier => {
12
- # :name => 'Glacier National Park',
13
- # },
14
- #
15
- # :yellowstone => {
16
- # :name => 'Yellowstone National Park',
17
- # }
18
- # }
19
- #
20
- # 2) create a yaml load file in db/built_in_data with the name of the model (ie. national_parks.yml)
21
- # glacier:
22
- # name: Glacier National Park
23
- #
24
- # yellowstone:
25
- # name: Yellowstone National Park
26
- #
27
- #
28
- # call load_built_in_data! to load
29
- # YourModel.load_built_in_data!
30
-
31
2
  extend ActiveSupport::Concern
32
-
3
+
33
4
  included do
34
5
  # all built in data objects should have a built_in_key, model objects without a key will not be modified or removed
35
6
  validates_uniqueness_of :built_in_key, :allow_nil => true
36
-
7
+
37
8
  scope :built_in, :conditions => 'built_in_key IS NOT NULL'
38
9
  end
39
-
10
+
40
11
  module ClassMethods
41
- def load_built_in_data!
12
+ def load_built_in_data!(hash = nil)
13
+ objects_hash = prepare_objects_hash(hash)
42
14
  Array.new.tap do |updated_objects|
43
-
44
- built_in_data_attributes.each do |key, attributes|
15
+
16
+ objects_hash.each do |key, attributes|
45
17
  updated_objects << create_or_update!(key, attributes)
46
18
  end
47
-
19
+
48
20
  # destroy any built_in objects that have been removed from built_in_data_attributes
49
21
  self.built_in.each do |object|
50
- object.destroy unless built_in_data_attributes.has_key?(object.built_in_key)
22
+ object.destroy unless objects_hash.has_key?(object.built_in_key)
51
23
  end
52
24
  end
53
25
  end
54
-
55
- def built_in_data_attributes
56
- @built_in_data_attributes ||= load_yaml_data
57
- end
58
-
59
- def built_in_data_attributes=(attributes)
60
- @built_in_data_attributes = (attributes.respond_to?(:with_indifferent_access) ? attributes.with_indifferent_access : attributes)
61
- end
62
-
63
-
26
+
64
27
  #######
65
28
  private
66
29
  #######
67
-
30
+
31
+ def prepare_objects_hash(hash)
32
+ return hash.nil? ? load_yaml_data : hash.with_indifferent_access
33
+ end
34
+
35
+ def load_yaml_data
36
+ YAML.load_file(Rails.root.join('db', 'built_in_data', "#{table_name}.yml"))
37
+ end
38
+
68
39
  def create_or_update!(key, attributes)
69
40
  attributes.merge!(:built_in_key => key.to_s)
70
-
41
+
71
42
  object = find_by_built_in_key(key)
72
43
  if object
73
44
  object.update_attributes!(attributes)
@@ -76,9 +47,6 @@ module BuiltInData
76
47
  result = create!(attributes)
77
48
  end
78
49
  end
79
-
80
- def load_yaml_data
81
- YAML.load_file(Rails.root.join('db', 'built_in_data', "#{table_name}.yml"))
82
- end
50
+
83
51
  end
84
52
  end
@@ -1,90 +1,71 @@
1
1
  require 'test_helper'
2
2
 
3
3
  class BuiltInDataTest < ActiveSupport::TestCase
4
-
4
+ HASH_DATA = {
5
+ :test => {
6
+ :name => 'Yellowstone National Park',
7
+ :established => '1872-03-01',
8
+ :url => 'http://www.nps.gov/yell/index.htm'
9
+ }
10
+ }
11
+
5
12
  test "should load built in data" do
6
13
  assert_difference 'NationalPark.count' do
7
- load_test_data
14
+ load_hash_data
8
15
  end
9
-
16
+
10
17
  assert_equal 'Yellowstone National Park', NationalPark.find_by_built_in_key('test').name
11
18
  end
12
-
19
+
13
20
  test "should remove built in data" do
14
- load_test_data
15
- NationalPark.built_in_data_attributes = {}
16
-
21
+ load_hash_data
22
+
17
23
  assert_difference 'NationalPark.count', -1 do
18
- NationalPark.load_built_in_data!
24
+ NationalPark.load_built_in_data!({})
19
25
  end
20
-
26
+
21
27
  assert_nil NationalPark.find_by_built_in_key('test')
22
28
  end
23
-
24
- test "should not add or remove records" do
25
- load_test_data
26
-
27
- assert_no_difference "NationalPark.count" do
28
- NationalPark.load_built_in_data!
29
- end
30
- end
31
-
29
+
32
30
  test "should not remove a record without a built_in_key" do
33
31
  park = NationalPark.create(:name => 'Testing')
34
-
32
+
35
33
  assert_difference 'NationalPark.count', 1 do
36
- load_test_data
34
+ load_hash_data
37
35
  end
38
36
  assert_equal 'Testing', park.reload.name
39
37
  end
40
-
38
+
41
39
  test "should update existing built in data" do
42
- load_test_data
43
- NationalPark.built_in_data_attributes[:test][:url] = 'http://en.wikipedia.org/wiki/Yellowstone_National_Park'
44
-
45
- NationalPark.load_built_in_data!
40
+ load_hash_data
41
+ assert_not_equal 'http://en.wikipedia.org/wiki/Yellowstone_National_Park', NationalPark.find_by_built_in_key('test').url
42
+ modified_hash = HASH_DATA.dup
43
+ modified_hash[:test][:url] = 'http://en.wikipedia.org/wiki/Yellowstone_National_Park'
44
+ NationalPark.load_built_in_data!(modified_hash)
46
45
  assert_equal 'http://en.wikipedia.org/wiki/Yellowstone_National_Park', NationalPark.find_by_built_in_key('test').url
47
46
  end
48
-
49
-
50
- # loading from a yaml file
51
- test "should load data from yaml file" do
52
- NationalPark.built_in_data_attributes = nil
53
- assert_equal 2, NationalPark.built_in_data_attributes.length
54
- end
55
-
47
+
56
48
  test "should load built in data from yaml file" do
57
- NationalPark.built_in_data_attributes = nil
58
-
59
49
  assert_difference 'NationalPark.count', 2 do
60
50
  NationalPark.load_built_in_data!
61
51
  end
62
52
  end
63
-
53
+
64
54
  test "should load elements from file only once" do
65
- NationalPark.built_in_data_attributes = nil
66
55
  NationalPark.load_built_in_data!
67
- NationalPark.built_in_data_attributes = nil
68
-
56
+
69
57
  assert_no_difference 'NationalPark.count' do
70
58
  NationalPark.load_built_in_data!
71
59
  end
72
60
  end
73
-
74
-
61
+
62
+
75
63
  #######
76
64
  private
77
65
  #######
78
-
79
- def load_test_data
80
- NationalPark.built_in_data_attributes = {
81
- :test => {
82
- :name => 'Yellowstone National Park',
83
- :established => '1872-03-01',
84
- :url => 'http://www.nps.gov/yell/index.htm'
85
- }
86
- }
87
-
88
- NationalPark.load_built_in_data!
66
+
67
+ def load_hash_data
68
+ NationalPark.load_built_in_data!(HASH_DATA)
89
69
  end
70
+
90
71
  end
Binary file
@@ -17,9 +17,9 @@ ActiveRecord::Schema.define(:version => 20121024201818) do
17
17
  t.string "name"
18
18
  t.date "established"
19
19
  t.string "url"
20
- t.string "built_in_key"
21
20
  t.datetime "created_at", :null => false
22
21
  t.datetime "updated_at", :null => false
22
+ t.string "built_in_key"
23
23
  end
24
24
 
25
25
  end
Binary file
@@ -1,17 +1,75 @@
1
1
  Connecting to database specified by database.yml
2
+  (20.1ms) select sqlite_version(*)
3
+  (147.2ms) CREATE TABLE "national_parks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "established" date, "url" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "built_in_key" varchar(255))
4
+  (108.1ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL) 
5
+  (0.1ms) PRAGMA index_list("schema_migrations")
6
+  (91.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
7
+  (0.1ms) SELECT version FROM "schema_migrations"
8
+  (124.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024201818')
9
+  (108.5ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024195810')
10
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
2
11
  Connecting to database specified by database.yml
3
-  (0.1ms) select sqlite_version(*)
4
-  (14.7ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
5
-  (0.0ms) PRAGMA index_list("schema_migrations")
6
-  (5.5ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
7
-  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
8
- Migrating to CreateNationalParks (20121024195810)
9
-  (0.0ms) begin transaction
10
-  (0.4ms) CREATE TABLE "national_parks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "established" date, "url" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) 
11
-  (0.1ms) INSERT INTO "schema_migrations" ("version") VALUES ('20121024195810')
12
-  (1.3ms) commit transaction
12
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
13
13
   (0.2ms) select sqlite_version(*)
14
+  (145.0ms) CREATE TABLE "national_parks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "established" date, "url" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "built_in_key" varchar(255)) 
15
+  (133.2ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
16
+  (0.1ms) PRAGMA index_list("schema_migrations")
17
+  (116.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
18
+  (0.1ms) SELECT version FROM "schema_migrations"
19
+  (116.1ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024201818')
20
+  (117.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024195810')
21
+ Connecting to database specified by database.yml
22
+  (19.0ms) select sqlite_version(*)
23
+  (195.5ms) DROP TABLE "national_parks"
24
+  (116.6ms) CREATE TABLE "national_parks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "established" date, "url" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "built_in_key" varchar(255)) 
25
+  (0.1ms) SELECT version FROM "schema_migrations"
14
26
   (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
15
-  (0.0ms) PRAGMA index_list("national_parks")
16
27
  Connecting to database specified by database.yml
28
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
29
+  (0.3ms) select sqlite_version(*)
30
+  (160.3ms) CREATE TABLE "national_parks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "established" date, "url" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "built_in_key" varchar(255)) 
31
+  (116.5ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
32
+  (0.1ms) PRAGMA index_list("schema_migrations")
33
+  (108.4ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
34
+  (0.1ms) SELECT version FROM "schema_migrations"
35
+  (149.4ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024201818')
36
+  (92.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024195810')
37
+ Connecting to database specified by database.yml
38
+  (18.3ms) select sqlite_version(*)
39
+  (137.1ms) DROP TABLE "national_parks"
40
+  (116.6ms) CREATE TABLE "national_parks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "established" date, "url" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "built_in_key" varchar(255)) 
41
+  (0.1ms) SELECT version FROM "schema_migrations"
42
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
17
43
  Connecting to database specified by database.yml
44
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
45
+  (0.3ms) select sqlite_version(*)
46
+  (135.2ms) CREATE TABLE "national_parks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "established" date, "url" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "built_in_key" varchar(255)) 
47
+  (116.5ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
48
+  (0.1ms) PRAGMA index_list("schema_migrations")
49
+  (91.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
50
+  (0.1ms) SELECT version FROM "schema_migrations"
51
+  (83.0ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024201818')
52
+  (118.3ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024195810')
53
+ Connecting to database specified by database.yml
54
+  (0.1ms) select sqlite_version(*)
55
+  (127.3ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
56
+  (0.1ms) PRAGMA index_list("schema_migrations")
57
+  (99.9ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
58
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
59
+ Connecting to database specified by database.yml
60
+  (18.7ms) select sqlite_version(*)
61
+  (152.0ms) CREATE TABLE "national_parks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "established" date, "url" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "built_in_key" varchar(255))
62
+  (0.1ms) SELECT version FROM "schema_migrations"
63
+  (117.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024201818')
64
+  (116.9ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024195810')
65
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations"
66
+ Connecting to database specified by database.yml
67
+  (0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" 
68
+  (0.2ms) select sqlite_version(*)
69
+  (142.2ms) CREATE TABLE "national_parks" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "established" date, "url" varchar(255), "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL, "built_in_key" varchar(255)) 
70
+  (116.4ms) CREATE TABLE "schema_migrations" ("version" varchar(255) NOT NULL)
71
+  (0.1ms) PRAGMA index_list("schema_migrations")
72
+  (91.7ms) CREATE UNIQUE INDEX "unique_schema_migrations" ON "schema_migrations" ("version")
73
+  (0.1ms) SELECT version FROM "schema_migrations"
74
+  (91.6ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024201818')
75
+  (83.1ms) INSERT INTO "schema_migrations" (version) VALUES ('20121024195810')