gauze 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 960130145b856193b59bbaf1009e3e2767231b37
4
+ data.tar.gz: 9ac939b5b580dc0f639dba1af90712a307d6da60
5
+ SHA512:
6
+ metadata.gz: 91b76a3d08c03012d0a54e8c7703cf18f18de4aa7eeec7b869d9e904bc1d7a0d235cd7de2dbc057fdc21fbd02a566413bce479b2194202ee4371e03419838a5a
7
+ data.tar.gz: 588faba6a77e9e1bf7243a80b5c17a828a9f1a21ec913dea9e2e5fcc77e4d3ca1a84be9f2477e1f8e40dc7786bd5c61a7d5c8f25b0b38c39869c3d2bfa0605cb
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in gauze.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Chris Ostrowski
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,65 @@
1
+ # Gauze
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'gauze'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install gauze
20
+
21
+ ## Usage
22
+
23
+ Create a class that your controller can find.
24
+ Define filters using the Gauze DSL.
25
+
26
+ The filter method accepts 4 arguments.
27
+ `filter :param_key, :column_name, :arel_method, :preprocessor`
28
+
29
+ * `:param_key` - The key to look for when params are passed in.
30
+ * `:column_name` - The name of the column that exists on `ActiveRecord::Base.arel_table`
31
+ * `:arel_method` - The AREL method to use in the `where` stanza.
32
+ * `:preprocessor` - (Optional) Takes a block to make an changes to the value before its passed into the AREL method.
33
+
34
+ ```ruby
35
+ class EventFilters < Gauze::Base
36
+ filter :start_range, :created_at, :gteq, -> val {Chronic.parse(val)}
37
+ filter :end_range, :created_at, :lteq, -> val {Chronic.parse(val)}
38
+ end
39
+ ```
40
+
41
+ Then in your controller:
42
+ ```ruby
43
+ class EventsController < ActionController::Base
44
+ def index
45
+ @events = EventFilters.build(Event, params)
46
+ render json: @events
47
+ end
48
+ end
49
+ ```
50
+
51
+ Because *Gauze* uses AREL to construct queries, you can use `.build` just like any other scope. You can pass relational objects & chain more relations as you need.
52
+ ```
53
+ def index
54
+ @events = EventFilters.build(current_user.events, params).order(created_at: :desc).where(name: "Football Game")
55
+ render json: @events
56
+ end
57
+ ```
58
+
59
+ ## Contributing
60
+
61
+ 1. Fork it ( https://github.com/TheKidCoder/gauze/fork )
62
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
63
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
64
+ 4. Push to the branch (`git push origin my-new-feature`)
65
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
data/gauze.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'gauze/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "gauze"
8
+ spec.version = Gauze::VERSION
9
+ spec.authors = ["Chris Ostrowski"]
10
+ spec.email = ["chris@madebyfunction.com"]
11
+ spec.summary = %q{A thin filtering library leveraging AREL & ActionController.}
12
+ spec.description = %q{Using scopes on a model that only need to accessed in a controller seems like a leakage of SRP. This gem will allow you to write simple filtering logic that translates your params to AREL queries.}
13
+ spec.homepage = "https://github.com/TheKidCoder/Gauze"
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_development_dependency "bundler", "~> 1.7"
22
+ spec.add_development_dependency "rake", "~> 10.0"
23
+ end
data/lib/gauze/base.rb ADDED
@@ -0,0 +1,43 @@
1
+ module Gauze
2
+ class Base
3
+ def self.filter(param_key, column_name, arel_method, preprocessor = nil)
4
+ @@filters ||= []
5
+ @@filters.push param_key: param_key, column: column_name, method: arel_method, preprocessor: preprocessor
6
+ end
7
+
8
+ def self.build(resource, params = {})
9
+ new(resource, params).build_nodes
10
+ end
11
+
12
+ def initialize(resource, params = {})
13
+ @resource = resource
14
+ @params = params.symbolize_keys
15
+ end
16
+
17
+ def build_nodes
18
+ wheres = applied_filters.map {|obj| build_arel_filter(obj)}
19
+ _query = @resource
20
+ wheres.each {|node| _query = _query.where(node)}
21
+ return _query
22
+ end
23
+
24
+ def build_arel_filter(filter_hash)
25
+ filter_val = @params[filter_hash[:param_key]]
26
+ filter_val = filter_hash[:preprocessor].call(filter_val) if filter_hash[:preprocessor]
27
+
28
+ @resource.arel_table[filter_hash[:column]].method(filter_hash[:method]).call(filter_val)
29
+ end
30
+
31
+ private
32
+ def applied_filters
33
+ _filters = []
34
+ @params.each do |k,v|
35
+ next unless v.present?
36
+ next unless filter = @@filters.find {|obj| obj[:param_key] == k}
37
+ _filters.push filter
38
+ end
39
+
40
+ _filters
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,3 @@
1
+ module Gauze
2
+ VERSION = "0.0.1"
3
+ end
data/lib/gauze.rb ADDED
@@ -0,0 +1,5 @@
1
+ require "gauze/version"
2
+
3
+ module Gauze
4
+ # Your code goes here...
5
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gauze
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Chris Ostrowski
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-13 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Using scopes on a model that only need to accessed in a controller seems
42
+ like a leakage of SRP. This gem will allow you to write simple filtering logic that
43
+ translates your params to AREL queries.
44
+ email:
45
+ - chris@madebyfunction.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".gitignore"
51
+ - Gemfile
52
+ - LICENSE.txt
53
+ - README.md
54
+ - Rakefile
55
+ - gauze.gemspec
56
+ - lib/gauze.rb
57
+ - lib/gauze/base.rb
58
+ - lib/gauze/version.rb
59
+ homepage: https://github.com/TheKidCoder/Gauze
60
+ licenses:
61
+ - MIT
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.4.5
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: A thin filtering library leveraging AREL & ActionController.
83
+ test_files: []