dm-default-scope 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gemspec +12 -0
- data/LICENSE +20 -0
- data/README.md +32 -0
- data/lib/dm-default-scope.rb +29 -0
- metadata +65 -0
data/.gemspec
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
Gem::Specification.new do |gem|
|
2
|
+
gem.name = "dm-default-scope"
|
3
|
+
gem.version = "0.0.1"
|
4
|
+
gem.summary = "Default scope in datamapper"
|
5
|
+
gem.description = "Default scope in datamapper"
|
6
|
+
gem.authors = ["CarlosIPe"]
|
7
|
+
gem.email = ["carlos2@compendium.com.ar"]
|
8
|
+
gem.homepage = "http://github.com/carlosipe/dm-default-scope"
|
9
|
+
gem.files = ["LICENSE","README.md","lib/dm-default-scope.rb",".gemspec"]
|
10
|
+
|
11
|
+
gem.add_runtime_dependency('dm-core', '<= 1.2')
|
12
|
+
end
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2013 CarlosIPe
|
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/README.md
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# Use a default scope with datamapper
|
2
|
+
|
3
|
+
DataMapper does not provide a way to set a default scope in a model.
|
4
|
+
If you need, for example, to handle soft deletes, it is a must to have
|
5
|
+
a default scope with :deleted =>false as a condition.
|
6
|
+
|
7
|
+
This library recreates the absolutely minimal feature for setting a default
|
8
|
+
scope
|
9
|
+
|
10
|
+
## Usage
|
11
|
+
|
12
|
+
class User
|
13
|
+
include DataMapper::Resource
|
14
|
+
include DataMapper::DefaultScope
|
15
|
+
|
16
|
+
property :id, Serial
|
17
|
+
property :email, String
|
18
|
+
property :deleted, Boolean
|
19
|
+
|
20
|
+
set_default_scope({:deleted=> false})
|
21
|
+
end
|
22
|
+
|
23
|
+
User.create(email: 'john@doe.com', deleted: true)
|
24
|
+
User.get(1) #=> nil
|
25
|
+
|
26
|
+
##Unscoped
|
27
|
+
|
28
|
+
If you need to bypass the default scope you can do it inside an unscoped block:
|
29
|
+
|
30
|
+
User.unscoped do
|
31
|
+
User.get(1) #=> <User @id=1 @email="john@doe.com" @deleted=true>
|
32
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module DataMapper
|
2
|
+
module DefaultScope
|
3
|
+
def self.included(base)
|
4
|
+
base.send(:extend, ClassMethods)
|
5
|
+
end
|
6
|
+
|
7
|
+
module ClassMethods
|
8
|
+
def default_scope(*args)
|
9
|
+
return Thread.current[scope_key] if Thread.current[scope_key]
|
10
|
+
return @default_scope ||{}
|
11
|
+
end
|
12
|
+
|
13
|
+
def set_default_scope(scope)
|
14
|
+
@default_scope = scope
|
15
|
+
end
|
16
|
+
|
17
|
+
def unscoped
|
18
|
+
Thread.current[scope_key] = {}
|
19
|
+
result = yield
|
20
|
+
ensure
|
21
|
+
Thread.current[scope_key] = nil
|
22
|
+
end
|
23
|
+
|
24
|
+
def scope_key
|
25
|
+
"dm_default_scope_#{self}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: dm-default-scope
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- CarlosIPe
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-09-28 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: dm-core
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - <=
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '1.2'
|
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: '1.2'
|
30
|
+
description: Default scope in datamapper
|
31
|
+
email:
|
32
|
+
- carlos2@compendium.com.ar
|
33
|
+
executables: []
|
34
|
+
extensions: []
|
35
|
+
extra_rdoc_files: []
|
36
|
+
files:
|
37
|
+
- LICENSE
|
38
|
+
- README.md
|
39
|
+
- lib/dm-default-scope.rb
|
40
|
+
- .gemspec
|
41
|
+
homepage: http://github.com/carlosipe/dm-default-scope
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project:
|
61
|
+
rubygems_version: 1.8.23
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: Default scope in datamapper
|
65
|
+
test_files: []
|