scopie 1.0.4 → 1.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b007c093187a56143ba81168c7c5b2c5b9aed7c1
4
- data.tar.gz: 71e9a5ad63b40ae3a066326cd39038f507a38086
3
+ metadata.gz: d0543b2ee6bdc2a9394156391427b022282d8669
4
+ data.tar.gz: 73a8eee4d9b96d7fae75907bb353f6842aa6b755
5
5
  SHA512:
6
- metadata.gz: fa6aeb12cf0a6fac877ee7adf85efff85c8f921140e86e40e9f837933d73444e8d899daa384b5e5944ea2f2c58a8b5afb0daa0fdb97ef21fbb447a805e4d100d
7
- data.tar.gz: d0f0529ff9a2f70398913b3b1a99161305a0fa9185db1f615f80fb53103654b687832f0c1e9baecce0d01ddeace6e99dc3672f8e14f076dcbf583bf3c60fff24
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: Scopies::GraduationsScopie.new).all
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:
@@ -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
@@ -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
- return target if Scopie::RESULTS_TO_IGNORE.include?(result)
51
- result
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 = Array(options[:only])
83
- methods_black_list = Array(options[:except])
124
+ methods_white_list = options[:only]
125
+ methods_black_list = options[:except]
84
126
 
85
- return false if methods_black_list.include?(method)
86
- return false if methods_white_list.any? && !methods_white_list.include?(method)
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
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Scopie::InvalidTypeError < StandardError
4
+
5
+ def initialize(type)
6
+ @type = type
7
+ end
8
+
9
+ def message
10
+ "Unknown value for option 'type' provided: :#{@type}"
11
+ end
12
+
13
+ end
@@ -11,14 +11,24 @@ class Scopie::Value
11
11
  end
12
12
 
13
13
  def raw
14
- return @hash[@key_name] if @hash.key?(@key_name)
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::InvalidOptionError, "Unknown value for option 'type' provided: :#{type}")
53
+ respond_to?(coercion_method_name, true) || raise(Scopie::InvalidTypeError.new(type))
55
54
 
56
55
  send(coercion_method_name, value)
57
56
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Scopie
4
- VERSION = '1.0.4'
4
+ VERSION = '1.0.5'
5
5
  end
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
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-06-28 00:00:00.000000000 Z
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.5.1
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: