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.
@@ -1,3 +1,8 @@
1
+ # 0.3.0
2
+
3
+ * Add a rails generator for creating a new static data model.
4
+ * Update docs to demonstrate `update` rake task and new generator.
5
+
1
6
  # 0.2.0
2
7
 
3
8
  * Add support for incrementally updating tables rather than merely deleting all
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, you can't referencing models
10
- in migrations can get you into trouble, and it can be awkward to
11
- update static data in migrations later.
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
- ## Usage
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 each model that you want to store static for. The
39
- file should be named after the model. Implement two class methods, `columns`
40
- and `rows`. The `columns` method should return the names of the columns in the
41
- order their data appears in the `rows`
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
- Install your static data:
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
@@ -0,0 +1,12 @@
1
+ class Static<%= class_name %> < StaticData::Base
2
+ def self.columns
3
+ [:col1, :col2, :col3]
4
+ end
5
+
6
+ def self.rows
7
+ [
8
+ ['col1value1', 'col2value1', 'col3value1'],
9
+ ['col1value2', 'col2value2', 'col3value2'],
10
+ ]
11
+ end
12
+ end
@@ -1,6 +1,6 @@
1
1
  require 'static-data'
2
2
  require 'rails'
3
- module YourGem
3
+ module StaticData
4
4
  class Railtie < Rails::Railtie
5
5
  rake_tasks do
6
6
  require 'rake'
@@ -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}: updating (%0.4fs)") do
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
@@ -1,3 +1,3 @@
1
1
  module StaticData
2
- VERSION = "0.2.0"
2
+ VERSION = "0.3.0"
3
3
  end
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.2.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-05-20 00:00:00.000000000 Z
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