static-data 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +80 -0
- data/Rakefile +1 -0
- data/lib/static-data.rb +6 -0
- data/lib/static-data/base.rb +27 -0
- data/lib/static-data/railtie.rb +10 -0
- data/lib/static-data/tasks.rake +31 -0
- data/lib/static-data/version.rb +3 -0
- data/spec/static_data_spec.rb +21 -0
- data/static-data.gemspec +29 -0
- metadata +125 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Benjamin Turner
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
# StaticData
|
2
|
+
|
3
|
+
StaticData is a Rails plugin that lets you easily manage fixed data
|
4
|
+
that you want to always have in your database, and is primarily
|
5
|
+
useful for pre-populating lookup tables and the like. It consists of
|
6
|
+
a simple framework for describing your static data and a Rake task
|
7
|
+
for ensuring that your static data is installed in your database.
|
8
|
+
|
9
|
+
It doesn't do much, but it's awfully handy to have a simple way of
|
10
|
+
adding lookup table data to your database. Anything that makes it
|
11
|
+
easier to set up a brand new database from scratch is a win in my
|
12
|
+
book.
|
13
|
+
|
14
|
+
## Installation
|
15
|
+
|
16
|
+
Add this line to your application's Gemfile:
|
17
|
+
|
18
|
+
gem 'static-data'
|
19
|
+
|
20
|
+
And then execute:
|
21
|
+
|
22
|
+
$ bundle
|
23
|
+
|
24
|
+
Or install it yourself as:
|
25
|
+
|
26
|
+
$ gem install static-data
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
Create a db/static-data directory in your app:
|
31
|
+
|
32
|
+
mkdir db/static-data
|
33
|
+
|
34
|
+
Create a file for each model that you want to store static for. The
|
35
|
+
file should be named after the model:
|
36
|
+
|
37
|
+
# For a model named ImageType:
|
38
|
+
cat > db/static-data/image_type.rb <<EOF
|
39
|
+
class StaticImageType < StaticData::Base
|
40
|
+
def self.columns
|
41
|
+
[:name, :mime_type, :extension]
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.row
|
45
|
+
[
|
46
|
+
['PNG', 'image/png', 'png'],
|
47
|
+
['JPEG', 'image/jpeg', 'jpg'],
|
48
|
+
]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
EOF
|
52
|
+
|
53
|
+
Install your static data:
|
54
|
+
|
55
|
+
rake static-data:install
|
56
|
+
|
57
|
+
Enjoy!
|
58
|
+
|
59
|
+
## Limitations
|
60
|
+
|
61
|
+
The current static-data:install task blows away any existing data
|
62
|
+
already present in the model's table before installing, so any
|
63
|
+
non-static data in the table will be nuked. It would be nice to
|
64
|
+
offer another task that simply ensures that all static data is
|
65
|
+
present without deleting other data. I consider that a TODO, but
|
66
|
+
am happy to receive pull requests.
|
67
|
+
|
68
|
+
If you have existing data in your tables and use foreign key
|
69
|
+
constraints (which do go hand-in-hand with lookup tables), you
|
70
|
+
probably don't want to run this rake task, because it will either
|
71
|
+
fail or cause delete cascades, depending on how you've set up your
|
72
|
+
constraints. Fixing the above item should hopefully fix this, too.
|
73
|
+
|
74
|
+
## Contributing
|
75
|
+
|
76
|
+
1. Fork it
|
77
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
78
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
79
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
80
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/lib/static-data.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
module StaticData
|
2
|
+
class Base
|
3
|
+
def self.set_model_class(other_model_class)
|
4
|
+
@model_class_name = other_model_class
|
5
|
+
end
|
6
|
+
|
7
|
+
def self.model_class
|
8
|
+
model_class_name = @model_class_name || self.name.sub(/^Static/, '')
|
9
|
+
Object.const_get(model_class_name)
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.reset
|
13
|
+
self.model_class.delete_all
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.install
|
17
|
+
cols = self.columns
|
18
|
+
row_class = self.model_class
|
19
|
+
rows.each do |row|
|
20
|
+
row_class.create!(Hash[cols.zip(row)], :without_protection => true)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
# def self.update
|
25
|
+
# end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
namespace "static-data" do
|
2
|
+
desc "Install all static data from db/static-data"
|
3
|
+
task :install => :environment do
|
4
|
+
require "static-data"
|
5
|
+
|
6
|
+
Dir.glob('db/static-data/*') do |file|
|
7
|
+
basename = File.basename(file).split('.', 2).first
|
8
|
+
expected_class_name = "Static" + basename.
|
9
|
+
camelize
|
10
|
+
|
11
|
+
start = Time.now
|
12
|
+
puts "== #{expected_class_name}: installing"
|
13
|
+
|
14
|
+
require "#{Rails.root}/db/static-data/#{basename}"
|
15
|
+
Object.const_get(expected_class_name).tap do |static_data_class|
|
16
|
+
step_start = Time.now
|
17
|
+
puts "-- reset"
|
18
|
+
static_data_class.reset
|
19
|
+
puts " -> %0.4fs" % [(Time.now - step_start)]
|
20
|
+
|
21
|
+
step_start = Time.now
|
22
|
+
puts "-- install"
|
23
|
+
static_data_class.install
|
24
|
+
puts " -> %0.4fs" % [(Time.now - step_start)]
|
25
|
+
end
|
26
|
+
|
27
|
+
puts "== #{expected_class_name}: installed (%0.4fs)" % [(Time.now - start)]
|
28
|
+
puts
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'static-data'
|
2
|
+
|
3
|
+
describe StaticData::Base do
|
4
|
+
it "model_class returns class without Static in it" do
|
5
|
+
class FooBar; end
|
6
|
+
class StaticFooBar < StaticData::Base; end
|
7
|
+
|
8
|
+
class_obj = Object.const_get('StaticFooBar')
|
9
|
+
class_obj.model_class.should eql(FooBar)
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'model_class returns alternate class if set_model_class is used' do
|
13
|
+
class Bazzle; end
|
14
|
+
class StaticFooBar < StaticData::Base
|
15
|
+
set_model_class 'Bazzle'
|
16
|
+
end
|
17
|
+
|
18
|
+
class_obj = Object.const_get('StaticFooBar')
|
19
|
+
class_obj.model_class.should eql(Bazzle)
|
20
|
+
end
|
21
|
+
end
|
data/static-data.gemspec
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'static-data/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "static-data"
|
8
|
+
spec.version = StaticData::VERSION
|
9
|
+
spec.authors = ["Ben Turner"]
|
10
|
+
spec.email = ["bjturner@outofcoffee.com"]
|
11
|
+
spec.description = %q{Rails plugin to manage static data in ActiveRecord
|
12
|
+
models/tables}
|
13
|
+
spec.summary = %q{StaticData is a Rails plugin that lets you easily
|
14
|
+
manage fixed data that you want to always have in your database, and
|
15
|
+
is primarily useful for pre-populating lookup tables and the like.}
|
16
|
+
spec.homepage = ""
|
17
|
+
spec.license = "MIT"
|
18
|
+
|
19
|
+
spec.files = `git ls-files`.split($/)
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "rake"
|
25
|
+
|
26
|
+
spec.add_development_dependency "bundler", "~> 1.3"
|
27
|
+
spec.add_development_dependency "rake"
|
28
|
+
spec.add_development_dependency "rspec", "~> 2.6"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: static-data
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Ben Turner
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-05-13 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: bundler
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: '1.3'
|
38
|
+
type: :development
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: '1.3'
|
46
|
+
- !ruby/object:Gem::Dependency
|
47
|
+
name: rake
|
48
|
+
requirement: !ruby/object:Gem::Requirement
|
49
|
+
none: false
|
50
|
+
requirements:
|
51
|
+
- - ! '>='
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
none: false
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
- !ruby/object:Gem::Dependency
|
63
|
+
name: rspec
|
64
|
+
requirement: !ruby/object:Gem::Requirement
|
65
|
+
none: false
|
66
|
+
requirements:
|
67
|
+
- - ~>
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '2.6'
|
70
|
+
type: :development
|
71
|
+
prerelease: false
|
72
|
+
version_requirements: !ruby/object:Gem::Requirement
|
73
|
+
none: false
|
74
|
+
requirements:
|
75
|
+
- - ~>
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '2.6'
|
78
|
+
description: ! "Rails plugin to manage static data in ActiveRecord \n models/tables"
|
79
|
+
email:
|
80
|
+
- bjturner@outofcoffee.com
|
81
|
+
executables: []
|
82
|
+
extensions: []
|
83
|
+
extra_rdoc_files: []
|
84
|
+
files:
|
85
|
+
- .gitignore
|
86
|
+
- Gemfile
|
87
|
+
- LICENSE.txt
|
88
|
+
- README.md
|
89
|
+
- Rakefile
|
90
|
+
- lib/static-data.rb
|
91
|
+
- lib/static-data/base.rb
|
92
|
+
- lib/static-data/railtie.rb
|
93
|
+
- lib/static-data/tasks.rake
|
94
|
+
- lib/static-data/version.rb
|
95
|
+
- spec/static_data_spec.rb
|
96
|
+
- static-data.gemspec
|
97
|
+
homepage: ''
|
98
|
+
licenses:
|
99
|
+
- MIT
|
100
|
+
post_install_message:
|
101
|
+
rdoc_options: []
|
102
|
+
require_paths:
|
103
|
+
- lib
|
104
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
105
|
+
none: false
|
106
|
+
requirements:
|
107
|
+
- - ! '>='
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '0'
|
110
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
111
|
+
none: false
|
112
|
+
requirements:
|
113
|
+
- - ! '>='
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
requirements: []
|
117
|
+
rubyforge_project:
|
118
|
+
rubygems_version: 1.8.23
|
119
|
+
signing_key:
|
120
|
+
specification_version: 3
|
121
|
+
summary: StaticData is a Rails plugin that lets you easily manage fixed data that
|
122
|
+
you want to always have in your database, and is primarily useful for pre-populating
|
123
|
+
lookup tables and the like.
|
124
|
+
test_files:
|
125
|
+
- spec/static_data_spec.rb
|