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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 23f650f06f254a6f1533c012d13cf77dc0e13ea4
4
- data.tar.gz: eef214200f43d53ebb467e07dc75dc78de239cec
3
+ metadata.gz: e74e06bdcf94fdf9c64373f47e7e477098ccbbb7
4
+ data.tar.gz: 953a9d7dbb2adda4570775a3d4cabd488b855ba7
5
5
  SHA512:
6
- metadata.gz: 4482b1af30b6d0261082a18ade4d87064f9fea350a59fc3846aabe912538df4daca388e5cf46c426e8df46923a9c5a937c4fd43135189299193c67311e36d62d
7
- data.tar.gz: 98f9ca89898cb88cac89e7d418778d055ff3bb1c97d42d0ee95b3b9776fcd667325421cfc0a2e820a785a965b45e974fd4220967db0520f63adfb02eaf8d73a8
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.scoped).with(params[:filters])
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 default
30
- relation.paginate(:page => params[:page])
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(scope_datatypes, params)
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
- # private
37
+ private
38
38
  # TODO : for now we're assuming scope_datatypes is a Hash, K ?
39
- def map_scope_datatypes(scope_datatypes, params)
39
+ def map_scope_datatypes( params, scope_datatypes )
40
40
  scope_hash = {}
41
41
 
42
- scope_datatypes.each do |scope, datatype|
43
- scope_hash[scope] = convert_type( params[scope], datatype )
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
- else
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
 
@@ -1,3 +1,3 @@
1
1
  module Siphon
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
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
- # def apply(args)
13
- # Base.new(args)
14
- # end
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
@@ -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(:published)).to eq(siphon)
27
+ expect(siphon.has_scopes({published: nil})).to eq(siphon)
28
28
  end
29
29
 
30
- it "registers scope_datatypes" do
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 "`sends` one params keys to an ActiveRelation" do
39
+ it "triggers a scope with string arg (default)" do
40
40
  relation = double
41
- expect(relation).to receive(:name).with("jack").and_return(relation)
42
- siphon = Siphon::Base.new(relation).has_scopes(name:nil)
41
+ siphon = Siphon::Base.new(relation).has_scopes(name: nil)
43
42
 
44
- expect(siphon.filter(name: "jack")).to eq relation
43
+ expect(relation).to receive(:name).with("jack")
44
+
45
+ siphon.filter(name: "jack")
45
46
  end
46
47
 
47
- it "`sends` one params keys to an ActiveRelation" do
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(siphon.filter(age_gt: "18")).to eq relation
52
+ expect(relation).to receive(:age_gt).with(18)
53
+
54
+ siphon.filter(age_gt: "18")
53
55
  end
54
56
 
55
- it "`sends` one params keys to an ActiveRelation" do
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(siphon.filter(admin: "false")).to eq relation
61
+ expect(relation).to receive(:admin).with(false)
62
+
63
+ siphon.filter(admin: "false")
61
64
  end
62
65
 
63
- it "`sends` multiple keys to an ActiveRelation" do
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
- expect(siphon.filter(age_gt: "18", admin: "false")).to eq relation
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 "map datatype date"
91
+ pending "IGNORE & DELETE if not working"
102
92
  siphon = Siphon::Base.new("")
103
- siphon.map_scope_datatypes({age_gt: :integer}, {age_gt: "18"}).should == {age_gt: 18}
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
@@ -5,7 +5,7 @@ describe Siphon do
5
5
  describe "#new" do
6
6
 
7
7
  it "shoot a Siphon::Base instance" do
8
- Siphon.apply.should be_an_instance_of(Siphon::Base)
8
+ expect(Siphon.apply_scope).to be_an_instance_of(Siphon::Base)
9
9
  end
10
10
  end
11
11
  end
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.1
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-05 00:00:00.000000000 Z
11
+ date: 2013-08-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport