static-data 0.2.0 → 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +5 -0
- data/README.md +39 -11
- data/lib/generators/static_data/create_generator.rb +16 -0
- data/lib/generators/static_data/templates/static-data-subclass.erb +12 -0
- data/lib/static-data/railtie.rb +1 -1
- data/lib/static-data/tasks.rake +1 -1
- data/lib/static-data/version.rb +1 -1
- metadata +4 -2
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -6,9 +6,9 @@ useful for pre-populating lookup tables and the like. It consists of
|
|
6
6
|
a simple framework for describing your static data and a Rake task
|
7
7
|
for ensuring that your static data is installed in your database.
|
8
8
|
|
9
|
-
While you can use migrations to do this,
|
10
|
-
|
11
|
-
|
9
|
+
While you can use migrations to do this, referencing models in migrations
|
10
|
+
can get you into trouble, and it can be awkward to update static data in
|
11
|
+
migrations later.
|
12
12
|
|
13
13
|
It doesn't do much, but it's awfully handy to have a simple way of
|
14
14
|
adding lookup table data to your database. And with StaticData, you
|
@@ -29,19 +29,42 @@ Or install it yourself as:
|
|
29
29
|
|
30
30
|
$ gem install static-data
|
31
31
|
|
32
|
-
##
|
32
|
+
## Setting up Static Data for a table
|
33
|
+
|
34
|
+
### Using the Generator
|
35
|
+
|
36
|
+
Run the generator with the name of the model you want to build static data for
|
37
|
+
(in the example below, ImageType):
|
38
|
+
|
39
|
+
rails generate static_data:create ImageType
|
40
|
+
|
41
|
+
### By Hand
|
33
42
|
|
34
43
|
Create a db/static-data directory in your app:
|
35
44
|
|
36
45
|
mkdir db/static-data
|
37
46
|
|
38
|
-
Create a file for
|
39
|
-
|
40
|
-
|
41
|
-
|
47
|
+
Create a file for the model that you want to store static data for, named
|
48
|
+
after the model. The class name should match the model name, but with the word
|
49
|
+
"Static" prepended (eg: Static\<ModelName\>). Implement two class methods,
|
50
|
+
`columns` and `rows`.
|
51
|
+
|
52
|
+
cat > db/static-data/image_type.rb <<EOF
|
53
|
+
# For a model named ImageType:
|
54
|
+
class StaticImageType < StaticData::Base
|
55
|
+
def self.columns
|
56
|
+
end
|
57
|
+
|
58
|
+
def self.row
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
## Describe your Static Data
|
63
|
+
|
64
|
+
The `columns` method should return the names of the columns in the order their
|
65
|
+
data appears in the `rows`
|
42
66
|
|
43
67
|
# For a model named ImageType:
|
44
|
-
cat > db/static-data/image_type.rb <<EOF
|
45
68
|
class StaticImageType < StaticData::Base
|
46
69
|
def self.columns
|
47
70
|
[:name, :mime_type, :extension]
|
@@ -54,9 +77,14 @@ order their data appears in the `rows`
|
|
54
77
|
]
|
55
78
|
end
|
56
79
|
end
|
57
|
-
EOF
|
58
80
|
|
59
|
-
|
81
|
+
## Getting Your Static Data into the Database
|
82
|
+
|
83
|
+
Update your database with your static data:
|
84
|
+
|
85
|
+
rake static-data:update
|
86
|
+
|
87
|
+
Or replace all existing data in the static data tables with your static data:
|
60
88
|
|
61
89
|
rake static-data:install
|
62
90
|
|
@@ -0,0 +1,16 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/named_base'
|
3
|
+
|
4
|
+
module StaticData
|
5
|
+
module Generators
|
6
|
+
class CreateGenerator < Rails::Generators::NamedBase
|
7
|
+
desc "This generator creates a static-data file at db/static-data/<file_name>"
|
8
|
+
|
9
|
+
source_root File.expand_path("../templates", __FILE__)
|
10
|
+
|
11
|
+
def create_initializer_file
|
12
|
+
template "static-data-subclass.erb", "db/static-data/#{file_name}.rb"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
data/lib/static-data/railtie.rb
CHANGED
data/lib/static-data/tasks.rake
CHANGED
@@ -27,7 +27,7 @@ namespace "static-data" do
|
|
27
27
|
|
28
28
|
StaticData.static_data_classes(Rails.root) do |static_data_class|
|
29
29
|
StaticData.report_duration("== #{static_data_class}: updating",
|
30
|
-
"== #{static_data_class}:
|
30
|
+
"== #{static_data_class}: updated (%0.4fs)") do
|
31
31
|
|
32
32
|
StaticData.report_duration("-- update", " -> %0.4fs") do
|
33
33
|
results = static_data_class.update
|
data/lib/static-data/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: static-data
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.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: 2013-
|
12
|
+
date: 2013-06-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -88,6 +88,8 @@ files:
|
|
88
88
|
- LICENSE.txt
|
89
89
|
- README.md
|
90
90
|
- Rakefile
|
91
|
+
- lib/generators/static_data/create_generator.rb
|
92
|
+
- lib/generators/static_data/templates/static-data-subclass.erb
|
91
93
|
- lib/static-data.rb
|
92
94
|
- lib/static-data/base.rb
|
93
95
|
- lib/static-data/railtie.rb
|