scopie 1.0.4 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +5 -1
- data/lib/scopie.rb +16 -2
- data/lib/scopie/base.rb +59 -6
- data/lib/scopie/invalid_type_error.rb +13 -0
- data/lib/scopie/value.rb +13 -14
- data/lib/scopie/version.rb +1 -1
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d0543b2ee6bdc2a9394156391427b022282d8669
|
4
|
+
data.tar.gz: 73a8eee4d9b96d7fae75907bb353f6842aa6b755
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4ea45fea31644d7f1d0d02a4c3054abb02f7646c5ac0aa95f342793c9c8d03cf83ecb561c7234d2ef8ea2d126358eb1478d4ef8dea747db3b86cc9868480fcb9
|
7
|
+
data.tar.gz: 24fbffa8dfb94e38d017489c24e7cc39bb9dbf92aebec3ff7e5394c0db367b1111be87ce530dd3c17584856f78324188d06da303fbe5c59e74b75c1c6a1408e3
|
data/README.md
CHANGED
@@ -90,7 +90,7 @@ Now, if you want to apply them to an specific resource, you just need to call `a
|
|
90
90
|
class GraduationsController < ApplicationController
|
91
91
|
|
92
92
|
def index
|
93
|
-
@graduations = Scopie.apply_scopes(Graduation, method: :index, scopie:
|
93
|
+
@graduations = Scopie.apply_scopes(Graduation, method: :index, scopie: GraduationsScopie.new).all
|
94
94
|
end
|
95
95
|
|
96
96
|
end
|
@@ -123,6 +123,10 @@ Add `scopie` to your Gemfile or install it from Rubygems.
|
|
123
123
|
gem 'scopie'
|
124
124
|
```
|
125
125
|
|
126
|
+
## Mutation testing
|
127
|
+
|
128
|
+
mutant --include lib --require scopie --use rspec Scopie*
|
129
|
+
|
126
130
|
## Options
|
127
131
|
|
128
132
|
Scopie supports several options:
|
data/lib/scopie.rb
CHANGED
@@ -1,17 +1,31 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Scopie
|
4
|
-
class InvalidOptionError < StandardError; end
|
5
|
-
|
6
4
|
RESULTS_TO_IGNORE = [true, false].freeze
|
7
5
|
|
6
|
+
require 'scopie/version'
|
7
|
+
require 'scopie/invalid_type_error'
|
8
8
|
require 'scopie/value'
|
9
9
|
require 'scopie/base'
|
10
10
|
|
11
|
+
# Receives an object where scopes will be applied to.
|
12
|
+
#
|
13
|
+
# class GraduationsScopie < Scopie::Base
|
14
|
+
# has_scope :featured, type: :boolean
|
15
|
+
# has_scope :by_degree, :by_period
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# class GraduationsController < ApplicationController
|
19
|
+
# def index
|
20
|
+
# @graduations = Scopie.apply_scopes(Graduation, method: :index, scopie: GraduationsScopie.new).all
|
21
|
+
# end
|
22
|
+
# end
|
23
|
+
#
|
11
24
|
def self.apply_scopes(target, hash, method: nil, scopie: Scopie::Base.new)
|
12
25
|
scopie.apply_scopes(target, hash, method)
|
13
26
|
end
|
14
27
|
|
28
|
+
# Returns the scopes used in this action.
|
15
29
|
def self.current_scopes(hash, method: nil, scopie: Scopie::Base.new)
|
16
30
|
scopie.current_scopes(hash, method)
|
17
31
|
end
|
data/lib/scopie/base.rb
CHANGED
@@ -10,8 +10,46 @@ class Scopie::Base
|
|
10
10
|
self.class.scopes_configuration
|
11
11
|
end
|
12
12
|
|
13
|
+
# Detects params from url and apply as scopes to your classes.
|
14
|
+
#
|
15
|
+
# == Options
|
16
|
+
#
|
17
|
+
# * <tt>:type</tt> - Coerces the type of the parameter sent.
|
18
|
+
#
|
19
|
+
# * <tt>:only</tt> - In which actions the scope is applied.
|
20
|
+
#
|
21
|
+
# * <tt>:except</tt> - In which actions the scope is not applied.
|
22
|
+
#
|
23
|
+
# * <tt>:as</tt> - The key in the params hash expected to find the scope.
|
24
|
+
# Defaults to the scope name.
|
25
|
+
#
|
26
|
+
# * <tt>:default</tt> - Default value for the scope. Whenever supplied the scope
|
27
|
+
# is always called.
|
28
|
+
#
|
29
|
+
# * <tt>:allow_blank</tt> - Blank values are not sent to scopes by default. Set to true to overwrite.
|
30
|
+
#
|
31
|
+
# == Method usage
|
32
|
+
#
|
33
|
+
# You can also define a method having the same name as a scope. The current scope, value and params are yielded
|
34
|
+
# to the block so the user can apply the scope on its own. The method can return new scope or the boolean value.
|
35
|
+
# In the latter case will be used not modified scope. This is useful in case we
|
36
|
+
# need to manipulate the given value:
|
37
|
+
#
|
38
|
+
# has_scope :category
|
39
|
+
#
|
40
|
+
# def category(scope, value, _hash)
|
41
|
+
# value != 'all' && scope.by_category(value)
|
42
|
+
# end
|
43
|
+
#
|
44
|
+
# has_scope :not_voted_by_me, type: :boolean
|
45
|
+
#
|
46
|
+
# def not_voted_by_me(scope, _value, _hash)
|
47
|
+
# scope.not_voted_by(controller.current_user.id) # The controller method is available in the scopie_rails gem
|
48
|
+
# end
|
49
|
+
#
|
13
50
|
def self.has_scope(*scopes, **options)
|
14
51
|
@scopes_configuration ||= {}
|
52
|
+
normalize_options!(options)
|
15
53
|
|
16
54
|
scopes.each do |scope|
|
17
55
|
@scopes_configuration[scope.to_sym] = options
|
@@ -47,8 +85,11 @@ class Scopie::Base
|
|
47
85
|
target.public_send(scope_name, value)
|
48
86
|
end
|
49
87
|
|
50
|
-
|
51
|
-
|
88
|
+
if Scopie::RESULTS_TO_IGNORE.include?(result)
|
89
|
+
target
|
90
|
+
else
|
91
|
+
result
|
92
|
+
end
|
52
93
|
end
|
53
94
|
|
54
95
|
def key_name(scope_name, options)
|
@@ -78,12 +119,13 @@ class Scopie::Base
|
|
78
119
|
|
79
120
|
def method_applicable?(method, options)
|
80
121
|
return true unless method
|
122
|
+
action = method.to_s
|
81
123
|
|
82
|
-
methods_white_list =
|
83
|
-
methods_black_list =
|
124
|
+
methods_white_list = options[:only]
|
125
|
+
methods_black_list = options[:except]
|
84
126
|
|
85
|
-
return false if methods_black_list.include?(
|
86
|
-
return false if methods_white_list.any? && !methods_white_list.include?(
|
127
|
+
return false if methods_black_list.include?(action)
|
128
|
+
return false if methods_white_list.any? && !methods_white_list.include?(action)
|
87
129
|
|
88
130
|
true
|
89
131
|
end
|
@@ -94,4 +136,15 @@ class Scopie::Base
|
|
94
136
|
|
95
137
|
private_class_method :reset_scopes_configuration!
|
96
138
|
|
139
|
+
def self.normalize_options!(options)
|
140
|
+
[:only, :except].each do |key|
|
141
|
+
options[key] = Array(options[key]).map(&:to_s)
|
142
|
+
options[key].reject!(&:empty?)
|
143
|
+
end
|
144
|
+
|
145
|
+
options
|
146
|
+
end
|
147
|
+
|
148
|
+
private_class_method :normalize_options!
|
149
|
+
|
97
150
|
end
|
data/lib/scopie/value.rb
CHANGED
@@ -11,14 +11,24 @@ class Scopie::Value
|
|
11
11
|
end
|
12
12
|
|
13
13
|
def raw
|
14
|
-
|
15
|
-
fetch_default
|
14
|
+
@hash.fetch(@key_name) { fetch_default }
|
16
15
|
end
|
17
16
|
|
18
17
|
def coerced
|
19
18
|
coerce_to_type(raw, fetch_type)
|
20
19
|
end
|
21
20
|
|
21
|
+
def given?
|
22
|
+
key_passed? || has_default?
|
23
|
+
end
|
24
|
+
|
25
|
+
def present?
|
26
|
+
value = raw
|
27
|
+
value.respond_to?(:empty?) ? !value.empty? : !!value
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
22
32
|
def fetch_type
|
23
33
|
@options[:type]
|
24
34
|
end
|
@@ -31,27 +41,16 @@ class Scopie::Value
|
|
31
41
|
@options.key?(:default)
|
32
42
|
end
|
33
43
|
|
34
|
-
def given?
|
35
|
-
key_passed? || has_default?
|
36
|
-
end
|
37
|
-
|
38
44
|
def key_passed?
|
39
45
|
@hash.key?(@key_name)
|
40
46
|
end
|
41
47
|
|
42
|
-
def present?
|
43
|
-
value = raw
|
44
|
-
value.respond_to?(:empty?) ? !value.empty? : !!value
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
48
|
def coerce_to_type(value, type)
|
50
49
|
return value unless type
|
51
50
|
|
52
51
|
coercion_method_name = "coerce_to_#{type}"
|
53
52
|
|
54
|
-
respond_to?(coercion_method_name, true) || raise(Scopie::
|
53
|
+
respond_to?(coercion_method_name, true) || raise(Scopie::InvalidTypeError.new(type))
|
55
54
|
|
56
55
|
send(coercion_method_name, value)
|
57
56
|
end
|
data/lib/scopie/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scopie
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yury Kotov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-07-04 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Minimal mapping of incoming parameters to named scopes in your resources
|
14
14
|
through OO design and pure Ruby classes
|
@@ -22,6 +22,7 @@ files:
|
|
22
22
|
- README.md
|
23
23
|
- lib/scopie.rb
|
24
24
|
- lib/scopie/base.rb
|
25
|
+
- lib/scopie/invalid_type_error.rb
|
25
26
|
- lib/scopie/value.rb
|
26
27
|
- lib/scopie/version.rb
|
27
28
|
homepage: https://github.com/beorc/scopie
|
@@ -44,8 +45,9 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
45
|
version: '0'
|
45
46
|
requirements: []
|
46
47
|
rubyforge_project:
|
47
|
-
rubygems_version: 2.
|
48
|
+
rubygems_version: 2.6.4
|
48
49
|
signing_key:
|
49
50
|
specification_version: 4
|
50
51
|
summary: Maps HTTP-parameters to your resource scopes
|
51
52
|
test_files: []
|
53
|
+
has_rdoc:
|