dm-predefined 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/History.txt +4 -0
- data/LICENSE.txt +20 -0
- data/Manifest.txt +13 -0
- data/README.txt +63 -0
- data/Rakefile +15 -0
- data/TODO.txt +0 -0
- data/lib/dm-predefined.rb +20 -0
- data/lib/dm-predefined/exceptions/unknown_resource.rb +6 -0
- data/lib/dm-predefined/predefined.rb +47 -0
- data/lib/dm-predefined/version.rb +5 -0
- data/spec/integration/predefined_spec.rb +75 -0
- data/spec/spec.opts +1 -0
- data/spec/spec_helper.rb +26 -0
- metadata +90 -0
data/History.txt
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Hal Brodigan
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest.txt
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
History.txt
|
2
|
+
LICENSE.txt
|
3
|
+
Manifest.txt
|
4
|
+
README.txt
|
5
|
+
Rakefile
|
6
|
+
TODO.txt
|
7
|
+
lib/dm-predefined.rb
|
8
|
+
lib/dm-predefined/predefined.rb
|
9
|
+
lib/dm-predefined/version.rb
|
10
|
+
lib/dm-predefined/exceptions/unknown_resource.rb
|
11
|
+
spec/integration/predefined_spec.rb
|
12
|
+
spec/spec.opts
|
13
|
+
spec/spec_helper.rb
|
data/README.txt
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
= dm-predefined
|
2
|
+
|
3
|
+
* http://dm-predefined.rubyforge.org/
|
4
|
+
* https://github.com/postmodern/dm-predefined
|
5
|
+
* Postmodern (postmodern.mod3 at gmail.com)
|
6
|
+
|
7
|
+
== DESCRIPTION:
|
8
|
+
|
9
|
+
A DataMapper plugin for adding predefined resources to Models.
|
10
|
+
|
11
|
+
== EXAMPLES:
|
12
|
+
|
13
|
+
require 'dm-core'
|
14
|
+
require 'dm-predefined'
|
15
|
+
|
16
|
+
class Licence
|
17
|
+
|
18
|
+
include DataMapper::Resource
|
19
|
+
include DataMapper::Predefined
|
20
|
+
|
21
|
+
# Name of the Licence
|
22
|
+
property :name, String
|
23
|
+
|
24
|
+
# URL to the licence
|
25
|
+
property :url, String
|
26
|
+
|
27
|
+
define :gpl2, :name => 'GPL-2', :url => 'http://www.gnu.org/copyleft/gpl.html'
|
28
|
+
define :mit, :name => 'MIT'
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
Licence.gpl2
|
33
|
+
# => #<Licence: id: 1, name: "GPL-2", url: "http://www.gnu.org/copyleft/gpl.html">
|
34
|
+
|
35
|
+
Licence[:mit]
|
36
|
+
# => #<Licence: id: 2, name: "MIT">
|
37
|
+
|
38
|
+
== INSTALL:
|
39
|
+
|
40
|
+
$ sudo gem install dm-predefined
|
41
|
+
|
42
|
+
== LICENSE:
|
43
|
+
|
44
|
+
Copyright (c) 2008 Hal Brodigan
|
45
|
+
|
46
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
47
|
+
a copy of this software and associated documentation files (the
|
48
|
+
"Software"), to deal in the Software without restriction, including
|
49
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
50
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
51
|
+
permit persons to whom the Software is furnished to do so, subject to
|
52
|
+
the following conditions:
|
53
|
+
|
54
|
+
The above copyright notice and this permission notice shall be
|
55
|
+
included in all copies or substantial portions of the Software.
|
56
|
+
|
57
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
58
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
59
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
60
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
61
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
62
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
63
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
# -*- ruby -*-
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'hoe'
|
5
|
+
require './tasks/spec.rb'
|
6
|
+
require './lib/dm-predefined/version.rb'
|
7
|
+
|
8
|
+
Hoe.new('dm-predefined', DataMapper::Predefined::VERSION) do |p|
|
9
|
+
p.rubyforge_name = 'dm-predefined'
|
10
|
+
p.developer('Postmodern','postmodern.mod3@gmail.com')
|
11
|
+
p.remote_rdoc_dir = ''
|
12
|
+
p.extra_deps = [['dm-core', '>=0.9.8']]
|
13
|
+
end
|
14
|
+
|
15
|
+
# vim: syntax=Ruby
|
data/TODO.txt
ADDED
File without changes
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# Needed to import datamapper and other gems
|
2
|
+
require 'rubygems'
|
3
|
+
require 'pathname'
|
4
|
+
|
5
|
+
# Add all external dependencies for the plugin here
|
6
|
+
gem 'dm-core', '~>0.9.8'
|
7
|
+
require 'dm-core'
|
8
|
+
|
9
|
+
# Require plugin-files
|
10
|
+
require Pathname(__FILE__).dirname.expand_path / 'dm-predefined' / 'predefined.rb'
|
11
|
+
|
12
|
+
|
13
|
+
# Include the plugin in Resource
|
14
|
+
module DataMapper
|
15
|
+
module Resource
|
16
|
+
module ClassMethods
|
17
|
+
include DataMapper::Predefined
|
18
|
+
end # module ClassMethods
|
19
|
+
end # module Resource
|
20
|
+
end # module DataMapper
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'dm-predefined/exceptions/unknown_resource'
|
2
|
+
|
3
|
+
module DataMapper
|
4
|
+
module Predefined
|
5
|
+
##
|
6
|
+
# fired when your plugin gets included into Resource
|
7
|
+
#
|
8
|
+
def self.included(base)
|
9
|
+
base.extend DataMapper::Predefined::ClassMethods
|
10
|
+
end
|
11
|
+
|
12
|
+
module ClassMethods
|
13
|
+
def [](name)
|
14
|
+
name = name.to_sym
|
15
|
+
attributes = predefined_attributes[name]
|
16
|
+
|
17
|
+
unless attributes
|
18
|
+
raise(UnknownResource,"the resource '#{name}' was not predefined",caller)
|
19
|
+
end
|
20
|
+
|
21
|
+
first_or_create(predefined_attributes[name.to_sym])
|
22
|
+
end
|
23
|
+
|
24
|
+
protected
|
25
|
+
|
26
|
+
def predefined_attributes
|
27
|
+
@@predefined_attributes ||= {}
|
28
|
+
end
|
29
|
+
|
30
|
+
def define(name,attributes={})
|
31
|
+
name = name.to_s
|
32
|
+
|
33
|
+
predefined_attributes[name.to_sym] = attributes
|
34
|
+
|
35
|
+
class_eval %{
|
36
|
+
class << self
|
37
|
+
define_method(#{name.dump}) do
|
38
|
+
self[#{name.dump}]
|
39
|
+
end
|
40
|
+
end
|
41
|
+
}
|
42
|
+
|
43
|
+
attributes
|
44
|
+
end
|
45
|
+
end # ClassMethods
|
46
|
+
end # Predefined
|
47
|
+
end # DataMapper
|
@@ -0,0 +1,75 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require Pathname(__FILE__).dirname.expand_path.parent + 'spec_helper'
|
3
|
+
|
4
|
+
if (HAS_SQLITE3 || HAS_MYSQL || HAS_POSTGRES)
|
5
|
+
describe 'DataMapper::Predefined' do
|
6
|
+
before(:all) do
|
7
|
+
class TestModel
|
8
|
+
|
9
|
+
include DataMapper::Resource
|
10
|
+
include DataMapper::Predefined
|
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
|
+
define :test1, :name => 'test1',
|
25
|
+
:number => 1,
|
26
|
+
:optional => 'yo',
|
27
|
+
:body => %{This is a test.}
|
28
|
+
|
29
|
+
define :test2, :name => 'test2', :number => 2
|
30
|
+
end
|
31
|
+
|
32
|
+
TestModel.auto_migrate!
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should be able to define resources of a Model" do
|
36
|
+
test1 = TestModel.test1
|
37
|
+
|
38
|
+
test1.should_not be_nil
|
39
|
+
test1.name.should == 'test1'
|
40
|
+
test1.number.should == 1
|
41
|
+
test1.optional.should == 'yo'
|
42
|
+
test1.body.should == %{This is a test.}
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should be able to define resources with empty attributes" do
|
46
|
+
test2 = TestModel.test2
|
47
|
+
|
48
|
+
test2.should_not be_nil
|
49
|
+
test2.name.should == 'test2'
|
50
|
+
test2.number.should == 2
|
51
|
+
test2.optional.should == 'hello'
|
52
|
+
test2.body.should be_nil
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should return existing resources" do
|
56
|
+
first_test1 = TestModel.test1
|
57
|
+
second_test1 = TestModel.test1
|
58
|
+
|
59
|
+
first_test1.id.should == second_test1.id
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should provide a generic interface for accessing resources" do
|
63
|
+
test1 = TestModel[:test1]
|
64
|
+
|
65
|
+
test1.name.should == 'test1'
|
66
|
+
test1.number.should == 1
|
67
|
+
end
|
68
|
+
|
69
|
+
it "should raise an UnknownResource exception when accessing undefined resources" do
|
70
|
+
lambda {
|
71
|
+
TestModel[:test3]
|
72
|
+
}.should raise_error(DataMapper::Predefined::UnknownResource)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
data/spec/spec.opts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--colour
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
require 'pathname'
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
gem 'rspec', '~>1.1.11'
|
5
|
+
require 'spec'
|
6
|
+
|
7
|
+
require Pathname(__FILE__).dirname.expand_path.parent + 'lib/dm-predefined'
|
8
|
+
|
9
|
+
def load_driver(name, default_uri)
|
10
|
+
return false if ENV['ADAPTER'] != name.to_s
|
11
|
+
|
12
|
+
begin
|
13
|
+
DataMapper.setup(name, ENV["#{name.to_s.upcase}_SPEC_URI"] || default_uri)
|
14
|
+
DataMapper::Repository.adapters[:default] = DataMapper::Repository.adapters[name]
|
15
|
+
true
|
16
|
+
rescue LoadError => e
|
17
|
+
warn "Could not load do_#{name}: #{e}"
|
18
|
+
false
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
ENV['ADAPTER'] ||= 'sqlite3'
|
23
|
+
|
24
|
+
HAS_SQLITE3 = load_driver(:sqlite3, 'sqlite3::memory:')
|
25
|
+
HAS_MYSQL = load_driver(:mysql, 'mysql://localhost/dm_core_test')
|
26
|
+
HAS_POSTGRES = load_driver(:postgres, 'postgres://postgres@localhost/dm_core_test')
|
metadata
ADDED
@@ -0,0 +1,90 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-predefined
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Postmodern
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-12-21 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: dm-core
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: 0.9.8
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: hoe
|
27
|
+
type: :development
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.8.2
|
34
|
+
version:
|
35
|
+
description: A DataMapper plugin for adding predefined resources to Models.
|
36
|
+
email:
|
37
|
+
- postmodern.mod3@gmail.com
|
38
|
+
executables: []
|
39
|
+
|
40
|
+
extensions: []
|
41
|
+
|
42
|
+
extra_rdoc_files:
|
43
|
+
- History.txt
|
44
|
+
- LICENSE.txt
|
45
|
+
- Manifest.txt
|
46
|
+
- README.txt
|
47
|
+
- TODO.txt
|
48
|
+
files:
|
49
|
+
- History.txt
|
50
|
+
- LICENSE.txt
|
51
|
+
- Manifest.txt
|
52
|
+
- README.txt
|
53
|
+
- Rakefile
|
54
|
+
- TODO.txt
|
55
|
+
- lib/dm-predefined.rb
|
56
|
+
- lib/dm-predefined/predefined.rb
|
57
|
+
- lib/dm-predefined/version.rb
|
58
|
+
- lib/dm-predefined/exceptions/unknown_resource.rb
|
59
|
+
- spec/integration/predefined_spec.rb
|
60
|
+
- spec/spec.opts
|
61
|
+
- spec/spec_helper.rb
|
62
|
+
has_rdoc: true
|
63
|
+
homepage: http://dm-predefined.rubyforge.org/
|
64
|
+
post_install_message:
|
65
|
+
rdoc_options:
|
66
|
+
- --main
|
67
|
+
- README.txt
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: "0"
|
75
|
+
version:
|
76
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
77
|
+
requirements:
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: "0"
|
81
|
+
version:
|
82
|
+
requirements: []
|
83
|
+
|
84
|
+
rubyforge_project: dm-predefined
|
85
|
+
rubygems_version: 1.3.1
|
86
|
+
signing_key:
|
87
|
+
specification_version: 2
|
88
|
+
summary: A DataMapper plugin for adding predefined resources to Models.
|
89
|
+
test_files: []
|
90
|
+
|