paraphrase 0.9.0 → 0.10.0
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/.travis.yml +4 -1
- data/Appraisals +12 -18
- data/CHANGELOG.md +13 -1
- data/README.md +53 -11
- data/gemfiles/3.1.gemfile +3 -3
- data/gemfiles/3.1.gemfile.lock +47 -42
- data/gemfiles/3.2.gemfile +3 -3
- data/gemfiles/3.2.gemfile.lock +36 -32
- data/gemfiles/4.0.gemfile +3 -3
- data/gemfiles/4.0.gemfile.lock +38 -36
- data/gemfiles/4.1.gemfile +3 -3
- data/gemfiles/4.1.gemfile.lock +35 -31
- data/lib/paraphrase.rb +14 -3
- data/lib/paraphrase/active_model.rb +1 -0
- data/lib/paraphrase/mapping.rb +28 -0
- data/lib/paraphrase/{params.rb → params_filter.rb} +10 -4
- data/lib/paraphrase/query.rb +64 -41
- data/lib/paraphrase/rails.rb +2 -0
- data/lib/paraphrase/repository.rb +36 -0
- data/lib/paraphrase/syntax.rb +12 -2
- data/lib/paraphrase/version.rb +1 -1
- data/paraphrase.gemspec +5 -5
- data/spec/paraphrase/query_spec.rb +39 -10
- data/spec/paraphrase/syntax_spec.rb +1 -1
- data/spec/spec_helper.rb +7 -8
- metadata +15 -19
- data/gemfiles/3.0.gemfile +0 -9
- data/gemfiles/3.0.gemfile.lock +0 -100
- data/lib/paraphrase/errors.rb +0 -3
- data/lib/paraphrase/scope.rb +0 -56
- data/spec/paraphrase/scope_spec.rb +0 -51
data/lib/paraphrase/rails.rb
CHANGED
@@ -0,0 +1,36 @@
|
|
1
|
+
module Paraphrase
|
2
|
+
class Repository
|
3
|
+
attr_reader :relation, :mapping, :params
|
4
|
+
|
5
|
+
def self.chain(relation, mapping, params)
|
6
|
+
new(relation, mapping, params).chain
|
7
|
+
end
|
8
|
+
|
9
|
+
def initialize(relation, mapping, params)
|
10
|
+
@relation, @mapping, @params = relation, mapping, params
|
11
|
+
end
|
12
|
+
|
13
|
+
def chain
|
14
|
+
if mapping.satisfied?(params)
|
15
|
+
|
16
|
+
if scope.arity.zero?
|
17
|
+
relation.scoping { scope.call }
|
18
|
+
else
|
19
|
+
values = mapping.values(params)
|
20
|
+
relation.scoping { scope.call(*values) }
|
21
|
+
end
|
22
|
+
else
|
23
|
+
relation
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def scope
|
28
|
+
@scope ||=
|
29
|
+
if respond_to?(mapping.name)
|
30
|
+
method(mapping.name)
|
31
|
+
else
|
32
|
+
relation.klass.method(mapping.name)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/paraphrase/syntax.rb
CHANGED
@@ -7,7 +7,7 @@ module Paraphrase
|
|
7
7
|
name.constantize
|
8
8
|
rescue NameError => e
|
9
9
|
if e.message =~ /uninitialized constant/
|
10
|
-
raise Paraphrase::NoQueryDefined.new(
|
10
|
+
raise Paraphrase::NoQueryDefined.new(name)
|
11
11
|
end
|
12
12
|
end
|
13
13
|
|
@@ -15,7 +15,17 @@ module Paraphrase
|
|
15
15
|
#
|
16
16
|
# @param [Hash] params query parameters
|
17
17
|
def paraphrase(params = {})
|
18
|
-
paraphraser.new(params,
|
18
|
+
paraphraser.new(params, default_paraphrase_relation).result
|
19
|
+
end
|
20
|
+
|
21
|
+
def default_paraphrase_relation
|
22
|
+
if is_a? ActiveRecord::Relation
|
23
|
+
self
|
24
|
+
elsif ActiveRecord::VERSION::MAJOR > 3
|
25
|
+
all
|
26
|
+
else
|
27
|
+
scoped
|
28
|
+
end
|
19
29
|
end
|
20
30
|
end
|
21
31
|
|
data/lib/paraphrase/version.rb
CHANGED
data/paraphrase.gemspec
CHANGED
@@ -24,14 +24,14 @@ Gem::Specification.new do |gem|
|
|
24
24
|
|
25
25
|
gem.required_ruby_version = '>= 1.9.3'
|
26
26
|
|
27
|
-
gem.add_dependency 'activerecord', '>= 3.
|
28
|
-
gem.add_dependency 'activesupport', '>= 3.
|
29
|
-
gem.add_dependency 'activemodel', '>= 3.
|
27
|
+
gem.add_dependency 'activerecord', '>= 3.1', '< 4.2'
|
28
|
+
gem.add_dependency 'activesupport', '>= 3.1', '< 4.2'
|
29
|
+
gem.add_dependency 'activemodel', '>= 3.1', '< 4.2'
|
30
30
|
|
31
|
-
gem.add_development_dependency 'actionpack', '>= 3.
|
31
|
+
gem.add_development_dependency 'actionpack', '>= 3.1', '< 4.2'
|
32
32
|
gem.add_development_dependency 'bundler', '~> 1.0'
|
33
33
|
gem.add_development_dependency 'yard', '~> 0.7'
|
34
|
-
gem.add_development_dependency 'rspec', '~>
|
34
|
+
gem.add_development_dependency 'rspec', '~> 3.0'
|
35
35
|
gem.add_development_dependency 'rake', '~> 0.9.2'
|
36
36
|
gem.add_development_dependency 'appraisal', '~> 0.4'
|
37
37
|
gem.add_development_dependency 'pry', '~> 0.9'
|
@@ -9,28 +9,38 @@ module Paraphrase
|
|
9
9
|
map :authors, to: :by_users
|
10
10
|
map :start_date, :end_date, to: :published_between
|
11
11
|
|
12
|
-
class
|
12
|
+
class ParamsFilter
|
13
13
|
def start_date
|
14
|
-
|
14
|
+
Time.parse(params[:start_date]) rescue nil
|
15
15
|
end
|
16
|
+
end
|
16
17
|
|
17
|
-
|
18
|
-
|
18
|
+
class Repository
|
19
|
+
def published_between(start_date, end_date)
|
20
|
+
where(published_at: start_date..end_date)
|
19
21
|
end
|
20
22
|
end
|
23
|
+
|
24
|
+
param :end_date do
|
25
|
+
Time.parse(params[:end_date]) rescue nil
|
26
|
+
end
|
27
|
+
|
28
|
+
scope :by_users do |authors|
|
29
|
+
relation.joins(:user).where(users: { name: authors })
|
30
|
+
end
|
21
31
|
end
|
22
32
|
|
23
33
|
describe ".map" do
|
24
34
|
it "adds information to Query.scopes" do
|
25
|
-
expect(PostQuery.
|
35
|
+
expect(PostQuery.mappings).not_to be_empty
|
26
36
|
end
|
27
37
|
|
28
38
|
it "raises an error if a scope is added twice" do
|
29
|
-
expect { PostQuery.map :name, to: :titled }.to raise_error Paraphrase::
|
39
|
+
expect { PostQuery.map :name, to: :titled }.to raise_error Paraphrase::DuplicateMappingError
|
30
40
|
end
|
31
41
|
|
32
42
|
it 'defines readers for each key' do
|
33
|
-
query = PostQuery.new
|
43
|
+
query = PostQuery.new(Hash.new)
|
34
44
|
|
35
45
|
expect(query).to respond_to :title
|
36
46
|
expect(query).to respond_to :is_published
|
@@ -45,15 +55,15 @@ module Paraphrase
|
|
45
55
|
|
46
56
|
describe '#source' do
|
47
57
|
it 'is determined via query class name' do
|
48
|
-
expect(PostQuery.
|
58
|
+
expect(PostQuery.source).to eq 'Post'
|
49
59
|
end
|
50
60
|
|
51
61
|
it 'can be manually specified in the class' do
|
52
62
|
klass = Class.new(Query) do
|
53
|
-
source :User
|
63
|
+
self.source = :User
|
54
64
|
end
|
55
65
|
|
56
|
-
expect(klass.
|
66
|
+
expect(klass.source).to eq :User
|
57
67
|
end
|
58
68
|
end
|
59
69
|
|
@@ -93,6 +103,25 @@ module Paraphrase
|
|
93
103
|
)
|
94
104
|
end
|
95
105
|
|
106
|
+
it 'supports defining scopes in the query class' do
|
107
|
+
robert = User.create!(name: 'Robert')
|
108
|
+
frank = User.create!(name: 'Frank')
|
109
|
+
susie = User.create!(name: 'Susie')
|
110
|
+
|
111
|
+
# Combination of all three attributes ensures that the scope is preserved
|
112
|
+
# before and after the call to the method on the query class
|
113
|
+
Post.create!(title: 'Summer', published: false, user: robert)
|
114
|
+
Post.create!(title: 'Summer', published: true, user: frank)
|
115
|
+
susie_post = Post.create!(title: 'Summer', published: true, user: susie)
|
116
|
+
|
117
|
+
params = { authors: ['Robert', 'Susie'], title: 'Summer', is_published: '1' }
|
118
|
+
query = PostQuery.new(params)
|
119
|
+
result = Post.paraphrase(params)
|
120
|
+
|
121
|
+
expect(query.result).to eq [susie_post]
|
122
|
+
expect(result).to eq [susie_post]
|
123
|
+
end
|
124
|
+
|
96
125
|
it 'preserves the original scope used to initialize the query' do
|
97
126
|
user = User.create!
|
98
127
|
blue_post = Post.create!(user: user, title: 'Blue', published: false)
|
data/spec/spec_helper.rb
CHANGED
@@ -1,5 +1,7 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
if ENV.key?('CODECLIMATE_REPO_TOKEN')
|
2
|
+
require 'codeclimate-test-reporter'
|
3
|
+
CodeClimate::TestReporter.start
|
4
|
+
end
|
3
5
|
|
4
6
|
require 'rspec'
|
5
7
|
require 'pry'
|
@@ -19,7 +21,8 @@ ActiveRecord::Base.establish_connection(
|
|
19
21
|
|
20
22
|
ActiveRecord::Migration.verbose = false
|
21
23
|
ActiveRecord::Schema.define do
|
22
|
-
create_table :users, force: true do
|
24
|
+
create_table :users, force: true do |t|
|
25
|
+
t.string :name
|
23
26
|
end
|
24
27
|
|
25
28
|
create_table :posts, force: true do |t|
|
@@ -35,8 +38,8 @@ ActiveRecord::Schema.define do
|
|
35
38
|
end
|
36
39
|
end
|
37
40
|
|
41
|
+
require 'paraphrase/syntax'
|
38
42
|
ActiveRecord::Base.extend Paraphrase::Syntax
|
39
|
-
ActiveRecord::Relation.send(:include, Paraphrase::Syntax)
|
40
43
|
|
41
44
|
class User < ActiveRecord::Base
|
42
45
|
has_many :accounts
|
@@ -55,10 +58,6 @@ class Post < ActiveRecord::Base
|
|
55
58
|
def self.by_users(names)
|
56
59
|
joins(:user).where(users: { name: names })
|
57
60
|
end
|
58
|
-
|
59
|
-
def self.published_between(start_date, end_date)
|
60
|
-
where(published_at: start_date..end_date)
|
61
|
-
end
|
62
61
|
end
|
63
62
|
|
64
63
|
class Account < ActiveRecord::Base
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: paraphrase
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Eduardo Gutierrez
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-
|
11
|
+
date: 2014-07-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -16,7 +16,7 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: '3.
|
19
|
+
version: '3.1'
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
22
|
version: '4.2'
|
@@ -26,7 +26,7 @@ dependencies:
|
|
26
26
|
requirements:
|
27
27
|
- - ">="
|
28
28
|
- !ruby/object:Gem::Version
|
29
|
-
version: '3.
|
29
|
+
version: '3.1'
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '4.2'
|
@@ -36,7 +36,7 @@ dependencies:
|
|
36
36
|
requirements:
|
37
37
|
- - ">="
|
38
38
|
- !ruby/object:Gem::Version
|
39
|
-
version: '3.
|
39
|
+
version: '3.1'
|
40
40
|
- - "<"
|
41
41
|
- !ruby/object:Gem::Version
|
42
42
|
version: '4.2'
|
@@ -46,7 +46,7 @@ dependencies:
|
|
46
46
|
requirements:
|
47
47
|
- - ">="
|
48
48
|
- !ruby/object:Gem::Version
|
49
|
-
version: '3.
|
49
|
+
version: '3.1'
|
50
50
|
- - "<"
|
51
51
|
- !ruby/object:Gem::Version
|
52
52
|
version: '4.2'
|
@@ -56,7 +56,7 @@ dependencies:
|
|
56
56
|
requirements:
|
57
57
|
- - ">="
|
58
58
|
- !ruby/object:Gem::Version
|
59
|
-
version: '3.
|
59
|
+
version: '3.1'
|
60
60
|
- - "<"
|
61
61
|
- !ruby/object:Gem::Version
|
62
62
|
version: '4.2'
|
@@ -66,7 +66,7 @@ dependencies:
|
|
66
66
|
requirements:
|
67
67
|
- - ">="
|
68
68
|
- !ruby/object:Gem::Version
|
69
|
-
version: '3.
|
69
|
+
version: '3.1'
|
70
70
|
- - "<"
|
71
71
|
- !ruby/object:Gem::Version
|
72
72
|
version: '4.2'
|
@@ -76,7 +76,7 @@ dependencies:
|
|
76
76
|
requirements:
|
77
77
|
- - ">="
|
78
78
|
- !ruby/object:Gem::Version
|
79
|
-
version: '3.
|
79
|
+
version: '3.1'
|
80
80
|
- - "<"
|
81
81
|
- !ruby/object:Gem::Version
|
82
82
|
version: '4.2'
|
@@ -86,7 +86,7 @@ dependencies:
|
|
86
86
|
requirements:
|
87
87
|
- - ">="
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '3.
|
89
|
+
version: '3.1'
|
90
90
|
- - "<"
|
91
91
|
- !ruby/object:Gem::Version
|
92
92
|
version: '4.2'
|
@@ -124,14 +124,14 @@ dependencies:
|
|
124
124
|
requirements:
|
125
125
|
- - "~>"
|
126
126
|
- !ruby/object:Gem::Version
|
127
|
-
version: '
|
127
|
+
version: '3.0'
|
128
128
|
type: :development
|
129
129
|
prerelease: false
|
130
130
|
version_requirements: !ruby/object:Gem::Requirement
|
131
131
|
requirements:
|
132
132
|
- - "~>"
|
133
133
|
- !ruby/object:Gem::Version
|
134
|
-
version: '
|
134
|
+
version: '3.0'
|
135
135
|
- !ruby/object:Gem::Dependency
|
136
136
|
name: rake
|
137
137
|
requirement: !ruby/object:Gem::Requirement
|
@@ -235,8 +235,6 @@ files:
|
|
235
235
|
- LICENSE.txt
|
236
236
|
- README.md
|
237
237
|
- Rakefile
|
238
|
-
- gemfiles/3.0.gemfile
|
239
|
-
- gemfiles/3.0.gemfile.lock
|
240
238
|
- gemfiles/3.1.gemfile
|
241
239
|
- gemfiles/3.1.gemfile.lock
|
242
240
|
- gemfiles/3.2.gemfile
|
@@ -247,16 +245,15 @@ files:
|
|
247
245
|
- gemfiles/4.1.gemfile.lock
|
248
246
|
- lib/paraphrase.rb
|
249
247
|
- lib/paraphrase/active_model.rb
|
250
|
-
- lib/paraphrase/
|
251
|
-
- lib/paraphrase/
|
248
|
+
- lib/paraphrase/mapping.rb
|
249
|
+
- lib/paraphrase/params_filter.rb
|
252
250
|
- lib/paraphrase/query.rb
|
253
251
|
- lib/paraphrase/rails.rb
|
254
|
-
- lib/paraphrase/
|
252
|
+
- lib/paraphrase/repository.rb
|
255
253
|
- lib/paraphrase/syntax.rb
|
256
254
|
- lib/paraphrase/version.rb
|
257
255
|
- paraphrase.gemspec
|
258
256
|
- spec/paraphrase/query_spec.rb
|
259
|
-
- spec/paraphrase/scope_spec.rb
|
260
257
|
- spec/paraphrase/syntax_spec.rb
|
261
258
|
- spec/spec_helper.rb
|
262
259
|
homepage: https://github.com/ecbypi/paraphrase
|
@@ -285,7 +282,6 @@ specification_version: 4
|
|
285
282
|
summary: Map query params to model scopes
|
286
283
|
test_files:
|
287
284
|
- spec/paraphrase/query_spec.rb
|
288
|
-
- spec/paraphrase/scope_spec.rb
|
289
285
|
- spec/paraphrase/syntax_spec.rb
|
290
286
|
- spec/spec_helper.rb
|
291
287
|
has_rdoc:
|
data/gemfiles/3.0.gemfile
DELETED
data/gemfiles/3.0.gemfile.lock
DELETED
@@ -1,100 +0,0 @@
|
|
1
|
-
PATH
|
2
|
-
remote: /Users/edd_d/Work/paraphrase
|
3
|
-
specs:
|
4
|
-
paraphrase (0.9.0)
|
5
|
-
activemodel (>= 3.0, < 4.2)
|
6
|
-
activerecord (>= 3.0, < 4.2)
|
7
|
-
activesupport (>= 3.0, < 4.2)
|
8
|
-
|
9
|
-
GEM
|
10
|
-
remote: https://rubygems.org/
|
11
|
-
specs:
|
12
|
-
actionpack (3.2.16)
|
13
|
-
activemodel (= 3.2.16)
|
14
|
-
activesupport (= 3.2.16)
|
15
|
-
builder (~> 3.0.0)
|
16
|
-
erubis (~> 2.7.0)
|
17
|
-
journey (~> 1.0.4)
|
18
|
-
rack (~> 1.4.5)
|
19
|
-
rack-cache (~> 1.2)
|
20
|
-
rack-test (~> 0.6.1)
|
21
|
-
sprockets (~> 2.2.1)
|
22
|
-
activemodel (3.2.16)
|
23
|
-
activesupport (= 3.2.16)
|
24
|
-
builder (~> 3.0.0)
|
25
|
-
activerecord (3.2.16)
|
26
|
-
activemodel (= 3.2.16)
|
27
|
-
activesupport (= 3.2.16)
|
28
|
-
arel (~> 3.0.2)
|
29
|
-
tzinfo (~> 0.3.29)
|
30
|
-
activesupport (3.2.16)
|
31
|
-
i18n (~> 0.6, >= 0.6.4)
|
32
|
-
multi_json (~> 1.0)
|
33
|
-
appraisal (0.5.2)
|
34
|
-
bundler
|
35
|
-
rake
|
36
|
-
arel (3.0.3)
|
37
|
-
builder (3.0.4)
|
38
|
-
codeclimate-test-reporter (0.3.0)
|
39
|
-
simplecov (>= 0.7.1, < 1.0.0)
|
40
|
-
coderay (1.1.0)
|
41
|
-
diff-lcs (1.2.5)
|
42
|
-
docile (1.1.3)
|
43
|
-
erubis (2.7.0)
|
44
|
-
hike (1.2.3)
|
45
|
-
i18n (0.6.9)
|
46
|
-
journey (1.0.4)
|
47
|
-
method_source (0.8.2)
|
48
|
-
multi_json (1.8.4)
|
49
|
-
pry (0.9.12.4)
|
50
|
-
coderay (~> 1.0)
|
51
|
-
method_source (~> 0.8)
|
52
|
-
slop (~> 3.4)
|
53
|
-
rack (1.4.5)
|
54
|
-
rack-cache (1.2)
|
55
|
-
rack (>= 0.4)
|
56
|
-
rack-test (0.6.2)
|
57
|
-
rack (>= 1.0)
|
58
|
-
rake (0.9.6)
|
59
|
-
redcarpet (2.1.1)
|
60
|
-
rspec (2.14.1)
|
61
|
-
rspec-core (~> 2.14.0)
|
62
|
-
rspec-expectations (~> 2.14.0)
|
63
|
-
rspec-mocks (~> 2.14.0)
|
64
|
-
rspec-core (2.14.7)
|
65
|
-
rspec-expectations (2.14.4)
|
66
|
-
diff-lcs (>= 1.1.3, < 2.0)
|
67
|
-
rspec-mocks (2.14.4)
|
68
|
-
simplecov (0.8.2)
|
69
|
-
docile (~> 1.1.0)
|
70
|
-
multi_json
|
71
|
-
simplecov-html (~> 0.8.0)
|
72
|
-
simplecov-html (0.8.0)
|
73
|
-
slop (3.4.7)
|
74
|
-
sprockets (2.2.2)
|
75
|
-
hike (~> 1.2)
|
76
|
-
multi_json (~> 1.0)
|
77
|
-
rack (~> 1.0)
|
78
|
-
tilt (~> 1.1, != 1.3.0)
|
79
|
-
sqlite3 (1.3.8)
|
80
|
-
tilt (1.4.1)
|
81
|
-
tzinfo (0.3.38)
|
82
|
-
yard (0.8.7.3)
|
83
|
-
|
84
|
-
PLATFORMS
|
85
|
-
ruby
|
86
|
-
|
87
|
-
DEPENDENCIES
|
88
|
-
actionpack (~> 3.0)
|
89
|
-
activerecord (~> 3.0)
|
90
|
-
activesupport (~> 3.0)
|
91
|
-
appraisal (~> 0.4)
|
92
|
-
bundler (~> 1.0)
|
93
|
-
codeclimate-test-reporter (~> 0.3)
|
94
|
-
paraphrase!
|
95
|
-
pry (~> 0.9)
|
96
|
-
rake (~> 0.9.2)
|
97
|
-
redcarpet (~> 2.1.1)
|
98
|
-
rspec (~> 2.14)
|
99
|
-
sqlite3 (~> 1.3.6)
|
100
|
-
yard (~> 0.7)
|