sinatra-has_scope 0.0.2 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/Gemfile +1 -1
- data/lib/sinatra/has_scope.rb +68 -61
- data/lib/sinatra/has_scope/version.rb +1 -1
- metadata +32 -55
data/.gitignore
CHANGED
data/Gemfile
CHANGED
data/lib/sinatra/has_scope.rb
CHANGED
@@ -13,6 +13,11 @@ module Sinatra
|
|
13
13
|
|
14
14
|
attr_accessor :scopes_configuration
|
15
15
|
|
16
|
+
def self.registered(base)
|
17
|
+
base.scopes_configuration = { }
|
18
|
+
base.helpers HasScope::Helpers
|
19
|
+
end
|
20
|
+
|
16
21
|
# Detects params from url and apply as scopes to your classes.
|
17
22
|
#
|
18
23
|
# == Options
|
@@ -66,75 +71,77 @@ module Sinatra
|
|
66
71
|
end
|
67
72
|
end
|
68
73
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
74
|
+
module Helpers
|
75
|
+
# Receives an object where scopes will be applied to.
|
76
|
+
#
|
77
|
+
# has_scope :graduation, :featured, :type => true
|
78
|
+
# has_scope :graduation, :by_degree
|
79
|
+
#
|
80
|
+
# get '/graduations' do
|
81
|
+
# @graduations = apply_scopes(:graduation, Graduation, params).all
|
82
|
+
# end
|
83
|
+
#
|
84
|
+
def apply_scopes(scope_group, target, hash)
|
85
|
+
return target unless settings.scopes_configuration
|
86
|
+
|
87
|
+
if settings.scopes_configuration.key?(scope_group)
|
88
|
+
settings.scopes_configuration[scope_group].each do |scope, options|
|
89
|
+
key = options[:as].to_s
|
90
|
+
|
91
|
+
if hash.key?(key)
|
92
|
+
value, call_scope = hash[key], true
|
93
|
+
elsif options.key?(:default)
|
94
|
+
value, call_scope = options[:default], true
|
95
|
+
value = value.call(self) if value.is_a?(Proc)
|
96
|
+
end
|
97
|
+
|
98
|
+
value = parse_value(options[:type], key, value)
|
99
|
+
|
100
|
+
if call_scope && (value.present? || options[:allow_blank])
|
101
|
+
target = call_scope_by_type(options[:type], scope, target, value, options)
|
102
|
+
end
|
96
103
|
end
|
97
104
|
end
|
98
|
-
end
|
99
105
|
|
100
|
-
|
101
|
-
end
|
102
|
-
|
103
|
-
# Set the real value for the current scope if type check.
|
104
|
-
def parse_value(type, key, value) #:nodoc:
|
105
|
-
if type == :boolean
|
106
|
-
TRUE_VALUES.include?(value)
|
107
|
-
elsif value && ALLOWED_TYPES[type].none?{ |klass| value.is_a?(klass) }
|
108
|
-
raise "Expected type :#{type} in params[:#{key}], got #{value.class}"
|
109
|
-
else
|
110
|
-
value
|
106
|
+
target
|
111
107
|
end
|
112
|
-
end
|
113
108
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
109
|
+
# Set the real value for the current scope if type check.
|
110
|
+
def parse_value(type, key, value) #:nodoc:
|
111
|
+
if type == :boolean
|
112
|
+
TRUE_VALUES.include?(value)
|
113
|
+
elsif value && ALLOWED_TYPES[type].none?{ |klass| value.is_a?(klass) }
|
114
|
+
raise "Expected type :#{type} in params[:#{key}], got #{value.class}"
|
115
|
+
else
|
116
|
+
value
|
117
|
+
end
|
123
118
|
end
|
124
|
-
end
|
125
119
|
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
133
|
-
string_proc_or_symbol.call(self) == expected
|
134
|
-
when Symbol
|
135
|
-
send(string_proc_or_symbol) == expected
|
120
|
+
# Call the scope taking into account its type.
|
121
|
+
def call_scope_by_type(type, scope, target, value, options) #:nodoc:
|
122
|
+
if type == :boolean
|
123
|
+
target.send(scope)
|
124
|
+
elsif value && options.key?(:using)
|
125
|
+
value = value.values_at(*options[:using])
|
126
|
+
target.send(scope, *value)
|
136
127
|
else
|
137
|
-
|
128
|
+
target.send(scope, value)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
# Evaluates the scope options :if or :unless. Returns true if the proc
|
133
|
+
# method, or string evals to the expected value.
|
134
|
+
def applicable?(string_proc_or_symbol, expected) #:nodoc:
|
135
|
+
case string_proc_or_symbol
|
136
|
+
when String
|
137
|
+
eval(string_proc_or_symbol) == expected
|
138
|
+
when Proc
|
139
|
+
string_proc_or_symbol.call(self) == expected
|
140
|
+
when Symbol
|
141
|
+
send(string_proc_or_symbol) == expected
|
142
|
+
else
|
143
|
+
true
|
144
|
+
end
|
138
145
|
end
|
139
146
|
end
|
140
147
|
end
|
metadata
CHANGED
@@ -1,49 +1,39 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: sinatra-has_scope
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
5
|
-
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 0
|
9
|
-
- 2
|
10
|
-
version: 0.0.2
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Simon COURTOIS
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2012-11-03 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
22
15
|
name: bundler
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
25
17
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
hash: 23
|
30
|
-
segments:
|
31
|
-
- 1
|
32
|
-
- 0
|
33
|
-
- 0
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
34
21
|
version: 1.0.0
|
35
22
|
type: :development
|
36
|
-
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.0.0
|
37
30
|
description: HasScope readaptation for the Sinatra micro-framework
|
38
|
-
email:
|
31
|
+
email:
|
39
32
|
- scourtois@cubyx.fr
|
40
33
|
executables: []
|
41
|
-
|
42
34
|
extensions: []
|
43
|
-
|
44
35
|
extra_rdoc_files: []
|
45
|
-
|
46
|
-
files:
|
36
|
+
files:
|
47
37
|
- .gitignore
|
48
38
|
- Gemfile
|
49
39
|
- README.mdown
|
@@ -51,41 +41,28 @@ files:
|
|
51
41
|
- lib/sinatra/has_scope.rb
|
52
42
|
- lib/sinatra/has_scope/version.rb
|
53
43
|
- sinatra-has_scope.gemspec
|
54
|
-
has_rdoc: true
|
55
44
|
homepage: http://rubygems.org/gems/sinatra-has_scope
|
56
45
|
licenses: []
|
57
|
-
|
58
46
|
post_install_message:
|
59
47
|
rdoc_options: []
|
60
|
-
|
61
|
-
require_paths:
|
48
|
+
require_paths:
|
62
49
|
- lib
|
63
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
64
51
|
none: false
|
65
|
-
requirements:
|
66
|
-
- -
|
67
|
-
- !ruby/object:Gem::Version
|
68
|
-
|
69
|
-
|
70
|
-
- 0
|
71
|
-
version: "0"
|
72
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
57
|
none: false
|
74
|
-
requirements:
|
75
|
-
- -
|
76
|
-
- !ruby/object:Gem::Version
|
77
|
-
hash: 23
|
78
|
-
segments:
|
79
|
-
- 1
|
80
|
-
- 3
|
81
|
-
- 6
|
58
|
+
requirements:
|
59
|
+
- - ! '>='
|
60
|
+
- !ruby/object:Gem::Version
|
82
61
|
version: 1.3.6
|
83
62
|
requirements: []
|
84
|
-
|
85
63
|
rubyforge_project: sinatra-has_scope
|
86
|
-
rubygems_version: 1.
|
64
|
+
rubygems_version: 1.8.23
|
87
65
|
signing_key:
|
88
66
|
specification_version: 3
|
89
67
|
summary: HasScope equivalent for Sinatra
|
90
68
|
test_files: []
|
91
|
-
|