nifty_scope 0.0.1 → 0.0.2

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: 9a0aa3cbcad77bb64485de13d8efcfd7c4afafe8
4
- data.tar.gz: ea24b5f05443b66094468902b284efb579622a75
3
+ metadata.gz: 413d2743ff2f7a25c565f79561e840a69cf96c75
4
+ data.tar.gz: 1643762b2c0195271adcb4b6820c8e07f226a01f
5
5
  SHA512:
6
- metadata.gz: de5afeb537ae8badb0d8a118dc2d1e83ea2aed2c973e6d59390a9034809814f43ce1a7ab17acd3184b506c440c9c7e12a84a8539182bf591df54e526ef1e75e5
7
- data.tar.gz: b8ca5095cb673a3490a8fdaa9fce3b0193c7cdf3d52405f8f42c1e77617726d5e5401b12ba39279d77cbaa56dc0ac8ab09b27efb6a43d85bd542f3d2e94c2336
6
+ metadata.gz: d28cf65a4f73dd0ac69481924c17408b34f11d1892d2a20c0cd5419489fd4d539388e91ae95f80bcc8823fcf7176b68708fe826563d41fa27dbdf36374ca1c70
7
+ data.tar.gz: ea0e360afb95dbc9ba994c442d861e30f475950c923aa3e7a296f0dda28247eb4dc27822723d37976a1a390cb3cb55828156e7356d517d78251d3f441bc5bbdc
data/lib/nifty_scope.rb CHANGED
@@ -1,44 +1,14 @@
1
- require "nifty_scope/version"
1
+ require 'nifty_scope/scope'
2
+ require 'nifty_scope/version'
2
3
 
3
4
  module NiftyScope
4
5
  extend ActiveSupport::Concern
5
6
 
6
7
  module ClassMethods
7
8
  def nifty_scope(params, options = {})
8
- if options.has_key?(:only)
9
- options[:only] = Array(options[:only])
10
- params.slice!(*options[:only])
11
- elsif options.has_key?(:except)
12
- options[:except] = Array(options[:except])
13
- params.except!(*options[:except])
14
- end
15
-
16
- mapping ||= options[:mapping] || {}
17
- scope = where(nil)
18
-
19
- params.each do |(key, value)|
20
- key = key.to_sym
21
- scope_method = if mapping.has_key?(key)
22
- mapping[key]
23
- else
24
- key
25
- end
26
- if scope_method.is_a?(Symbol) && !scope.respond_to?(scope_method)
27
- raise IrresponsibleScope, "#{scope.name} can't respond to ##{scope_method} method"
28
- end
29
-
30
- scope = if scope_method.is_a?(Proc)
31
- scope.instance_exec(value, &scope_method)
32
- else
33
- scope.send(scope_method, value)
34
- end
35
- end
36
- scope
9
+ Scope.new(where(nil), params, options).apply
37
10
  end
38
11
  end
39
-
40
- class IrresponsibleScope < Exception
41
- end
42
12
  end
43
13
 
44
14
  ActiveRecord::Base.class_eval do
@@ -0,0 +1,4 @@
1
+ module NiftyScope
2
+ class IrresponsibleScope < Exception
3
+ end
4
+ end
@@ -0,0 +1,52 @@
1
+ require 'nifty_scope/irresponsible_scope'
2
+
3
+ module NiftyScope
4
+ class Scope
5
+ def initialize(scope, params, options)
6
+ @scope = scope
7
+ @mapping = options[:mapping] || {}
8
+ @params = filter(params, options)
9
+ end
10
+
11
+ def apply
12
+ @params.each do |key, value|
13
+ scope_method = scope_method_for(key.to_sym)
14
+ ensure_scope_responds_to(scope_method)
15
+
16
+ @scope = eval_scope(scope_method, scope_args(value))
17
+ end
18
+
19
+ @scope
20
+ end
21
+
22
+ private
23
+
24
+ def eval_scope(method, args)
25
+ if method.is_a?(Proc)
26
+ @scope.instance_exec(*args, &method)
27
+ else
28
+ @scope.send(method, *args)
29
+ end
30
+ end
31
+
32
+ def scope_args(*args)
33
+ args.delete_if { |arg| !!arg == arg }
34
+ end
35
+
36
+ def scope_method_for(key)
37
+ @mapping[key] || key
38
+ end
39
+
40
+ def ensure_scope_responds_to(method)
41
+ if method.is_a?(Symbol) && !@scope.respond_to?(method)
42
+ raise IrresponsibleScope, "#{@scope.name} can't respond to ##{method} method"
43
+ end
44
+ end
45
+
46
+ def filter(params, options)
47
+ return params.slice(*options[:only]) if options.has_key?(:only)
48
+ return params.except(*options[:except]) if options.has_key?(:except)
49
+ params
50
+ end
51
+ end
52
+ end
@@ -1,3 +1,3 @@
1
1
  module NiftyScope
2
- VERSION = "0.0.1"
2
+ VERSION = '0.0.2'
3
3
  end
@@ -1,21 +1,44 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe NiftyScope do
4
-
5
4
  before(:all) do
6
- params = { name: 'cunt' }
7
-
8
- create_list(:deer, 3)
5
+ create(:deer)
6
+ create(:deer, :dead => true)
9
7
  create(:deer, :name => 'cunt')
8
+ end
9
+
10
+ context 'with parametrized scope' do
11
+ let!(:params) { { name: 'cunt' } }
12
+ let!(:deers) do
13
+ Deer.nifty_scope(params,
14
+ mapping: {
15
+ name: ->(name) { with_name(name) }
16
+ }, only: [:name]
17
+ )
18
+ end
10
19
 
11
- @deers = Deer.nifty_scope(params,
12
- mapping: {
13
- name: ->(name) { with_name(name) }
14
- }, only: [:name]
15
- )
20
+ it 'calls appropriate scope method' do
21
+ expect(deers.count).to eq(1)
22
+ end
16
23
  end
17
24
 
18
- it 'calls appropriate scope method' do
19
- expect(@deers.count).to eq(1)
25
+ context 'with parameterless scope' do
26
+ let!(:params) { { :dead => true } }
27
+
28
+ context 'when mapping is specified' do
29
+ let!(:deers) { Deer.nifty_scope(params, only: [:dead]) }
30
+
31
+ it 'calls appropriate scope method' do
32
+ expect(deers.count).to eq(1)
33
+ end
34
+ end
35
+
36
+ context 'when mapping is not specified' do
37
+ let!(:deers) { Deer.nifty_scope(params) }
38
+
39
+ it 'calls appropriate scope method' do
40
+ expect(deers.count).to eq(1)
41
+ end
42
+ end
20
43
  end
21
44
  end
@@ -1,4 +1,7 @@
1
1
  class Deer < ActiveRecord::Base
2
- scope :over_21, -> { where('age > 21') }
3
- scope :with_name, ->(name) { where(:name => name) }
2
+ scope :over_21, -> { where('age > 21') }
3
+ scope :with_name, ->(name) { where(name: name) }
4
+
5
+ scope :dead, -> { where(dead: true) }
6
+ scope :alive, -> { where(dead: false) }
4
7
  end
@@ -5,6 +5,7 @@ ActiveRecord::Schema.define do
5
5
  t.string :name
6
6
  t.integer :age
7
7
  t.integer :weight
8
+ t.boolean :dead, null: false, default: false
8
9
 
9
10
  t.timestamps
10
11
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nifty_scope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vasiliy Yorkin, Alexandr Shuhin
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-03-17 00:00:00.000000000 Z
11
+ date: 2014-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -149,6 +149,8 @@ files:
149
149
  - README.md
150
150
  - Rakefile
151
151
  - lib/nifty_scope.rb
152
+ - lib/nifty_scope/irresponsible_scope.rb
153
+ - lib/nifty_scope/scope.rb
152
154
  - lib/nifty_scope/version.rb
153
155
  - nifty_scope.gemspec
154
156
  - spec/nifty_scope/nifty_scope_spec.rb
@@ -186,3 +188,4 @@ test_files:
186
188
  - spec/nifty_scope/support/models.rb
187
189
  - spec/nifty_scope/support/schema.rb
188
190
  - spec/spec_helper.rb
191
+ has_rdoc: