siphon 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 +4 -4
- data/README.md +11 -4
- data/lib/siphon/base.rb +12 -7
- data/lib/siphon/version.rb +1 -1
- data/lib/siphon.rb +12 -4
- data/spec/siphon/base_spec.rb +29 -39
- data/spec/siphon_spec.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e74e06bdcf94fdf9c64373f47e7e477098ccbbb7
|
4
|
+
data.tar.gz: 953a9d7dbb2adda4570775a3d4cabd488b855ba7
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 23ec3db1cc57a2387396f75933ef705157c3fc48355da20306e8e59f033b45dad2b7566ef6a77149154ad3f0861102716a631da336493c7391ef5577d7aacb2a
|
7
|
+
data.tar.gz: e02a1597243a1a7647e2fde9c2465eece79d789c656e9177ca1565facea45d3f1682ca73cb92d986371663f09deb3f529b68e316729ec2aa6cf2b262f12f6fa8
|
data/README.md
CHANGED
@@ -18,17 +18,24 @@ Siphon is super simple and can be easily overidden. However for basic usage it h
|
|
18
18
|
#books_controller.rb
|
19
19
|
|
20
20
|
def collection
|
21
|
-
@books ||= Siphon.new(Book.
|
21
|
+
@books ||= Siphon.new(Book.includes(:auhtor)).
|
22
|
+
has_scopes({year: integer, author_name: :string}).filter(params)
|
22
23
|
end
|
23
24
|
|
24
25
|
### Advanced Usage
|
26
|
+
TODO: More examples
|
25
27
|
|
26
28
|
# books_siphon.rb
|
27
29
|
BookSiphon < Siphon
|
28
|
-
|
29
|
-
def
|
30
|
-
|
30
|
+
|
31
|
+
def initialize
|
32
|
+
super
|
33
|
+
has_scopes({some: :integer, default: :boolean, scopes: nil})
|
31
34
|
end
|
35
|
+
|
36
|
+
# def default
|
37
|
+
# relation.paginate(:page => params[:page])
|
38
|
+
# end
|
32
39
|
end
|
33
40
|
|
34
41
|
## Why Siphon ?
|
data/lib/siphon/base.rb
CHANGED
@@ -12,7 +12,7 @@ module Siphon
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def filter(params)
|
15
|
-
@scopes = map_scope_datatypes(
|
15
|
+
@scopes = map_scope_datatypes( params, scope_datatypes )
|
16
16
|
|
17
17
|
scopes.each do |key, value|
|
18
18
|
self.relation= relation.send(key, *value)
|
@@ -30,17 +30,18 @@ module Siphon
|
|
30
30
|
# before: Date
|
31
31
|
#
|
32
32
|
def has_scopes(scope_datatypes = {})
|
33
|
-
@scope_datatypes = scope_datatypes
|
33
|
+
@scope_datatypes = scope_datatypes.symbolize_keys
|
34
34
|
self
|
35
35
|
end
|
36
36
|
|
37
|
-
|
37
|
+
private
|
38
38
|
# TODO : for now we're assuming scope_datatypes is a Hash, K ?
|
39
|
-
def map_scope_datatypes(
|
39
|
+
def map_scope_datatypes( params, scope_datatypes )
|
40
40
|
scope_hash = {}
|
41
41
|
|
42
|
-
|
43
|
-
|
42
|
+
params.symbolize_keys.each do |scope, value|
|
43
|
+
next unless scope_datatypes.has_key?(scope)
|
44
|
+
scope_hash[scope] = convert_type( value, scope_datatypes[scope] )
|
44
45
|
end
|
45
46
|
|
46
47
|
return scope_hash
|
@@ -52,8 +53,12 @@ module Siphon
|
|
52
53
|
Integer(value)
|
53
54
|
when :boolean
|
54
55
|
value != "false"
|
55
|
-
|
56
|
+
when :string
|
56
57
|
value
|
58
|
+
when :none
|
59
|
+
nil
|
60
|
+
else
|
61
|
+
value == "nil" ? nil : value
|
57
62
|
end
|
58
63
|
end
|
59
64
|
|
data/lib/siphon/version.rb
CHANGED
data/lib/siphon.rb
CHANGED
@@ -9,10 +9,18 @@ require "siphon/base"
|
|
9
9
|
module Siphon
|
10
10
|
# Your code goes here...
|
11
11
|
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
def siphon(args)
|
13
|
+
@siphon = Siphon::Base.new(args)
|
14
|
+
end
|
15
|
+
|
16
|
+
def siphon_scopes
|
17
|
+
@siphon and Hash[ @siphon.scopes.map {|k, v| v||= "nil"; [k, v]} ]
|
18
|
+
end
|
15
19
|
|
16
|
-
# module_function :apply
|
17
20
|
|
18
21
|
end
|
22
|
+
|
23
|
+
ActiveSupport.on_load :action_controller do
|
24
|
+
include Siphon
|
25
|
+
helper_method :siphon_scopes
|
26
|
+
end
|
data/spec/siphon/base_spec.rb
CHANGED
@@ -24,10 +24,10 @@ describe Siphon::Base do
|
|
24
24
|
it "returns self" do
|
25
25
|
siphon = Siphon::Base.new("")
|
26
26
|
|
27
|
-
expect(siphon.has_scopes(:
|
27
|
+
expect(siphon.has_scopes({published: nil})).to eq(siphon)
|
28
28
|
end
|
29
29
|
|
30
|
-
it "
|
30
|
+
it "sets scope_datatypes" do
|
31
31
|
siphon = Siphon::Base.new("").has_scopes(published: :integer)
|
32
32
|
|
33
33
|
expect(siphon.scope_datatypes).to eq(published: :integer)
|
@@ -36,71 +36,61 @@ describe Siphon::Base do
|
|
36
36
|
|
37
37
|
describe "#filter" do
|
38
38
|
|
39
|
-
it "
|
39
|
+
it "triggers a scope with string arg (default)" do
|
40
40
|
relation = double
|
41
|
-
|
42
|
-
siphon = Siphon::Base.new(relation).has_scopes(name:nil)
|
41
|
+
siphon = Siphon::Base.new(relation).has_scopes(name: nil)
|
43
42
|
|
44
|
-
expect(
|
43
|
+
expect(relation).to receive(:name).with("jack")
|
44
|
+
|
45
|
+
siphon.filter(name: "jack")
|
45
46
|
end
|
46
47
|
|
47
|
-
it "
|
48
|
+
it "triggers a scope with an integer arg" do
|
48
49
|
relation = double
|
49
|
-
expect(relation).to receive(:age_gt).with(18).and_return(relation)
|
50
50
|
siphon = Siphon::Base.new(relation).has_scopes(age_gt: :integer)
|
51
51
|
|
52
|
-
expect(
|
52
|
+
expect(relation).to receive(:age_gt).with(18)
|
53
|
+
|
54
|
+
siphon.filter(age_gt: "18")
|
53
55
|
end
|
54
56
|
|
55
|
-
it "
|
57
|
+
it "triggers a scope with a boolean arg" do
|
56
58
|
relation = double
|
57
|
-
expect(relation).to receive(:admin).with(false).and_return(relation)
|
58
59
|
siphon = Siphon::Base.new(relation).has_scopes(admin: :boolean)
|
59
60
|
|
60
|
-
expect(
|
61
|
+
expect(relation).to receive(:admin).with(false)
|
62
|
+
|
63
|
+
siphon.filter(admin: "false")
|
61
64
|
end
|
62
65
|
|
63
|
-
it "
|
66
|
+
it "chains multiple scopes" do
|
64
67
|
relation = double
|
68
|
+
siphon = Siphon::Base.new(relation).has_scopes(age_gt: :integer, admin: :boolean)
|
69
|
+
|
65
70
|
expect(relation).to receive(:admin).with(false).and_return(relation)
|
66
71
|
expect(relation).to receive(:age_gt).with(18).and_return(relation)
|
67
|
-
siphon = Siphon::Base.new(relation).has_scopes(age_gt: :integer, admin: :boolean)
|
68
72
|
|
69
|
-
|
73
|
+
siphon.filter(age_gt: "18", admin: "false")
|
70
74
|
end
|
71
75
|
|
76
|
+
it "chains **only** scopes defined by has_scopes" do
|
77
|
+
relation = double
|
78
|
+
siphon = Siphon::Base.new(relation).has_scopes(age_gt: :integer)
|
72
79
|
|
80
|
+
expect(relation).to receive(:age_gt)
|
81
|
+
expect(relation).to_not receive(:controller)
|
73
82
|
|
83
|
+
siphon.filter(age_gt: "18", controller: "books_controller")
|
84
|
+
end
|
74
85
|
end
|
75
86
|
|
76
|
-
|
77
87
|
# TESTING PRIVATE METHOD !
|
78
|
-
describe "#map_scope_datatypes" do
|
79
|
-
|
80
|
-
it "maps no scope value to nil" do
|
81
|
-
siphon = Siphon::Base.new("")
|
82
|
-
siphon.map_scope_datatypes({sorted: nil}, {sorted: nil}) == {sorted: nil}
|
83
|
-
end
|
84
|
-
|
85
|
-
it "maps default datatype string" do
|
86
|
-
siphon = Siphon::Base.new("")
|
87
|
-
siphon.map_scope_datatypes({sorted: nil}, {sorted: "name"}) == {sorted: "name"}
|
88
|
-
end
|
89
|
-
|
90
|
-
it "maps datatype boolean" do
|
91
|
-
siphon = Siphon::Base.new("")
|
92
|
-
siphon.map_scope_datatypes({published: :boolean}, {published: "false"}).should == {published: false}
|
93
|
-
end
|
94
|
-
|
95
|
-
it "maps datatype integer" do
|
96
|
-
siphon = Siphon::Base.new("")
|
97
|
-
siphon.map_scope_datatypes({age_gt: :integer}, {age_gt: "18"}).should == {age_gt: 18}
|
98
|
-
end
|
88
|
+
describe "PRIVATE !! : #map_scope_datatypes" do
|
99
89
|
|
100
90
|
it "maps datatype date" do
|
101
|
-
pending "
|
91
|
+
pending "IGNORE & DELETE if not working"
|
102
92
|
siphon = Siphon::Base.new("")
|
103
|
-
siphon.map_scope_datatypes({age_gt:
|
93
|
+
siphon.map_scope_datatypes({age_gt: "18"}, {age_gt: :integer}).should == {age_gt: 18}
|
104
94
|
end
|
105
95
|
end
|
106
96
|
|
data/spec/siphon_spec.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: siphon
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Charles Sistovaris
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2013-08-
|
11
|
+
date: 2013-08-06 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|