nifty_scope 0.0.1
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 +7 -0
- data/.gitignore +18 -0
- data/Gemfile +13 -0
- data/LICENSE +21 -0
- data/README.md +29 -0
- data/Rakefile +7 -0
- data/lib/nifty_scope/version.rb +3 -0
- data/lib/nifty_scope.rb +46 -0
- data/nifty_scope.gemspec +31 -0
- data/spec/nifty_scope/nifty_scope_spec.rb +21 -0
- data/spec/nifty_scope/support/factories.rb +7 -0
- data/spec/nifty_scope/support/models.rb +4 -0
- data/spec/nifty_scope/support/schema.rb +11 -0
- data/spec/spec_helper.rb +46 -0
- metadata +188 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 9a0aa3cbcad77bb64485de13d8efcfd7c4afafe8
|
4
|
+
data.tar.gz: ea24b5f05443b66094468902b284efb579622a75
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: de5afeb537ae8badb0d8a118dc2d1e83ea2aed2c973e6d59390a9034809814f43ce1a7ab17acd3184b506c440c9c7e12a84a8539182bf591df54e526ef1e75e5
|
7
|
+
data.tar.gz: b8ca5095cb673a3490a8fdaa9fce3b0193c7cdf3d52405f8f42c1e77617726d5e5401b12ba39279d77cbaa56dc0ac8ab09b27efb6a43d85bd542f3d2e94c2336
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2014 Vasiliy Yorkin
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# NiftyScope
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'nifty_scope'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install nifty_scope
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( http://github.com/<my-github-username>/nifty_scope/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create new Pull Request
|
data/Rakefile
ADDED
data/lib/nifty_scope.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require "nifty_scope/version"
|
2
|
+
|
3
|
+
module NiftyScope
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
module ClassMethods
|
7
|
+
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
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
class IrresponsibleScope < Exception
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
ActiveRecord::Base.class_eval do
|
45
|
+
include NiftyScope
|
46
|
+
end
|
data/nifty_scope.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'nifty_scope/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "nifty_scope"
|
8
|
+
spec.version = NiftyScope::VERSION
|
9
|
+
spec.authors = ["Vasiliy Yorkin, Alexandr Shuhin"]
|
10
|
+
spec.email = ["vasiliy.yorkin@gmail.com"]
|
11
|
+
spec.summary = %q{ Provides an easy way to map request parameters to scopes }
|
12
|
+
spec.description = %q{ Provides an easy way to map request parameters to scopes }
|
13
|
+
spec.homepage = "http://github.com/vyorkin/nifty_scope"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_runtime_dependency "activerecord", ">= 3.0.0"
|
22
|
+
spec.add_runtime_dependency "activesupport", ">= 3.0.0"
|
23
|
+
|
24
|
+
spec.add_development_dependency "bundler", "~> 1.5"
|
25
|
+
spec.add_development_dependency "rake"
|
26
|
+
spec.add_development_dependency "rspec"
|
27
|
+
spec.add_development_dependency "sqlite3"
|
28
|
+
spec.add_development_dependency "factory_girl"
|
29
|
+
spec.add_development_dependency "factory_girl_sequences"
|
30
|
+
spec.add_development_dependency "database_cleaner"
|
31
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe NiftyScope do
|
4
|
+
|
5
|
+
before(:all) do
|
6
|
+
params = { name: 'cunt' }
|
7
|
+
|
8
|
+
create_list(:deer, 3)
|
9
|
+
create(:deer, :name => 'cunt')
|
10
|
+
|
11
|
+
@deers = Deer.nifty_scope(params,
|
12
|
+
mapping: {
|
13
|
+
name: ->(name) { with_name(name) }
|
14
|
+
}, only: [:name]
|
15
|
+
)
|
16
|
+
end
|
17
|
+
|
18
|
+
it 'calls appropriate scope method' do
|
19
|
+
expect(@deers.count).to eq(1)
|
20
|
+
end
|
21
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'rspec'
|
2
|
+
require 'active_record'
|
3
|
+
require 'database_cleaner'
|
4
|
+
require 'factory_girl'
|
5
|
+
require 'factory_girl_sequences'
|
6
|
+
|
7
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
8
|
+
|
9
|
+
RSpec.configure do |config|
|
10
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
11
|
+
config.run_all_when_everything_filtered = true
|
12
|
+
config.filter_run :focus => true
|
13
|
+
config.order = 'random'
|
14
|
+
config.include FactoryGirl::Syntax::Methods
|
15
|
+
|
16
|
+
config.before(:suite) do
|
17
|
+
begin
|
18
|
+
FactoryGirl.lint
|
19
|
+
ensure
|
20
|
+
DatabaseCleaner.clean_with(:truncation)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
config.before(:each) { DatabaseCleaner.strategy = :transaction }
|
25
|
+
|
26
|
+
config.before(:each, :js => true) do
|
27
|
+
DatabaseCleaner.strategy = :truncation
|
28
|
+
end
|
29
|
+
|
30
|
+
config.before(:each) { DatabaseCleaner.start }
|
31
|
+
config.after(:each) { DatabaseCleaner.clean }
|
32
|
+
|
33
|
+
if ENV['RUN_SLOW_TESTS'] != "true"
|
34
|
+
config.filter_run_excluding :slow => true
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
ActiveRecord::Base.establish_connection adapter: 'sqlite3', database: 'memory'
|
39
|
+
|
40
|
+
support_dir_pattern = File.join(File.dirname(__FILE__),
|
41
|
+
'nifty_scope', 'support', '**', '*.rb'
|
42
|
+
)
|
43
|
+
|
44
|
+
Dir[File.expand_path(support_dir_pattern)].each { |f| load f }
|
45
|
+
|
46
|
+
require 'nifty_scope'
|
metadata
ADDED
@@ -0,0 +1,188 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: nifty_scope
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Vasiliy Yorkin, Alexandr Shuhin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-03-17 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - '>='
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 3.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - '>='
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 3.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 3.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - '>='
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 3.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ~>
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.5'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - '>='
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - '>='
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - '>='
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: sqlite3
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - '>='
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - '>='
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: factory_girl
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - '>='
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - '>='
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: factory_girl_sequences
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - '>='
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: database_cleaner
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - '>='
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - '>='
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
description: ' Provides an easy way to map request parameters to scopes '
|
140
|
+
email:
|
141
|
+
- vasiliy.yorkin@gmail.com
|
142
|
+
executables: []
|
143
|
+
extensions: []
|
144
|
+
extra_rdoc_files: []
|
145
|
+
files:
|
146
|
+
- .gitignore
|
147
|
+
- Gemfile
|
148
|
+
- LICENSE
|
149
|
+
- README.md
|
150
|
+
- Rakefile
|
151
|
+
- lib/nifty_scope.rb
|
152
|
+
- lib/nifty_scope/version.rb
|
153
|
+
- nifty_scope.gemspec
|
154
|
+
- spec/nifty_scope/nifty_scope_spec.rb
|
155
|
+
- spec/nifty_scope/support/factories.rb
|
156
|
+
- spec/nifty_scope/support/models.rb
|
157
|
+
- spec/nifty_scope/support/schema.rb
|
158
|
+
- spec/spec_helper.rb
|
159
|
+
homepage: http://github.com/vyorkin/nifty_scope
|
160
|
+
licenses:
|
161
|
+
- MIT
|
162
|
+
metadata: {}
|
163
|
+
post_install_message:
|
164
|
+
rdoc_options: []
|
165
|
+
require_paths:
|
166
|
+
- lib
|
167
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
168
|
+
requirements:
|
169
|
+
- - '>='
|
170
|
+
- !ruby/object:Gem::Version
|
171
|
+
version: '0'
|
172
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
173
|
+
requirements:
|
174
|
+
- - '>='
|
175
|
+
- !ruby/object:Gem::Version
|
176
|
+
version: '0'
|
177
|
+
requirements: []
|
178
|
+
rubyforge_project:
|
179
|
+
rubygems_version: 2.2.1
|
180
|
+
signing_key:
|
181
|
+
specification_version: 4
|
182
|
+
summary: Provides an easy way to map request parameters to scopes
|
183
|
+
test_files:
|
184
|
+
- spec/nifty_scope/nifty_scope_spec.rb
|
185
|
+
- spec/nifty_scope/support/factories.rb
|
186
|
+
- spec/nifty_scope/support/models.rb
|
187
|
+
- spec/nifty_scope/support/schema.rb
|
188
|
+
- spec/spec_helper.rb
|