dm-is-predefined 0.3.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +10 -0
- data/.specopts +1 -0
- data/.yardopts +1 -0
- data/ChangeLog.md +39 -0
- data/Gemfile +97 -0
- data/LICENSE.txt +22 -0
- data/README.md +59 -0
- data/Rakefile +34 -0
- data/VERSION +1 -0
- data/dm-is-predefined.gemspec +75 -0
- data/lib/dm-is-predefined.rb +4 -0
- data/lib/dm-is-predefined/is/exceptions/unknown_resource.rb +8 -0
- data/lib/dm-is-predefined/is/predefined.rb +129 -0
- data/spec/integration/models/test_model.rb +31 -0
- data/spec/integration/predefined_spec.rb +90 -0
- data/spec/spec_helper.rb +22 -0
- metadata +159 -0
data/.gitignore
ADDED
data/.specopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour --format specdoc
|
data/.yardopts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--markup markdown --title 'dm-predefined Documentation' --protected --files ChangeLog.md,LICENSE.txt
|
data/ChangeLog.md
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
### 0.3.0 / 2010-07-08
|
2
|
+
|
3
|
+
* Require DataMapper 1.0.0.
|
4
|
+
* Renamed dm-predefined to dm-is-predefined.
|
5
|
+
* Renamed `DataMapper::Predefined` to {DataMapper::Is::Predefined}.
|
6
|
+
|
7
|
+
### 0.2.3 / 2010-04-01
|
8
|
+
|
9
|
+
* Store the predefined attributes in an instance variable in the Model.
|
10
|
+
|
11
|
+
### 0.2.2 / 2010-03-20
|
12
|
+
|
13
|
+
* Switched to Jeweler for the building and releasing of RubyGems.
|
14
|
+
* Split `dm-predefined/yard` out into the `yard-dm-predefined` library.
|
15
|
+
* Require dm-core >= 0.10.2.
|
16
|
+
* Renamed `DataMapper::Predefined::ClassMethods#[]` to
|
17
|
+
`DataMapper::Predefined::ClassMethods#predefined_resource`.
|
18
|
+
* Added `DataMapper::Predefined::ClassMethods#predefined_names`.
|
19
|
+
* Added `DataMapper::Predefined::ClassMethods#predefined_resource_with`.
|
20
|
+
* Added more specs.
|
21
|
+
|
22
|
+
### 0.2.0 / 2009-09-21
|
23
|
+
|
24
|
+
* Require Hoe >= 2.3.3.
|
25
|
+
* Require YARD >= 0.2.3.5.
|
26
|
+
* Require dm-core >= 0.10.0.
|
27
|
+
* Moved to YARD based documentation.
|
28
|
+
* Added YARD handlers for documenting +predefine+ method-calls.
|
29
|
+
* Renamed Predefined.predefined to Predefined.predefine.
|
30
|
+
|
31
|
+
### 0.1.1 / 2009-01-17
|
32
|
+
|
33
|
+
* Fixed specs to work with RSpec 1.1.12.
|
34
|
+
* All specs pass in Ruby 1.9.1-rc1.
|
35
|
+
|
36
|
+
### 0.1.0 / 2008-12-21
|
37
|
+
|
38
|
+
* Initial release.
|
39
|
+
|
data/Gemfile
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
source 'http://rubygems.org'
|
2
|
+
dm = 'git://github.com/datamapper'
|
3
|
+
|
4
|
+
group :runtime do
|
5
|
+
# We bundle both AS and extlib while extlib compatibility needs to be kept
|
6
|
+
# around. require 'dm-core' will ensure that only one is activated at any
|
7
|
+
# time though. This is done by trying to require AS components and
|
8
|
+
# fallback to requiring extlib in case a LoadError was rescued when
|
9
|
+
# requiring AS code.
|
10
|
+
#
|
11
|
+
# Due to bundle exec activating all groups in the Gemfile, it's
|
12
|
+
# recommended to run
|
13
|
+
#
|
14
|
+
# bundle install --without quality
|
15
|
+
#
|
16
|
+
# to have a development environment that is able to run the specs.
|
17
|
+
# The problem is that metric_fu activates active_support=2.2.3 if we
|
18
|
+
# comment out the gem 'activesupport' declaration - have a look below for
|
19
|
+
# why we would want to do that (and a bit later, for why that's actually
|
20
|
+
# not *strictly* necessary, but recommended)
|
21
|
+
#
|
22
|
+
# To run the specs using AS, leave this Gemfile as it is and just run:
|
23
|
+
#
|
24
|
+
# bundle install --without qality
|
25
|
+
# ADAPTERS=sqlite3 bundle exec rake spec # or whatever adapter
|
26
|
+
#
|
27
|
+
# To run the specs using extlib, comment out the: gem 'activesupport' line
|
28
|
+
# and run:
|
29
|
+
#
|
30
|
+
# bundle install --without quality
|
31
|
+
# ADAPTERS=sqlite3 bundle exec rake spec # or whatever adapter
|
32
|
+
#
|
33
|
+
# If you want to run the quality tasks as provided by metric_fu and
|
34
|
+
# related gems, you have to run:
|
35
|
+
#
|
36
|
+
# bundle install
|
37
|
+
# bundle exec rake metrics:all
|
38
|
+
#
|
39
|
+
# Switch back to a bundle without quality gems before trying to run the
|
40
|
+
# specs again:
|
41
|
+
#
|
42
|
+
# bundle install --without quality
|
43
|
+
# ADAPTERS=sqlite3 bundle exec rake spec # or whatever adapter
|
44
|
+
#
|
45
|
+
# It was mentioned above that all this is not *strictly* necessary, and
|
46
|
+
# this is true. Currently dm-core does the following as the first require
|
47
|
+
# when checking for AS:
|
48
|
+
#
|
49
|
+
# require 'active_support/core_ext/object/singleton_class'
|
50
|
+
#
|
51
|
+
# Because this method is not present in activesupport <= 3.0.0.beta,
|
52
|
+
# dm-core's feature detection will actually do the "right thing" and fall
|
53
|
+
# back to extlib. However, since this is not the case for all dm-more gems
|
54
|
+
# as well, the safest thing to do is to respect the more tedious workflow
|
55
|
+
# for now, as it will at least be guaranteed to work the same for both
|
56
|
+
# dm-core and dm-more.
|
57
|
+
#
|
58
|
+
# Note that this won't be an issue anymore once we dropped support for
|
59
|
+
# extlib completely, or bundler folks decide to support something like
|
60
|
+
# "bundle exec --without=foo rake spec" (which probably is not going to
|
61
|
+
# happen anytime soon).
|
62
|
+
#
|
63
|
+
|
64
|
+
if ENV['EXTLIB']
|
65
|
+
gem 'extlib', '~> 0.9.15'
|
66
|
+
else
|
67
|
+
gem 'activesupport', '~> 3.0.0.beta3', :require => 'active_support'
|
68
|
+
end
|
69
|
+
|
70
|
+
gem 'dm-core', '~> 1.0.0'
|
71
|
+
end
|
72
|
+
|
73
|
+
group :development do
|
74
|
+
gem 'rake', '~> 0.8.7'
|
75
|
+
gem 'jeweler', '~> 1.4.0', :git => 'git://github.com/technicalpickles/jeweler.git'
|
76
|
+
end
|
77
|
+
|
78
|
+
group :doc do
|
79
|
+
case RUBY_PLATFORM
|
80
|
+
when 'java'
|
81
|
+
gem 'maruku', '~> 0.6.0'
|
82
|
+
else
|
83
|
+
gem 'rdiscount', '~> 1.6.3'
|
84
|
+
end
|
85
|
+
|
86
|
+
gem 'yard', '~> 0.5.3'
|
87
|
+
end
|
88
|
+
|
89
|
+
group :test do
|
90
|
+
gem 'data_objects', '~> 0.10.2'
|
91
|
+
gem 'do_sqlite3', '~> 0.10.2'
|
92
|
+
gem 'dm-do-adapter', '~> 1.0.0'
|
93
|
+
gem 'dm-sqlite-adapter', '~> 1.0.0'
|
94
|
+
gem 'dm-migrations', '~> 1.0.0'
|
95
|
+
end
|
96
|
+
|
97
|
+
gem 'rspec', '~> 1.3.0', :group => [:development, :test]
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
(The MIT License)
|
2
|
+
|
3
|
+
Copyright (c) 2008-2010 Hal Brodigan
|
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,59 @@
|
|
1
|
+
# dm-is-predefined
|
2
|
+
|
3
|
+
* [github.com/postmodern/dm-is-predefined](http://github.com/postmodern/dm-is-predefined)
|
4
|
+
* [github.com/postmodern/dm-is-predefined/issues](http://github.com/postmodern/dm-is-predefined/issues)
|
5
|
+
* Postmodern (postmodern.mod3 at gmail.com)
|
6
|
+
|
7
|
+
## Description
|
8
|
+
|
9
|
+
A DataMapper plugin for adding predefined resources to Models.
|
10
|
+
|
11
|
+
## Example
|
12
|
+
|
13
|
+
require 'dm-core'
|
14
|
+
require 'dm-is-predefined'
|
15
|
+
|
16
|
+
class Licence
|
17
|
+
|
18
|
+
include DataMapper::Resource
|
19
|
+
|
20
|
+
is :predefined
|
21
|
+
|
22
|
+
# Name of the Licence
|
23
|
+
property :name, String
|
24
|
+
|
25
|
+
# URL to the licence
|
26
|
+
property :url, String
|
27
|
+
|
28
|
+
predefine :gpl2,
|
29
|
+
:name => 'GPL-2',
|
30
|
+
:url => 'http://www.gnu.org/copyleft/gpl.html'
|
31
|
+
|
32
|
+
predefine :mit, :name => 'MIT'
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
Licence.gpl2
|
37
|
+
# => #<Licence: id: 1, name: "GPL-2", url: "http://www.gnu.org/copyleft/gpl.html">
|
38
|
+
|
39
|
+
Licence.predefined_resource(:mit)
|
40
|
+
# => #<Licence: id: 2, name: "MIT">
|
41
|
+
|
42
|
+
License.predefined_resource_with(:name => 'GPL-2')
|
43
|
+
# => #<License: id: 1, name: "GPL-2", url: "http://www.gnu.org/copyleft/gpl.html">
|
44
|
+
|
45
|
+
License.predefined_names
|
46
|
+
# => [:gpl2, :mit]
|
47
|
+
|
48
|
+
## Requirements
|
49
|
+
|
50
|
+
* [dm-core](http://github.com/datamapper/dm-core/) ~> 1.0.0
|
51
|
+
|
52
|
+
## Install
|
53
|
+
|
54
|
+
$ sudo gem install dm-is-predefined
|
55
|
+
|
56
|
+
## License
|
57
|
+
|
58
|
+
See {file:LICENSE.txt} for license information.
|
59
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
begin
|
5
|
+
Bundler.setup(:development, :doc)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
STDERR.puts e.message
|
8
|
+
STDERR.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'rake'
|
13
|
+
require 'jeweler'
|
14
|
+
Jeweler::Tasks.new do |gem|
|
15
|
+
gem.name = 'dm-is-predefined'
|
16
|
+
gem.license = 'MIT'
|
17
|
+
gem.summary = %Q{A DataMapper plugin for adding predefined resources to Models.}
|
18
|
+
gem.description = %Q{A DataMapper plugin for adding predefined resources to Models.}
|
19
|
+
gem.email = 'postmodern.mod3@gmail.com'
|
20
|
+
gem.homepage = 'http://github.com/postmodern/dm-is-predefined'
|
21
|
+
gem.authors = ['Postmodern']
|
22
|
+
gem.has_rdoc = 'yard'
|
23
|
+
end
|
24
|
+
|
25
|
+
require 'spec/rake/spectask'
|
26
|
+
Spec::Rake::SpecTask.new(:spec) do |spec|
|
27
|
+
spec.libs += ['lib', 'spec']
|
28
|
+
spec.spec_files = FileList['spec/**/*_spec.rb']
|
29
|
+
spec.spec_opts = ['--options', '.specopts']
|
30
|
+
end
|
31
|
+
task :default => :spec
|
32
|
+
|
33
|
+
require 'yard'
|
34
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.3.0
|
@@ -0,0 +1,75 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{dm-is-predefined}
|
8
|
+
s.version = "0.3.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Postmodern"]
|
12
|
+
s.date = %q{2010-06-10}
|
13
|
+
s.description = %q{A DataMapper plugin for adding predefined resources to Models.}
|
14
|
+
s.email = %q{postmodern.mod3@gmail.com}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"ChangeLog.md",
|
17
|
+
"LICENSE.txt",
|
18
|
+
"README.md"
|
19
|
+
]
|
20
|
+
s.files = [
|
21
|
+
".gitignore",
|
22
|
+
".specopts",
|
23
|
+
".yardopts",
|
24
|
+
"ChangeLog.md",
|
25
|
+
"Gemfile",
|
26
|
+
"LICENSE.txt",
|
27
|
+
"README.md",
|
28
|
+
"Rakefile",
|
29
|
+
"VERSION",
|
30
|
+
"dm-is-predefined.gemspec",
|
31
|
+
"lib/dm-is-predefined.rb",
|
32
|
+
"lib/dm-is-predefined/is/exceptions/unknown_resource.rb",
|
33
|
+
"lib/dm-is-predefined/is/predefined.rb",
|
34
|
+
"spec/integration/models/test_model.rb",
|
35
|
+
"spec/integration/predefined_spec.rb",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
s.has_rdoc = %q{yard}
|
39
|
+
s.homepage = %q{http://github.com/postmodern/dm-is-predefined}
|
40
|
+
s.licenses = ["MIT"]
|
41
|
+
s.require_paths = ["lib"]
|
42
|
+
s.rubygems_version = %q{1.3.7}
|
43
|
+
s.summary = %q{A DataMapper plugin for adding predefined resources to Models.}
|
44
|
+
s.test_files = [
|
45
|
+
"spec/integration/models/test_model.rb",
|
46
|
+
"spec/integration/predefined_spec.rb",
|
47
|
+
"spec/spec_helper.rb"
|
48
|
+
]
|
49
|
+
|
50
|
+
if s.respond_to? :specification_version then
|
51
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
52
|
+
s.specification_version = 3
|
53
|
+
|
54
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
55
|
+
s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
|
56
|
+
s.add_runtime_dependency(%q<dm-core>, ["~> 1.0.0"])
|
57
|
+
s.add_development_dependency(%q<rake>, ["~> 0.8.7"])
|
58
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.4.0"])
|
59
|
+
s.add_development_dependency(%q<rspec>, ["~> 1.3.0"])
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
|
62
|
+
s.add_dependency(%q<dm-core>, ["~> 1.0.0"])
|
63
|
+
s.add_dependency(%q<rake>, ["~> 0.8.7"])
|
64
|
+
s.add_dependency(%q<jeweler>, ["~> 1.4.0"])
|
65
|
+
s.add_dependency(%q<rspec>, ["~> 1.3.0"])
|
66
|
+
end
|
67
|
+
else
|
68
|
+
s.add_dependency(%q<activesupport>, ["~> 3.0.0.beta3"])
|
69
|
+
s.add_dependency(%q<dm-core>, ["~> 1.0.0"])
|
70
|
+
s.add_dependency(%q<rake>, ["~> 0.8.7"])
|
71
|
+
s.add_dependency(%q<jeweler>, ["~> 1.4.0"])
|
72
|
+
s.add_dependency(%q<rspec>, ["~> 1.3.0"])
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
@@ -0,0 +1,129 @@
|
|
1
|
+
require 'dm-is-predefined/is/exceptions/unknown_resource'
|
2
|
+
|
3
|
+
module DataMapper
|
4
|
+
module Is
|
5
|
+
module Predefined
|
6
|
+
#
|
7
|
+
# Fired when your plugin gets included into Resource.
|
8
|
+
#
|
9
|
+
def is_predefined
|
10
|
+
extend DataMapper::Is::Predefined::ClassMethods
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
#
|
15
|
+
# Returns the names of the predefined resources.
|
16
|
+
#
|
17
|
+
# @return [Array<Symbol>]
|
18
|
+
# The names of the predefined resources.
|
19
|
+
#
|
20
|
+
# @since 0.2.1
|
21
|
+
#
|
22
|
+
def predefined_names
|
23
|
+
predefined_attributes.keys
|
24
|
+
end
|
25
|
+
|
26
|
+
#
|
27
|
+
# Finds or auto-creates the pre-defined resource with the given name.
|
28
|
+
#
|
29
|
+
# @param [Symbol, String] name
|
30
|
+
# The name of the pre-defined resource.
|
31
|
+
#
|
32
|
+
# @return [DataMapper::Resource]
|
33
|
+
# The pre-defined resource.
|
34
|
+
#
|
35
|
+
# @raise [UnknownResource]
|
36
|
+
# Indicates that there are no predefined attributes for the resource
|
37
|
+
# with the given name.
|
38
|
+
#
|
39
|
+
# @since 0.2.1
|
40
|
+
#
|
41
|
+
def predefined_resource(name)
|
42
|
+
name = name.to_sym
|
43
|
+
attributes = self.predefined_attributes[name]
|
44
|
+
|
45
|
+
unless attributes
|
46
|
+
raise(UnknownResource,"The resource '#{name}' was not predefined",caller)
|
47
|
+
end
|
48
|
+
|
49
|
+
self.first_or_create(attributes)
|
50
|
+
end
|
51
|
+
|
52
|
+
#
|
53
|
+
# Finds or auto-creates the predefined resource which shares the
|
54
|
+
# given attributes.
|
55
|
+
#
|
56
|
+
# @param [Hash{Symbol => Object}] desired_attributes
|
57
|
+
# The attribute names and values that the predefined resource
|
58
|
+
# should shared.
|
59
|
+
#
|
60
|
+
# @return [DataMapper::Resource]
|
61
|
+
# The predefined resource.
|
62
|
+
#
|
63
|
+
# @raise [UnknownResource]
|
64
|
+
# Could not find a predefined resource that shared all of the
|
65
|
+
# desired attributes.
|
66
|
+
#
|
67
|
+
# @since 0.2.1
|
68
|
+
#
|
69
|
+
def predefined_resource_with(desired_attributes={})
|
70
|
+
self.predefined_attributes.each do |name,attributes|
|
71
|
+
shares_attributes = desired_attributes.all? do |key,value|
|
72
|
+
key = key.to_sym
|
73
|
+
|
74
|
+
attributes.has_key?(key) && (attributes[key] == value)
|
75
|
+
end
|
76
|
+
|
77
|
+
return predefined_resource(name) if shares_attributes
|
78
|
+
end
|
79
|
+
|
80
|
+
raise(UnknownResource,"Could not find a predefined resource which shared the given attributes",caller)
|
81
|
+
end
|
82
|
+
|
83
|
+
protected
|
84
|
+
|
85
|
+
#
|
86
|
+
# All pre-defined resources of the model.
|
87
|
+
#
|
88
|
+
# @return [Hash{Symbol => Hash}]
|
89
|
+
# The Hash of pre-defined resources and their attributes.
|
90
|
+
#
|
91
|
+
def predefined_attributes
|
92
|
+
@predefined_attributes ||= {}
|
93
|
+
end
|
94
|
+
|
95
|
+
#
|
96
|
+
# Defines a new pre-defined resource for the model.
|
97
|
+
#
|
98
|
+
# @param [Symbol, String] name
|
99
|
+
# The name of the pre-defined resource.
|
100
|
+
#
|
101
|
+
# @param [Hash] attributes
|
102
|
+
# The attributes for the pre-defined resource.
|
103
|
+
#
|
104
|
+
# @return [Hash]
|
105
|
+
# The attributes that will be assigned to the pre-defined resource.
|
106
|
+
#
|
107
|
+
def predefine(name,attributes={})
|
108
|
+
name = name.to_s
|
109
|
+
|
110
|
+
if attributes.empty?
|
111
|
+
raise(ArgumentError,"Cannot predefine a resource with no attributes",caller)
|
112
|
+
end
|
113
|
+
|
114
|
+
self.predefined_attributes[name.to_sym] = attributes
|
115
|
+
|
116
|
+
class_eval %{
|
117
|
+
class << self
|
118
|
+
define_method(#{name.dump}) do
|
119
|
+
predefined_resource(#{name.dump})
|
120
|
+
end
|
121
|
+
end
|
122
|
+
}
|
123
|
+
|
124
|
+
attributes
|
125
|
+
end
|
126
|
+
end # ClassMethods
|
127
|
+
end # Predefined
|
128
|
+
end # Is
|
129
|
+
end # DataMapper
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'dm-is-predefined'
|
2
|
+
|
3
|
+
class TestModel
|
4
|
+
|
5
|
+
include DataMapper::Resource
|
6
|
+
|
7
|
+
is :predefined
|
8
|
+
|
9
|
+
# Primary key of the test model
|
10
|
+
property :id, Serial
|
11
|
+
|
12
|
+
# Name property to test String values
|
13
|
+
property :name, String
|
14
|
+
|
15
|
+
# Number property to test Integer values
|
16
|
+
property :number, Integer
|
17
|
+
|
18
|
+
# Optional property to test default values
|
19
|
+
property :optional, String, :default => 'hello'
|
20
|
+
|
21
|
+
# Body property to test Text values
|
22
|
+
property :body, Text
|
23
|
+
|
24
|
+
predefine :test1, :name => 'test1',
|
25
|
+
:number => 1,
|
26
|
+
:optional => 'yo',
|
27
|
+
:body => %{This is a test.}
|
28
|
+
|
29
|
+
predefine :test2, :name => 'test2', :number => 2
|
30
|
+
|
31
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'integration/models/test_model'
|
3
|
+
|
4
|
+
describe DataMapper::Is::Predefined do
|
5
|
+
before(:all) do
|
6
|
+
TestModel.auto_migrate!
|
7
|
+
end
|
8
|
+
|
9
|
+
it "should define the @predefined_attributes instance variable" do
|
10
|
+
TestModel.should be_instance_variable_defined(:@predefined_attributes)
|
11
|
+
end
|
12
|
+
|
13
|
+
it "should provide the names of all predefined resources of a Model" do
|
14
|
+
TestModel.predefined_names.should =~ [:test1, :test2]
|
15
|
+
end
|
16
|
+
|
17
|
+
it "should be able to define resources of a Model" do
|
18
|
+
test1 = TestModel.test1
|
19
|
+
|
20
|
+
test1.should_not be_nil
|
21
|
+
test1.name.should == 'test1'
|
22
|
+
test1.number.should == 1
|
23
|
+
test1.optional.should == 'yo'
|
24
|
+
test1.body.should == %{This is a test.}
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should be able to define resources with missing attributes" do
|
28
|
+
test2 = TestModel.test2
|
29
|
+
|
30
|
+
test2.should_not be_nil
|
31
|
+
test2.name.should == 'test2'
|
32
|
+
test2.number.should == 2
|
33
|
+
test2.optional.should == 'hello'
|
34
|
+
test2.body.should be_nil
|
35
|
+
end
|
36
|
+
|
37
|
+
it "should return existing resources" do
|
38
|
+
first_test1 = TestModel.test1
|
39
|
+
second_test1 = TestModel.test1
|
40
|
+
|
41
|
+
first_test1.id.should == second_test1.id
|
42
|
+
end
|
43
|
+
|
44
|
+
describe "predefined_resource" do
|
45
|
+
it "should provide a generic interface for accessing resources" do
|
46
|
+
test1 = TestModel.predefined_resource(:test1)
|
47
|
+
|
48
|
+
test1.name.should == 'test1'
|
49
|
+
test1.number.should == 1
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should raise UnknownResource when accessing undefined resources" do
|
53
|
+
lambda {
|
54
|
+
TestModel.predefined_resource(:missing_test)
|
55
|
+
}.should raise_error(DataMapper::Is::Predefined::UnknownResource)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "predefined_resource_with" do
|
60
|
+
it "should find resources based on attributes they share" do
|
61
|
+
test2 = TestModel.predefined_resource_with(:name => 'test2', :number => 2)
|
62
|
+
|
63
|
+
test2.name.should == 'test2'
|
64
|
+
end
|
65
|
+
|
66
|
+
it "should raise UnknownResource if no resource shares all attribute names" do
|
67
|
+
lambda {
|
68
|
+
TestModel.predefined_resource_with(:number => 1, :missing => 'yo')
|
69
|
+
}.should raise_error(DataMapper::Is::Predefined::UnknownResource)
|
70
|
+
end
|
71
|
+
|
72
|
+
it "should raise UnknownResource if no resource shares any attribute names" do
|
73
|
+
lambda {
|
74
|
+
TestModel.predefined_resource_with(:missing => 1, :typo => 'yo')
|
75
|
+
}.should raise_error(DataMapper::Is::Predefined::UnknownResource)
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should raise UnknownResource if no resource shares all attribute values" do
|
79
|
+
lambda {
|
80
|
+
TestModel.predefined_resource_with(:number => 2, :optional => 'yo')
|
81
|
+
}.should raise_error(DataMapper::Is::Predefined::UnknownResource)
|
82
|
+
end
|
83
|
+
|
84
|
+
it "should raise UnknownResource if no resource shares any attribute values" do
|
85
|
+
lambda {
|
86
|
+
TestModel.predefined_resource_with(:number => 3, :optional => 'bla')
|
87
|
+
}.should raise_error(DataMapper::Is::Predefined::UnknownResource)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
|
4
|
+
begin
|
5
|
+
Bundler.setup(:runtime, :test)
|
6
|
+
rescue Bundler::BundlerError => e
|
7
|
+
STDERR.puts e.message
|
8
|
+
STDERR.puts "Run `bundle install` to install missing gems"
|
9
|
+
exit e.status_code
|
10
|
+
end
|
11
|
+
|
12
|
+
require 'spec'
|
13
|
+
require 'dm-core/spec/setup'
|
14
|
+
require 'dm-core/spec/lib/adapter_helpers'
|
15
|
+
|
16
|
+
require 'dm-is-predefined'
|
17
|
+
|
18
|
+
DataMapper::Spec.setup
|
19
|
+
|
20
|
+
Spec::Runner.configure do |config|
|
21
|
+
config.extend(DataMapper::Spec::Adapters::Helpers)
|
22
|
+
end
|
metadata
ADDED
@@ -0,0 +1,159 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-is-predefined
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 3
|
8
|
+
- 0
|
9
|
+
version: 0.3.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Postmodern
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-10 00:00:00 -07:00
|
18
|
+
default_executable:
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: activesupport
|
22
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
23
|
+
none: false
|
24
|
+
requirements:
|
25
|
+
- - ~>
|
26
|
+
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 3
|
29
|
+
- 0
|
30
|
+
- 0
|
31
|
+
- beta3
|
32
|
+
version: 3.0.0.beta3
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *id001
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: dm-core
|
38
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ~>
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
segments:
|
44
|
+
- 1
|
45
|
+
- 0
|
46
|
+
- 0
|
47
|
+
version: 1.0.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: *id002
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
name: rake
|
53
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ~>
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
segments:
|
59
|
+
- 0
|
60
|
+
- 8
|
61
|
+
- 7
|
62
|
+
version: 0.8.7
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: *id003
|
66
|
+
- !ruby/object:Gem::Dependency
|
67
|
+
name: jeweler
|
68
|
+
requirement: &id004 !ruby/object:Gem::Requirement
|
69
|
+
none: false
|
70
|
+
requirements:
|
71
|
+
- - ~>
|
72
|
+
- !ruby/object:Gem::Version
|
73
|
+
segments:
|
74
|
+
- 1
|
75
|
+
- 4
|
76
|
+
- 0
|
77
|
+
version: 1.4.0
|
78
|
+
type: :development
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: *id004
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rspec
|
83
|
+
requirement: &id005 !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ~>
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
segments:
|
89
|
+
- 1
|
90
|
+
- 3
|
91
|
+
- 0
|
92
|
+
version: 1.3.0
|
93
|
+
type: :development
|
94
|
+
prerelease: false
|
95
|
+
version_requirements: *id005
|
96
|
+
description: A DataMapper plugin for adding predefined resources to Models.
|
97
|
+
email: postmodern.mod3@gmail.com
|
98
|
+
executables: []
|
99
|
+
|
100
|
+
extensions: []
|
101
|
+
|
102
|
+
extra_rdoc_files:
|
103
|
+
- ChangeLog.md
|
104
|
+
- LICENSE.txt
|
105
|
+
- README.md
|
106
|
+
files:
|
107
|
+
- .gitignore
|
108
|
+
- .specopts
|
109
|
+
- .yardopts
|
110
|
+
- ChangeLog.md
|
111
|
+
- Gemfile
|
112
|
+
- LICENSE.txt
|
113
|
+
- README.md
|
114
|
+
- Rakefile
|
115
|
+
- VERSION
|
116
|
+
- dm-is-predefined.gemspec
|
117
|
+
- lib/dm-is-predefined.rb
|
118
|
+
- lib/dm-is-predefined/is/exceptions/unknown_resource.rb
|
119
|
+
- lib/dm-is-predefined/is/predefined.rb
|
120
|
+
- spec/integration/models/test_model.rb
|
121
|
+
- spec/integration/predefined_spec.rb
|
122
|
+
- spec/spec_helper.rb
|
123
|
+
has_rdoc: yard
|
124
|
+
homepage: http://github.com/postmodern/dm-is-predefined
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
post_install_message:
|
128
|
+
rdoc_options: []
|
129
|
+
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
none: false
|
134
|
+
requirements:
|
135
|
+
- - ">="
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
hash: 76076225
|
138
|
+
segments:
|
139
|
+
- 0
|
140
|
+
version: "0"
|
141
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
142
|
+
none: false
|
143
|
+
requirements:
|
144
|
+
- - ">="
|
145
|
+
- !ruby/object:Gem::Version
|
146
|
+
segments:
|
147
|
+
- 0
|
148
|
+
version: "0"
|
149
|
+
requirements: []
|
150
|
+
|
151
|
+
rubyforge_project:
|
152
|
+
rubygems_version: 1.3.7
|
153
|
+
signing_key:
|
154
|
+
specification_version: 3
|
155
|
+
summary: A DataMapper plugin for adding predefined resources to Models.
|
156
|
+
test_files:
|
157
|
+
- spec/integration/models/test_model.rb
|
158
|
+
- spec/integration/predefined_spec.rb
|
159
|
+
- spec/spec_helper.rb
|