hario 0.1.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 +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +22 -0
- data/README.md +33 -0
- data/Rakefile +9 -0
- data/hario.gemspec +25 -0
- data/lib/hario/behaviours/filter.rb +57 -0
- data/lib/hario/behaviours/pluck.rb +42 -0
- data/lib/hario/behaviours/utils.rb +11 -0
- data/lib/hario/version.rb +3 -0
- data/lib/hario.rb +36 -0
- data/test/database.yml +3 -0
- data/test/debug.log +16 -0
- data/test/filter_test.rb +19 -0
- data/test/models.rb +5 -0
- data/test/pluck_test.rb +0 -0
- data/test/schema.rb +15 -0
- data/test/test_helper.rb +12 -0
- metadata +125 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 314a179b80afaf6621fc5fd75b8ac07f9b666e83
|
4
|
+
data.tar.gz: 66974a51f922f7141e76032957f94bc671c98a34
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e7374fc27c93e5d1eb848a1b955919f9b938913a0120277a58e10a33b7d9fa7bed23faf0f57cf2ba8e37794cb549606629d9b0285c7d4570ab40413799180c01
|
7
|
+
data.tar.gz: 0fdc5240d5b1959848b1b76280cdd97219493d639a22bcf735fa1207acd0d4e1ef0178945b9d0637be46a808d0107f5bd2c23a49cc7c92d02e4f027313b4f949
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 Mike Campbell
|
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,33 @@
|
|
1
|
+
# Hario
|
2
|
+
|
3
|
+
Hario provides ActiveRecord filtering for Rails APIs.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'hario'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install hario
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
def Animal < ActiveRecord::Base
|
23
|
+
include Hario::Filterable
|
24
|
+
end
|
25
|
+
```
|
26
|
+
|
27
|
+
## Contributing
|
28
|
+
|
29
|
+
1. Fork it ( https://github.com/[my-github-username]/hario/fork )
|
30
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
31
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
32
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
33
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/hario.gemspec
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'hario/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "hario"
|
8
|
+
spec.version = Hario::VERSION
|
9
|
+
spec.authors = ["Mike Campbell"]
|
10
|
+
spec.email = ["mike@wordofmike.net"]
|
11
|
+
spec.summary = %q{Hario provides ActiveRecord filtering for Rails APIs.}
|
12
|
+
spec.homepage = "https://github.com/meritec/hario"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0")
|
16
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
17
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_runtime_dependency "activerecord", "~> 3.0"
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
23
|
+
spec.add_development_dependency "rake", "~> 0"
|
24
|
+
spec.add_development_dependency "sqlite3", "~> 0"
|
25
|
+
end
|
@@ -0,0 +1,57 @@
|
|
1
|
+
require "hario/behaviours/utils"
|
2
|
+
|
3
|
+
class FilterParser
|
4
|
+
include ParserUtils
|
5
|
+
|
6
|
+
OPERATORS = { lt: '<', gt: '>', lte: '<=', gte: '>=', like: 'like', equals: '=' }
|
7
|
+
|
8
|
+
attr_accessor :join_clause, :where_clauses
|
9
|
+
|
10
|
+
def initialize(filters, klass)
|
11
|
+
@filters = filters
|
12
|
+
@klass = klass
|
13
|
+
|
14
|
+
parse_filters
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
def parse_filters
|
19
|
+
@join_clause, @where_clauses = @filters.inject([{}, []]) do |m, (descriptor, value)|
|
20
|
+
association_chain, attribute, operator = parse_descriptor(descriptor)
|
21
|
+
condition = build_condition(association_chain, attribute, operator, value)
|
22
|
+
|
23
|
+
nested_associations = (association_chain << {}).reverse.inject { |v, key| { key => v } }
|
24
|
+
joins = m[0].deep_merge(nested_associations)
|
25
|
+
wheres = m[1] + [condition]
|
26
|
+
[joins, wheres]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def parse_descriptor(descriptor)
|
31
|
+
parts = descriptor.split('.')
|
32
|
+
operator = parts.pop.to_sym
|
33
|
+
attribute = parts.pop
|
34
|
+
association_chain = parts
|
35
|
+
|
36
|
+
[association_chain, attribute, operator]
|
37
|
+
end
|
38
|
+
|
39
|
+
def build_condition(association_chain, attribute, operator, value)
|
40
|
+
if association_chain.any?
|
41
|
+
attribute_table = table_name_from_association_chain(association_chain)
|
42
|
+
end
|
43
|
+
|
44
|
+
case operator
|
45
|
+
when :equals
|
46
|
+
condition = { attribute => value }
|
47
|
+
condition = { attribute_table => condition } if attribute_table
|
48
|
+
else
|
49
|
+
value = "%#{value}%" if operator == :like
|
50
|
+
operator = OPERATORS[operator]
|
51
|
+
condition = ["? #{operator} ?", attribute, value]
|
52
|
+
condition[1].prepend("#{attribute_table}.") if attribute_table
|
53
|
+
end
|
54
|
+
|
55
|
+
condition
|
56
|
+
end
|
57
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require "hario/behaviours/utils"
|
2
|
+
|
3
|
+
class PluckParser
|
4
|
+
include ParserUtils
|
5
|
+
|
6
|
+
attr_accessor :join_clause, :pluck_clause
|
7
|
+
|
8
|
+
def initialize(pluck, klass)
|
9
|
+
@pluck = pluck
|
10
|
+
@klass = klass
|
11
|
+
|
12
|
+
parse_pluck
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
def parse_pluck
|
17
|
+
@join_clause = {}
|
18
|
+
@pluck_clause = [[@klass.table_name, 'id'].join('.')]
|
19
|
+
|
20
|
+
ns, no_ns = @pluck.partition{ |p| p.include?('.') }
|
21
|
+
|
22
|
+
no_ns.each{ |p| @pluck_clause << [@klass.table_name, p].join('.') }
|
23
|
+
|
24
|
+
ns.each do |p|
|
25
|
+
association_chain, attribute = parse_namespace(p)
|
26
|
+
|
27
|
+
nested_associations = (association_chain << {}).reverse.inject { |v, key| { key => v } }
|
28
|
+
@join_clause.deep_merge!(nested_associations)
|
29
|
+
|
30
|
+
attribute_table = table_name_from_association_chain(association_chain)
|
31
|
+
@pluck_clause << [attribute_table, attribute].join('.')
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_namespace(namespace)
|
36
|
+
parts = namespace.split('.')
|
37
|
+
attribute = parts.pop
|
38
|
+
association_chain = parts
|
39
|
+
|
40
|
+
[association_chain, attribute]
|
41
|
+
end
|
42
|
+
end
|
data/lib/hario.rb
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
require "hario/version"
|
2
|
+
require "hario/behaviours/filter"
|
3
|
+
require "hario/behaviours/pluck"
|
4
|
+
|
5
|
+
module Hario
|
6
|
+
module Filterable
|
7
|
+
extend ActiveSupport::Concern
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
def search(filters, pluck = nil)
|
11
|
+
s = all
|
12
|
+
s = s.apply_filters(filters) if filters
|
13
|
+
s = s.apply_pluck(pluck) if pluck
|
14
|
+
s
|
15
|
+
end
|
16
|
+
|
17
|
+
def apply_filters(filters)
|
18
|
+
fp = FilterParser.new(filters, self)
|
19
|
+
|
20
|
+
fp.where_clauses.reduce(joins(fp.join_clause), &:where)
|
21
|
+
end
|
22
|
+
|
23
|
+
def apply_pluck(pluck)
|
24
|
+
pp = PluckParser.new(pluck, self)
|
25
|
+
|
26
|
+
results = hash_pluck(*pp.pluck_clause)
|
27
|
+
|
28
|
+
remove_local_table_prefix(results)
|
29
|
+
end
|
30
|
+
|
31
|
+
def remove_local_table_prefix(results)
|
32
|
+
results.map{ |r| r.transform_keys!{ |k| k.gsub(/^#{table_name}\./, '') } }
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/test/database.yml
ADDED
data/test/debug.log
ADDED
@@ -0,0 +1,16 @@
|
|
1
|
+
# Logfile created on 2015-06-25 23:10:26 +0100 by logger.rb/44203
|
2
|
+
D, [2015-06-25T23:10:27.058465 #74888] DEBUG -- : [1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
3
|
+
D, [2015-06-25T23:10:27.088174 #74888] DEBUG -- : [1m[35mSQL (0.2ms)[0m INSERT INTO "brands" ("name") VALUES (?) [["name", "Adidas"]]
|
4
|
+
D, [2015-06-25T23:10:27.104100 #74888] DEBUG -- : [1m[36m (0.2ms)[0m [1mcommit transaction[0m
|
5
|
+
D, [2015-06-25T23:10:27.127906 #74888] DEBUG -- : [1m[35mBrand Load (0.2ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."name" = ? [["name", "Adidas"]]
|
6
|
+
D, [2015-06-25T23:10:27.145343 #74888] DEBUG -- : [1m[36mSQL (15.2ms)[0m [1mDELETE FROM "brands"[0m
|
7
|
+
D, [2015-06-25T23:11:17.882386 #74899] DEBUG -- : [1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
8
|
+
D, [2015-06-25T23:11:17.912442 #74899] DEBUG -- : [1m[35mSQL (0.3ms)[0m INSERT INTO "brands" ("name") VALUES (?) [["name", "Adidas"]]
|
9
|
+
D, [2015-06-25T23:11:17.913038 #74899] DEBUG -- : [1m[36m (0.1ms)[0m [1mcommit transaction[0m
|
10
|
+
D, [2015-06-25T23:11:17.939361 #74899] DEBUG -- : [1m[35mBrand Load (0.2ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."name" = ? [["name", "Adidas"]]
|
11
|
+
D, [2015-06-25T23:11:17.941683 #74899] DEBUG -- : [1m[36mSQL (0.1ms)[0m [1mDELETE FROM "brands"[0m
|
12
|
+
D, [2015-06-25T23:11:27.792300 #74906] DEBUG -- : [1m[36m (0.1ms)[0m [1mbegin transaction[0m
|
13
|
+
D, [2015-06-25T23:11:27.857213 #74906] DEBUG -- : [1m[35mSQL (0.2ms)[0m INSERT INTO "brands" ("name") VALUES (?) [["name", "Adidas"]]
|
14
|
+
D, [2015-06-25T23:11:27.857824 #74906] DEBUG -- : [1m[36m (0.1ms)[0m [1mcommit transaction[0m
|
15
|
+
D, [2015-06-25T23:11:27.885282 #74906] DEBUG -- : [1m[35mBrand Load (0.2ms)[0m SELECT "brands".* FROM "brands" WHERE "brands"."name" = ? [["name", "Adidas"]]
|
16
|
+
D, [2015-06-25T23:11:27.887789 #74906] DEBUG -- : [1m[36mSQL (0.1ms)[0m [1mDELETE FROM "brands"[0m
|
data/test/filter_test.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'minitest/autorun'
|
2
|
+
require_relative 'test_helper'
|
3
|
+
|
4
|
+
class FilterTest < Minitest::Test
|
5
|
+
def setup
|
6
|
+
@brand = Brand.create!(name: "Adidas")
|
7
|
+
end
|
8
|
+
|
9
|
+
def teardown
|
10
|
+
Brand.delete_all
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_simple_filter
|
14
|
+
filters = { 'name.equals' => "Adidas" }
|
15
|
+
brands = Brand.search(filters)
|
16
|
+
|
17
|
+
assert !brands.any?{|b| b.name != "Adidas" }
|
18
|
+
end
|
19
|
+
end
|
data/test/models.rb
ADDED
data/test/pluck_test.rb
ADDED
File without changes
|
data/test/schema.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
ActiveRecord::Schema.define(:version => 1) do
|
2
|
+
create_table :brands, :force => true do |t|
|
3
|
+
t.column :name, :string
|
4
|
+
end
|
5
|
+
|
6
|
+
create_table :products, :force => true do |t|
|
7
|
+
t.column :name, :string
|
8
|
+
t.column :product_category_id, :integer
|
9
|
+
t.column :brand_id, :integer
|
10
|
+
end
|
11
|
+
|
12
|
+
create_table :product_categories, :force => true do |t|
|
13
|
+
t.column :name, :string
|
14
|
+
end
|
15
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
|
4
|
+
require "active_record"
|
5
|
+
require "hario"
|
6
|
+
require "models"
|
7
|
+
|
8
|
+
ActiveRecord::Base.configurations = YAML.load_file(File.join(File.dirname(__FILE__), 'database.yml'))
|
9
|
+
ActiveRecord::Base.establish_connection(ENV['DB'] || :sqlite3)
|
10
|
+
load(File.join(File.dirname(__FILE__), "/schema.rb"))
|
11
|
+
|
12
|
+
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hario
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mike Campbell
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-06-25 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.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.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
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: sqlite3
|
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
|
+
description:
|
70
|
+
email:
|
71
|
+
- mike@wordofmike.net
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files: []
|
75
|
+
files:
|
76
|
+
- ".gitignore"
|
77
|
+
- Gemfile
|
78
|
+
- LICENSE.txt
|
79
|
+
- README.md
|
80
|
+
- Rakefile
|
81
|
+
- hario.gemspec
|
82
|
+
- lib/hario.rb
|
83
|
+
- lib/hario/behaviours/filter.rb
|
84
|
+
- lib/hario/behaviours/pluck.rb
|
85
|
+
- lib/hario/behaviours/utils.rb
|
86
|
+
- lib/hario/version.rb
|
87
|
+
- test/database.yml
|
88
|
+
- test/debug.log
|
89
|
+
- test/filter_test.rb
|
90
|
+
- test/models.rb
|
91
|
+
- test/pluck_test.rb
|
92
|
+
- test/schema.rb
|
93
|
+
- test/test_helper.rb
|
94
|
+
homepage: https://github.com/meritec/hario
|
95
|
+
licenses:
|
96
|
+
- MIT
|
97
|
+
metadata: {}
|
98
|
+
post_install_message:
|
99
|
+
rdoc_options: []
|
100
|
+
require_paths:
|
101
|
+
- lib
|
102
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
103
|
+
requirements:
|
104
|
+
- - ">="
|
105
|
+
- !ruby/object:Gem::Version
|
106
|
+
version: '0'
|
107
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
108
|
+
requirements:
|
109
|
+
- - ">="
|
110
|
+
- !ruby/object:Gem::Version
|
111
|
+
version: '0'
|
112
|
+
requirements: []
|
113
|
+
rubyforge_project:
|
114
|
+
rubygems_version: 2.2.2
|
115
|
+
signing_key:
|
116
|
+
specification_version: 4
|
117
|
+
summary: Hario provides ActiveRecord filtering for Rails APIs.
|
118
|
+
test_files:
|
119
|
+
- test/database.yml
|
120
|
+
- test/debug.log
|
121
|
+
- test/filter_test.rb
|
122
|
+
- test/models.rb
|
123
|
+
- test/pluck_test.rb
|
124
|
+
- test/schema.rb
|
125
|
+
- test/test_helper.rb
|