siphon 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 23f650f06f254a6f1533c012d13cf77dc0e13ea4
4
+ data.tar.gz: eef214200f43d53ebb467e07dc75dc78de239cec
5
+ SHA512:
6
+ metadata.gz: 4482b1af30b6d0261082a18ade4d87064f9fea350a59fc3846aabe912538df4daca388e5cf46c426e8df46923a9c5a937c4fd43135189299193c67311e36d62d
7
+ data.tar.gz: 98f9ca89898cb88cac89e7d418778d055ff3bb1c97d42d0ee95b3b9776fcd667325421cfc0a2e820a785a965b45e974fd4220967db0520f63adfb02eaf8d73a8
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .DS_Store
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.pryrc ADDED
@@ -0,0 +1 @@
1
+ require "siphon"
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format documentation
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in siphon.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Charles Sistovaris
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,86 @@
1
+ # Siphon
2
+
3
+ Siphon's a minimalistic gem which enables you to easily apply/combine/exclude your ActiveRecord scopes with params.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'siphon'
10
+
11
+ ## Usage
12
+
13
+ Siphon is super simple and can be easily overidden. However for basic usage it has a couple of helpers.
14
+
15
+ ### Basic Usage
16
+
17
+ ```ruby
18
+ #books_controller.rb
19
+
20
+ def collection
21
+ @books ||= Siphon.new(Book.scoped).with(params[:filters])
22
+ end
23
+
24
+ ### Advanced Usage
25
+
26
+ # books_siphon.rb
27
+ BookSiphon < Siphon
28
+
29
+ def default
30
+ relation.paginate(:page => params[:page])
31
+ end
32
+ end
33
+
34
+ ## Why Siphon ?
35
+
36
+ Thanks to Arel & ActiveRelation, Rails has an extraordinary, super powerful API to create scopes. You can chain them, merge them, pass them around, in what ever order you want, they'll just build the SQL you expect and only fire when necessary.
37
+
38
+ However once you're in the controller all this beauty is lost if you want to start applying scopes conditionally - e.g : depending on params in the url.
39
+
40
+ One way of doing it is like that :
41
+
42
+ def index
43
+ @results = Results.scoped
44
+ @results = @results.gender(params[:gender]) if params[:gender]
45
+ @results = @results.career(params[:career]) if params[:career]
46
+ end
47
+
48
+ It's ugly, it doesn't scale and each time I see an `if` I hear the squeeking sound of a tree falling in the rain forest to build another Mc Donalds.
49
+
50
+ ## Why not Has_Scope ?
51
+
52
+ So what about has_scope ?
53
+
54
+ # books_controller.rb
55
+ has_scope :gender
56
+ has_scope :career
57
+
58
+ def index
59
+ @results = apply_scopes(Results).all
60
+ end
61
+
62
+ and yee it was build by the awesome José Valim. But in my taste it suffers from the same problem Paperclip and all gem which rely on configuration with hashes have : Hashitis.
63
+
64
+ # tree_controller.rb
65
+ has_scope :color, :unless => :show_all_colors?
66
+ has_scope :only_tall, :type => :boolean, :only => :index, :if => :restrict_to_only_tall_trees?
67
+ has_scope :shadown_range, :default => 10, :except => [ :index, :show, :new ]
68
+ has_scope :root_type, :as => :root, :allow_blank => true
69
+ has_scope :calculate_height, :default => proc {|c| c.session[:height] || 20 }, :only => :new
70
+
71
+ As soon as you reach a certain level of complexity it looks as bad as if nested ifs were crawling all over your code. That's why most of us go for CarrierWave today : because well if you need behaviour why try to invent another DSL when you already have the best at hands : RUBY !
72
+
73
+ * You unclutter your controllers - which I like anorexic.
74
+ * You're not limited by the set of configuration the gem gives you.
75
+ * you can apply as much logic as you whish
76
+ * It's encapsultated, unit-testable & respects SRP
77
+
78
+ Actually Siphon shouln't even be a gem but just a set of guidelines. But hey it has a cool name...
79
+
80
+ ## Contributing
81
+
82
+ 1. Fork it
83
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
84
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
85
+ 4. Push to the branch (`git push origin my-new-feature`)
86
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,61 @@
1
+ #
2
+ #
3
+ #
4
+ module Siphon
5
+ class Base
6
+ attr_accessor :relation, :scopes, :scope_datatypes
7
+
8
+
9
+ # Siphon.new(Book.scoped)
10
+ def initialize(relation)
11
+ @relation = relation
12
+ end
13
+
14
+ def filter(params)
15
+ @scopes = map_scope_datatypes(scope_datatypes, params)
16
+
17
+ scopes.each do |key, value|
18
+ self.relation= relation.send(key, *value)
19
+ end
20
+
21
+ relation
22
+ end
23
+
24
+ # Possible Datatype
25
+ # -----------------
26
+ # admin: nil
27
+ # category_name: String
28
+ # published: Boolean,
29
+ # age_gt: Integer
30
+ # before: Date
31
+ #
32
+ def has_scopes(scope_datatypes = {})
33
+ @scope_datatypes = scope_datatypes
34
+ self
35
+ end
36
+
37
+ # private
38
+ # TODO : for now we're assuming scope_datatypes is a Hash, K ?
39
+ def map_scope_datatypes(scope_datatypes, params)
40
+ scope_hash = {}
41
+
42
+ scope_datatypes.each do |scope, datatype|
43
+ scope_hash[scope] = convert_type( params[scope], datatype )
44
+ end
45
+
46
+ return scope_hash
47
+ end
48
+
49
+ def convert_type(value, type)
50
+ v = case type
51
+ when :integer
52
+ Integer(value)
53
+ when :boolean
54
+ value != "false"
55
+ else
56
+ value
57
+ end
58
+ end
59
+
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module Siphon
2
+ VERSION = "0.0.1"
3
+ end
data/lib/siphon.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "bundler/setup"
2
+
3
+ require "active_support/core_ext"
4
+
5
+
6
+ require "siphon/version"
7
+ require "siphon/base"
8
+
9
+ module Siphon
10
+ # Your code goes here...
11
+
12
+ # def apply(args)
13
+ # Base.new(args)
14
+ # end
15
+
16
+ # module_function :apply
17
+
18
+ end
data/siphon.gemspec ADDED
@@ -0,0 +1,29 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'siphon/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "siphon"
8
+ spec.version = Siphon::VERSION
9
+ spec.authors = ["Charles Sistovaris"]
10
+ spec.email = ["charlysisto@gmail.com"]
11
+ spec.description = %q{Siphon enables you to easily apply/combine/exclude your ActiveRecord scopes with params}
12
+ spec.summary = %q{Siphon enables you to easily apply/combine/exclude your ActiveRecord scopes with params}
13
+ spec.homepage = "http://github.com/charly/siphon"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
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_dependency "activesupport"
22
+ # spec.add_dependency "activesupport"
23
+
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "pry"
29
+ end
@@ -0,0 +1,10 @@
1
+ #
2
+ class ActiveRelation
3
+
4
+
5
+
6
+ def self.title_with
7
+
8
+ end
9
+
10
+ end
@@ -0,0 +1,107 @@
1
+ require "spec_helper"
2
+
3
+ class Tree
4
+ end
5
+
6
+ describe Siphon::Base do
7
+
8
+ describe "#initialize" do
9
+
10
+ it "must initialize with an ActiveRelation" do
11
+ expect { Siphon::Base.new }.to raise_error(ArgumentError)
12
+ end
13
+
14
+ it "sets @relation" do
15
+ relation = double
16
+ siphon = Siphon::Base.new(relation)
17
+
18
+ expect(siphon.relation).to eq(relation)
19
+ end
20
+ end
21
+
22
+ describe "#has_scopes" do
23
+
24
+ it "returns self" do
25
+ siphon = Siphon::Base.new("")
26
+
27
+ expect(siphon.has_scopes(:published)).to eq(siphon)
28
+ end
29
+
30
+ it "registers scope_datatypes" do
31
+ siphon = Siphon::Base.new("").has_scopes(published: :integer)
32
+
33
+ expect(siphon.scope_datatypes).to eq(published: :integer)
34
+ end
35
+ end
36
+
37
+ describe "#filter" do
38
+
39
+ it "`sends` one params keys to an ActiveRelation" do
40
+ relation = double
41
+ expect(relation).to receive(:name).with("jack").and_return(relation)
42
+ siphon = Siphon::Base.new(relation).has_scopes(name:nil)
43
+
44
+ expect(siphon.filter(name: "jack")).to eq relation
45
+ end
46
+
47
+ it "`sends` one params keys to an ActiveRelation" do
48
+ relation = double
49
+ expect(relation).to receive(:age_gt).with(18).and_return(relation)
50
+ siphon = Siphon::Base.new(relation).has_scopes(age_gt: :integer)
51
+
52
+ expect(siphon.filter(age_gt: "18")).to eq relation
53
+ end
54
+
55
+ it "`sends` one params keys to an ActiveRelation" do
56
+ relation = double
57
+ expect(relation).to receive(:admin).with(false).and_return(relation)
58
+ siphon = Siphon::Base.new(relation).has_scopes(admin: :boolean)
59
+
60
+ expect(siphon.filter(admin: "false")).to eq relation
61
+ end
62
+
63
+ it "`sends` multiple keys to an ActiveRelation" do
64
+ relation = double
65
+ expect(relation).to receive(:admin).with(false).and_return(relation)
66
+ 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
+
69
+ expect(siphon.filter(age_gt: "18", admin: "false")).to eq relation
70
+ end
71
+
72
+
73
+
74
+ end
75
+
76
+
77
+ # 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
99
+
100
+ it "maps datatype date" do
101
+ pending "map datatype date"
102
+ siphon = Siphon::Base.new("")
103
+ siphon.map_scope_datatypes({age_gt: :integer}, {age_gt: "18"}).should == {age_gt: 18}
104
+ end
105
+ end
106
+
107
+ end
@@ -0,0 +1,11 @@
1
+
2
+
3
+ describe Siphon do
4
+
5
+ describe "#new" do
6
+
7
+ it "shoot a Siphon::Base instance" do
8
+ Siphon.apply.should be_an_instance_of(Siphon::Base)
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,25 @@
1
+ # This file was generated by the `rspec --init` command. Conventionally, all
2
+ # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
+ # Require this file using `require "spec_helper"` to ensure that it is only
4
+ # loaded once.
5
+ #
6
+ require "siphon"
7
+
8
+
9
+ class ActiveRelation
10
+ def self.title_with
11
+ end
12
+ end
13
+
14
+ # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
15
+ RSpec.configure do |config|
16
+ config.treat_symbols_as_metadata_keys_with_true_values = true
17
+ config.run_all_when_everything_filtered = true
18
+ config.filter_run :focus
19
+
20
+ # Run specs in random order to surface order dependencies. If you find an
21
+ # order dependency and want to debug it, you can fix the order by providing
22
+ # the seed, which is printed after each run.
23
+ # --seed 1234
24
+ # config.order = 'random'
25
+ end
metadata ADDED
@@ -0,0 +1,136 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: siphon
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Charles Sistovaris
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-08-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '1.3'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '1.3'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
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: pry
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
+ description: Siphon enables you to easily apply/combine/exclude your ActiveRecord
84
+ scopes with params
85
+ email:
86
+ - charlysisto@gmail.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - .gitignore
92
+ - .pryrc
93
+ - .rspec
94
+ - .ruby-version
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.md
98
+ - Rakefile
99
+ - lib/siphon.rb
100
+ - lib/siphon/base.rb
101
+ - lib/siphon/version.rb
102
+ - siphon.gemspec
103
+ - spec/class/active_relation.rb
104
+ - spec/siphon/base_spec.rb
105
+ - spec/siphon_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: http://github.com/charly/siphon
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - '>='
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Siphon enables you to easily apply/combine/exclude your ActiveRecord scopes
131
+ with params
132
+ test_files:
133
+ - spec/class/active_relation.rb
134
+ - spec/siphon/base_spec.rb
135
+ - spec/siphon_spec.rb
136
+ - spec/spec_helper.rb