amee-data-persistence 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -0
- data/CHANGELOG.txt +4 -0
- data/Gemfile +17 -0
- data/Gemfile.lock +48 -0
- data/LICENSE.txt +27 -0
- data/README.txt +89 -0
- data/Rakefile +102 -0
- data/VERSION +1 -0
- data/amee-data-persistence.gemspec +93 -0
- data/amee-data-persistence.tmproj +27 -0
- data/generators/persistence/persistence_generator.rb +97 -0
- data/generators/persistence/templates/config/persistence.yml.erb +1 -0
- data/generators/persistence/templates/db/migrate/001_create_persistence_tables.rb +29 -0
- data/generators/persistence/templates/db/migrate/002_add_unit_columns.rb +15 -0
- data/generators/persistence/templates/db/migrate/003_add_value_types.rb +9 -0
- data/init.rb +1 -0
- data/lib/amee-data-persistence.rb +12 -0
- data/lib/amee/data_abstraction/calculation_collection.rb +22 -0
- data/lib/amee/data_abstraction/persistence_support.rb +274 -0
- data/lib/amee/db/calculation.rb +156 -0
- data/lib/amee/db/config.rb +54 -0
- data/lib/amee/db/term.rb +72 -0
- data/rails/init.rb +31 -0
- data/spec/amee/db/calculation_spec.rb +176 -0
- data/spec/amee/db/config_spec.rb +92 -0
- data/spec/amee/db/persistence_support_spec.rb +356 -0
- data/spec/amee/db/term_spec.rb +147 -0
- data/spec/database.yml +4 -0
- data/spec/spec.opts +2 -0
- data/spec/spec_helper.rb +136 -0
- metadata +239 -0
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm 1.8.7@amee-data-persistence
|
data/CHANGELOG.txt
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
|
3
|
+
# Dependencies
|
4
|
+
gem "amee-data-abstraction","~> 1.0.0"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "bundler", "~> 1.0.0"
|
10
|
+
gem "jeweler", "~> 1.6.4"
|
11
|
+
gem 'rspec', '1.3.0'
|
12
|
+
gem 'rcov'
|
13
|
+
gem 'rspec_spinner', '1.1.3'
|
14
|
+
gem 'flexmock', '> 0.8.6'
|
15
|
+
gem 'activerecord', '~> 2.3.5'
|
16
|
+
gem 'sqlite3'
|
17
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activerecord (2.3.12)
|
5
|
+
activesupport (= 2.3.12)
|
6
|
+
activesupport (2.3.12)
|
7
|
+
amee (3.0.1)
|
8
|
+
activesupport (~> 2.3.5)
|
9
|
+
json
|
10
|
+
log4r
|
11
|
+
nokogiri (~> 1.4.3.1)
|
12
|
+
amee-data-abstraction (1.0.0)
|
13
|
+
amee (~> 3.0)
|
14
|
+
quantify (= 1.1.0)
|
15
|
+
uuidtools (= 2.1.2)
|
16
|
+
flexmock (0.9.0)
|
17
|
+
git (1.2.5)
|
18
|
+
jeweler (1.6.4)
|
19
|
+
bundler (~> 1.0)
|
20
|
+
git (>= 1.2.5)
|
21
|
+
rake
|
22
|
+
json (1.5.3)
|
23
|
+
log4r (1.1.9)
|
24
|
+
nokogiri (1.4.3.1)
|
25
|
+
quantify (1.1.0)
|
26
|
+
activesupport
|
27
|
+
rake (0.9.2)
|
28
|
+
rcov (0.9.10)
|
29
|
+
rspec (1.3.0)
|
30
|
+
rspec_spinner (1.1.3)
|
31
|
+
rtui
|
32
|
+
rtui (0.2.2)
|
33
|
+
sqlite3 (1.3.4)
|
34
|
+
uuidtools (2.1.2)
|
35
|
+
|
36
|
+
PLATFORMS
|
37
|
+
ruby
|
38
|
+
|
39
|
+
DEPENDENCIES
|
40
|
+
activerecord (~> 2.3.5)
|
41
|
+
amee-data-abstraction (~> 1.0.0)
|
42
|
+
bundler (~> 1.0.0)
|
43
|
+
flexmock (> 0.8.6)
|
44
|
+
jeweler (~> 1.6.4)
|
45
|
+
rcov
|
46
|
+
rspec (= 1.3.0)
|
47
|
+
rspec_spinner (= 1.1.3)
|
48
|
+
sqlite3
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
Copyright (c) 2011, AMEE UK Ltd.
|
2
|
+
All rights reserved.
|
3
|
+
|
4
|
+
Redistribution and use in source and binary forms, with or without
|
5
|
+
modification, are permitted provided that the following conditions are met:
|
6
|
+
|
7
|
+
Redistributions of source code must retain the above copyright notice,
|
8
|
+
this list of conditions and the following disclaimer.
|
9
|
+
|
10
|
+
Redistributions in binary form must reproduce the above copyright notice,
|
11
|
+
this list of conditions and the following disclaimer in the documentation
|
12
|
+
and/or other materials provided with the distribution.
|
13
|
+
|
14
|
+
Neither the name of AMEE UK Ltd nor the names of its contributors may be
|
15
|
+
used to endorse or promote products derived from this software without
|
16
|
+
specific prior written permission.
|
17
|
+
|
18
|
+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
19
|
+
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
20
|
+
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
21
|
+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
22
|
+
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
23
|
+
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
24
|
+
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
25
|
+
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
26
|
+
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
27
|
+
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
data/README.txt
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
== amee-data-persistence
|
2
|
+
|
3
|
+
The amee-data-persistence gem provides database support for use in conjunction
|
4
|
+
with the amee-data-abstraction gem.
|
5
|
+
|
6
|
+
Licensed under the BSD 3-Clause license (See LICENSE.txt for details)
|
7
|
+
|
8
|
+
Authors: James Hetherington, James Smith, Andrew Berkeley, George Palmer
|
9
|
+
|
10
|
+
Copyright: Copyright (c) 2011 AMEE UK Ltd
|
11
|
+
|
12
|
+
Homepage: http://github.com/AMEE/amee-data-persistence
|
13
|
+
|
14
|
+
Documentation: http://rubydoc.info/gems/amee-data-persistence
|
15
|
+
== INSTALLATION
|
16
|
+
|
17
|
+
gem install amee-data-persistence
|
18
|
+
|
19
|
+
== REQUIREMENTS
|
20
|
+
|
21
|
+
* ruby 1.8.7
|
22
|
+
* rubygems >= 1.5
|
23
|
+
|
24
|
+
All gem requirements should be installed as part of the rubygems installation process
|
25
|
+
above, but are listed here for completeness.
|
26
|
+
|
27
|
+
* amee-data-abstraction ~> 1.0.0
|
28
|
+
|
29
|
+
== USAGE
|
30
|
+
|
31
|
+
The library repesents calculations as
|
32
|
+
database records using two classes (<i>AMEE::Db::Calculation</i> and
|
33
|
+
<i>AMEE::Db::Term</i>) both of which inherit <i>ActiveRecord::Base</i>.
|
34
|
+
|
35
|
+
The library also defines the <i>AMEE::DataAbstraction::PersistenceSupport</i>
|
36
|
+
module which is mixed into the <i>AMEE::DataAbstraction::OngoingCalculation</i>
|
37
|
+
class by default when the library is required. The module provides a number of
|
38
|
+
class and instance methods which provide an interface between the
|
39
|
+
<i>AMEE::DataAbstraction::OngoingCalculation</i> class (and its instances) and
|
40
|
+
the database. It is via these methods that the persistence functionality
|
41
|
+
provided by the <i>AMEE::Db</i> classes is principally used.
|
42
|
+
|
43
|
+
The level of data storage can be configured to three distinct levels, representing
|
44
|
+
the range of calculation terms which are persisted: all; outputs and metadata only;
|
45
|
+
and metadata only.
|
46
|
+
|
47
|
+
The global persistence storage level and migrations for the database tables
|
48
|
+
(under /db/migrate) can be generated using the command line generator command:
|
49
|
+
|
50
|
+
$ script/generate persistence <storage_level>
|
51
|
+
|
52
|
+
where <storage_level> can be either 'everything', 'outputs' or 'metadata', e.g.,
|
53
|
+
|
54
|
+
$ script/generate persistence everything
|
55
|
+
|
56
|
+
|
57
|
+
=== Example usage
|
58
|
+
|
59
|
+
my_calculation = OngoingCalculation.find(:first)
|
60
|
+
|
61
|
+
#=> <AMEE::DataAbstraction::OngoingCalculation ... >
|
62
|
+
|
63
|
+
my_calculation = OngoingCalculation.find(28)
|
64
|
+
|
65
|
+
#=> <AMEE::DataAbstraction::OngoingCalculation ... >
|
66
|
+
|
67
|
+
my_calculation = OngoingCalculation.find(:all)
|
68
|
+
|
69
|
+
#=> <AMEE::DataAbstraction::CalculationCollection ... >
|
70
|
+
|
71
|
+
my_calculation = OngoingCalculation.find_by_type(:all, :electricity)
|
72
|
+
|
73
|
+
#=> <AMEE::DataAbstraction::CalculationCollection ... >
|
74
|
+
|
75
|
+
my_calculation.id #= 28
|
76
|
+
|
77
|
+
my_calculation.db_calculation #=> <AMEE::Db::Calculation ... >
|
78
|
+
|
79
|
+
my_calculation.to_hash #=> { :profile_uid => "EYR758EY36WY",
|
80
|
+
:profile_item_uid => "W83URT48DY3W",
|
81
|
+
:type => { :value => 'car' },
|
82
|
+
:distance => { :value => 1600,
|
83
|
+
:unit => <Quantify::Unit::SI> },
|
84
|
+
:co2 => { :value => 234.1,
|
85
|
+
:unit => <Quantify::Unit::NonSI> }}
|
86
|
+
|
87
|
+
my_calculation.save #=> true
|
88
|
+
|
89
|
+
my_calculation.delete #=> nil
|
data/Rakefile
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
# Copyright (C) 2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
# encoding: utf-8
|
5
|
+
|
6
|
+
require 'rubygems'
|
7
|
+
require 'bundler'
|
8
|
+
begin
|
9
|
+
Bundler.setup(:default, :development)
|
10
|
+
rescue Bundler::BundlerError => e
|
11
|
+
$stderr.puts e.message
|
12
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
13
|
+
exit e.status_code
|
14
|
+
end
|
15
|
+
require 'rake'
|
16
|
+
require 'spec'
|
17
|
+
require 'spec/rake/spectask'
|
18
|
+
|
19
|
+
task :default => [:spec]
|
20
|
+
|
21
|
+
Spec::Rake::SpecTask.new do |t|
|
22
|
+
t.spec_opts = ['--options', "spec/spec.opts"]
|
23
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
24
|
+
t.rcov = true
|
25
|
+
t.rcov_opts = ['--exclude', 'spec,/*ruby*,']
|
26
|
+
end
|
27
|
+
|
28
|
+
require 'jeweler'
|
29
|
+
# Fix for Jeweler to use stable branch
|
30
|
+
class Jeweler
|
31
|
+
module Commands
|
32
|
+
class ReleaseToGit
|
33
|
+
def run
|
34
|
+
unless clean_staging_area?
|
35
|
+
system "git status"
|
36
|
+
raise "Unclean staging area! Be sure to commit or .gitignore everything first. See `git status` above."
|
37
|
+
end
|
38
|
+
repo.checkout('stable')
|
39
|
+
repo.push('origin', 'stable')
|
40
|
+
if release_not_tagged?
|
41
|
+
output.puts "Tagging #{release_tag}"
|
42
|
+
repo.add_tag(release_tag)
|
43
|
+
output.puts "Pushing #{release_tag} to origin"
|
44
|
+
repo.push('origin', release_tag)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
class ReleaseGemspec
|
49
|
+
def run
|
50
|
+
unless clean_staging_area?
|
51
|
+
system "git status"
|
52
|
+
raise "Unclean staging area! Be sure to commit or .gitignore everything first. See `git status` above."
|
53
|
+
end
|
54
|
+
repo.checkout('stable')
|
55
|
+
regenerate_gemspec!
|
56
|
+
commit_gemspec! if gemspec_changed?
|
57
|
+
output.puts "Pushing stable to origin"
|
58
|
+
repo.push('origin', 'stable')
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
Jeweler::Tasks.new do |gem|
|
65
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
66
|
+
gem.name = "amee-data-persistence"
|
67
|
+
gem.homepage = "http://github.com/AMEE/amee-data-persistence"
|
68
|
+
gem.license = "BSD 3-Clause"
|
69
|
+
gem.summary = %Q{Persistent storage of calculations performed against the AMEE API}
|
70
|
+
gem.description = %Q{Part of the AMEEappkit, this gem provides storage and retrival of data provided by the amee-data-abstraction gem}
|
71
|
+
gem.email = "help@amee.com"
|
72
|
+
gem.authors = ["James Hetherington", "Andrew Berkeley", "James Smith", "George Palmer"]
|
73
|
+
# dependencies defined in Gemfile
|
74
|
+
end
|
75
|
+
Jeweler::RubygemsDotOrgTasks.new
|
76
|
+
|
77
|
+
require 'rake/testtask'
|
78
|
+
Rake::TestTask.new(:test) do |test|
|
79
|
+
test.libs << 'lib' << 'test'
|
80
|
+
test.pattern = 'test/**/test_*.rb'
|
81
|
+
test.verbose = true
|
82
|
+
end
|
83
|
+
|
84
|
+
require 'rcov/rcovtask'
|
85
|
+
Rcov::RcovTask.new do |test|
|
86
|
+
test.libs << 'test'
|
87
|
+
test.pattern = 'test/**/test_*.rb'
|
88
|
+
test.verbose = true
|
89
|
+
test.rcov_opts << '--exclude "gems/*"'
|
90
|
+
end
|
91
|
+
|
92
|
+
task :default => :test
|
93
|
+
|
94
|
+
require 'rake/rdoctask'
|
95
|
+
Rake::RDocTask.new do |rdoc|
|
96
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
97
|
+
|
98
|
+
rdoc.rdoc_dir = 'rdoc'
|
99
|
+
rdoc.title = "amee-data-persistence #{version}"
|
100
|
+
rdoc.rdoc_files.include('README*')
|
101
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
102
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
1.0.0
|
@@ -0,0 +1,93 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{amee-data-persistence}
|
8
|
+
s.version = "1.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["James Hetherington", "Andrew Berkeley", "James Smith", "George Palmer"]
|
12
|
+
s.date = %q{2011-08-11}
|
13
|
+
s.description = %q{Part of the AMEEappkit, this gem provides storage and retrival of data provided by the amee-data-abstraction gem}
|
14
|
+
s.email = %q{help@amee.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.txt"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".rvmrc",
|
21
|
+
"CHANGELOG.txt",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.txt",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"amee-data-persistence.gemspec",
|
29
|
+
"amee-data-persistence.tmproj",
|
30
|
+
"generators/persistence/persistence_generator.rb",
|
31
|
+
"generators/persistence/templates/config/persistence.yml.erb",
|
32
|
+
"generators/persistence/templates/db/migrate/001_create_persistence_tables.rb",
|
33
|
+
"generators/persistence/templates/db/migrate/002_add_unit_columns.rb",
|
34
|
+
"generators/persistence/templates/db/migrate/003_add_value_types.rb",
|
35
|
+
"init.rb",
|
36
|
+
"lib/amee-data-persistence.rb",
|
37
|
+
"lib/amee/data_abstraction/calculation_collection.rb",
|
38
|
+
"lib/amee/data_abstraction/persistence_support.rb",
|
39
|
+
"lib/amee/db/calculation.rb",
|
40
|
+
"lib/amee/db/config.rb",
|
41
|
+
"lib/amee/db/term.rb",
|
42
|
+
"rails/init.rb",
|
43
|
+
"spec/amee/db/calculation_spec.rb",
|
44
|
+
"spec/amee/db/config_spec.rb",
|
45
|
+
"spec/amee/db/persistence_support_spec.rb",
|
46
|
+
"spec/amee/db/term_spec.rb",
|
47
|
+
"spec/database.yml",
|
48
|
+
"spec/spec.opts",
|
49
|
+
"spec/spec_helper.rb"
|
50
|
+
]
|
51
|
+
s.homepage = %q{http://github.com/AMEE/amee-data-persistence}
|
52
|
+
s.licenses = ["BSD 3-Clause"]
|
53
|
+
s.require_paths = ["lib"]
|
54
|
+
s.rubygems_version = %q{1.6.2}
|
55
|
+
s.summary = %q{Persistent storage of calculations performed against the AMEE API}
|
56
|
+
|
57
|
+
if s.respond_to? :specification_version then
|
58
|
+
s.specification_version = 3
|
59
|
+
|
60
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
61
|
+
s.add_runtime_dependency(%q<amee-data-abstraction>, ["~> 1.0.0"])
|
62
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
64
|
+
s.add_development_dependency(%q<rspec>, ["= 1.3.0"])
|
65
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
66
|
+
s.add_development_dependency(%q<rspec_spinner>, ["= 1.1.3"])
|
67
|
+
s.add_development_dependency(%q<flexmock>, ["> 0.8.6"])
|
68
|
+
s.add_development_dependency(%q<activerecord>, ["~> 2.3.5"])
|
69
|
+
s.add_development_dependency(%q<sqlite3>, [">= 0"])
|
70
|
+
else
|
71
|
+
s.add_dependency(%q<amee-data-abstraction>, ["~> 1.0.0"])
|
72
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
73
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
74
|
+
s.add_dependency(%q<rspec>, ["= 1.3.0"])
|
75
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
76
|
+
s.add_dependency(%q<rspec_spinner>, ["= 1.1.3"])
|
77
|
+
s.add_dependency(%q<flexmock>, ["> 0.8.6"])
|
78
|
+
s.add_dependency(%q<activerecord>, ["~> 2.3.5"])
|
79
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
80
|
+
end
|
81
|
+
else
|
82
|
+
s.add_dependency(%q<amee-data-abstraction>, ["~> 1.0.0"])
|
83
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
84
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
85
|
+
s.add_dependency(%q<rspec>, ["= 1.3.0"])
|
86
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
87
|
+
s.add_dependency(%q<rspec_spinner>, ["= 1.1.3"])
|
88
|
+
s.add_dependency(%q<flexmock>, ["> 0.8.6"])
|
89
|
+
s.add_dependency(%q<activerecord>, ["~> 2.3.5"])
|
90
|
+
s.add_dependency(%q<sqlite3>, [">= 0"])
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
<?xml version="1.0" encoding="UTF-8"?>
|
2
|
+
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
|
3
|
+
<plist version="1.0">
|
4
|
+
<dict>
|
5
|
+
<key>documents</key>
|
6
|
+
<array>
|
7
|
+
<dict>
|
8
|
+
<key>expanded</key>
|
9
|
+
<true/>
|
10
|
+
<key>name</key>
|
11
|
+
<string>amee-data-persistence</string>
|
12
|
+
<key>regexFolderFilter</key>
|
13
|
+
<string>!.*/(\.[^/]*|CVS|_darcs|_MTN|\{arch\}|blib|.*~\.nib|.*\.(framework|app|pbproj|pbxproj|xcode(proj)?|bundle))$</string>
|
14
|
+
<key>sourceDirectory</key>
|
15
|
+
<string></string>
|
16
|
+
</dict>
|
17
|
+
</array>
|
18
|
+
<key>fileHierarchyDrawerWidth</key>
|
19
|
+
<integer>200</integer>
|
20
|
+
<key>metaData</key>
|
21
|
+
<dict/>
|
22
|
+
<key>showFileHierarchyDrawer</key>
|
23
|
+
<true/>
|
24
|
+
<key>windowFrame</key>
|
25
|
+
<string>{{193, 217}, {1390, 940}}</string>
|
26
|
+
</dict>
|
27
|
+
</plist>
|
@@ -0,0 +1,97 @@
|
|
1
|
+
# Copyright (C) 2011 AMEE UK Ltd. - http://www.amee.com
|
2
|
+
# Released as Open Source Software under the BSD 3-Clause license. See LICENSE.txt for details.
|
3
|
+
|
4
|
+
# :title: Class: PersistenceGenerator
|
5
|
+
|
6
|
+
# The global persistence storage level and migrations for the database tables can be
|
7
|
+
# generated the <i>PersistenceGenerator</i> class.
|
8
|
+
#
|
9
|
+
# The level of data storage for <i>AMEE::Db</i> can be configured to three
|
10
|
+
# distinct levels, representing the range of calculation terms which are
|
11
|
+
# persisted: all; outputs and metadata only; and metadata only.
|
12
|
+
#
|
13
|
+
# To set the global persistence level and generate migration files in /db/migrate,
|
14
|
+
# execute the following command line generator command:
|
15
|
+
#
|
16
|
+
# $ script/generate persistence <storage_level>
|
17
|
+
#
|
18
|
+
# where <storage_level> can be either 'everything', 'outputs' or 'metadata', e.g.,
|
19
|
+
#
|
20
|
+
# $ script/generate persistence everything
|
21
|
+
#
|
22
|
+
class PersistenceGenerator < Rails::Generator::Base
|
23
|
+
|
24
|
+
# Need to explicitly define the paths to both the templates directory and the
|
25
|
+
# standard application migrations directory. These are equivalent when using
|
26
|
+
# the #template method it is assumed therein that there is a mapping to the
|
27
|
+
# /templates directory for the generator templates (and from there the sub-
|
28
|
+
# paths are the same). This assumption is not valid when accessing the
|
29
|
+
# templates and the existing application migrations otherwise.
|
30
|
+
#
|
31
|
+
DEFAULT_MIGRATION_PATH = File.join("db/migrate")
|
32
|
+
TEMPLATE_MIGRATION_PATH = File.join(File.dirname(__FILE__),"templates",DEFAULT_MIGRATION_PATH)
|
33
|
+
|
34
|
+
def manifest
|
35
|
+
record do |m|
|
36
|
+
|
37
|
+
########################################
|
38
|
+
# persistence level configuration file #
|
39
|
+
########################################
|
40
|
+
|
41
|
+
# Get method from command line - default is metadata
|
42
|
+
method = args[0] || 'metadata'
|
43
|
+
# Make sure there is a config directory
|
44
|
+
m.directory File.join("config")
|
45
|
+
# Create persistence.yml file
|
46
|
+
m.template File.join("config","persistence.yml.erb"),
|
47
|
+
File.join("config","persistence.yml"),
|
48
|
+
:assigns => {:method => method}
|
49
|
+
|
50
|
+
|
51
|
+
|
52
|
+
#######################
|
53
|
+
# database migrations #
|
54
|
+
#######################
|
55
|
+
|
56
|
+
# Check db/migrate exists
|
57
|
+
m.directory DEFAULT_MIGRATION_PATH
|
58
|
+
# Create migrations if not already represented
|
59
|
+
migration_template_files.each_with_index do |file_name,index|
|
60
|
+
unless migration_exists? file_name
|
61
|
+
template_path = File.join(DEFAULT_MIGRATION_PATH,file_name)
|
62
|
+
# increment timestamp to differentiate files generated within same second
|
63
|
+
destination_file_name = file_name.gsub(/\d{3}/,"#{Time.now.strftime("%Y%m%d%I%M%S").to_i+index}")
|
64
|
+
destination_path = File.join(DEFAULT_MIGRATION_PATH,destination_file_name)
|
65
|
+
|
66
|
+
m.template template_path, destination_path
|
67
|
+
else
|
68
|
+
# print to stdout in standard migration reporting format
|
69
|
+
puts " exists migration content in template: #{file_name}"
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
# Get names of all migration template files and sort them by version number
|
77
|
+
def migration_template_files
|
78
|
+
Dir.new(TEMPLATE_MIGRATION_PATH).entries.reject { |entry| File.directory? entry }.sort
|
79
|
+
end
|
80
|
+
|
81
|
+
# Check whether a migration already exists, by comparing the content of the
|
82
|
+
# migration template with any migrations which already exist in the
|
83
|
+
# applicaition's db/migrate directory. This is preferable to using file names
|
84
|
+
# since file names (1) have a time stamp and are therefore not consistent
|
85
|
+
# between separate instances of a file generation and (2) migration names may
|
86
|
+
# be changed in the future.
|
87
|
+
#
|
88
|
+
def migration_exists?(template)
|
89
|
+
template_content = File.read(File.join(TEMPLATE_MIGRATION_PATH,template))
|
90
|
+
|
91
|
+
Dir.new(DEFAULT_MIGRATION_PATH).entries.any? do |file|
|
92
|
+
next if File.directory?(file)
|
93
|
+
File.read(File.join(DEFAULT_MIGRATION_PATH,file)) == template_content
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
end
|