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 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
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hario.gemspec
4
+ gemspec
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
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs = ["lib"]
6
+ t.warning = true
7
+ t.verbose = true
8
+ t.test_files = FileList['test/*_test.rb']
9
+ end
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
@@ -0,0 +1,11 @@
1
+ module ParserUtils
2
+ def table_name_from_association_chain(association_chain)
3
+ head = @klass
4
+
5
+ association_chain.each do |a_name|
6
+ head = head.reflect_on_all_associations.find{ |a| a.name.to_s == a_name }.klass
7
+ end
8
+
9
+ head.table_name
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Hario
2
+ VERSION = "0.1.0"
3
+ 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
@@ -0,0 +1,3 @@
1
+ sqlite3:
2
+ adapter: sqlite3
3
+ database: ":memory:"
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 -- :  (0.1ms) begin transaction
3
+ D, [2015-06-25T23:10:27.088174 #74888] DEBUG -- : SQL (0.2ms) INSERT INTO "brands" ("name") VALUES (?) [["name", "Adidas"]]
4
+ D, [2015-06-25T23:10:27.104100 #74888] DEBUG -- :  (0.2ms) commit transaction
5
+ D, [2015-06-25T23:10:27.127906 #74888] DEBUG -- : Brand Load (0.2ms) SELECT "brands".* FROM "brands" WHERE "brands"."name" = ? [["name", "Adidas"]]
6
+ D, [2015-06-25T23:10:27.145343 #74888] DEBUG -- : SQL (15.2ms) DELETE FROM "brands"
7
+ D, [2015-06-25T23:11:17.882386 #74899] DEBUG -- :  (0.1ms) begin transaction
8
+ D, [2015-06-25T23:11:17.912442 #74899] DEBUG -- : SQL (0.3ms) INSERT INTO "brands" ("name") VALUES (?) [["name", "Adidas"]]
9
+ D, [2015-06-25T23:11:17.913038 #74899] DEBUG -- :  (0.1ms) commit transaction
10
+ D, [2015-06-25T23:11:17.939361 #74899] DEBUG -- : Brand Load (0.2ms) SELECT "brands".* FROM "brands" WHERE "brands"."name" = ? [["name", "Adidas"]]
11
+ D, [2015-06-25T23:11:17.941683 #74899] DEBUG -- : SQL (0.1ms) DELETE FROM "brands"
12
+ D, [2015-06-25T23:11:27.792300 #74906] DEBUG -- :  (0.1ms) begin transaction
13
+ D, [2015-06-25T23:11:27.857213 #74906] DEBUG -- : SQL (0.2ms) INSERT INTO "brands" ("name") VALUES (?) [["name", "Adidas"]]
14
+ D, [2015-06-25T23:11:27.857824 #74906] DEBUG -- :  (0.1ms) commit transaction
15
+ D, [2015-06-25T23:11:27.885282 #74906] DEBUG -- : Brand Load (0.2ms) SELECT "brands".* FROM "brands" WHERE "brands"."name" = ? [["name", "Adidas"]]
16
+ D, [2015-06-25T23:11:27.887789 #74906] DEBUG -- : SQL (0.1ms) DELETE FROM "brands"
@@ -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
@@ -0,0 +1,5 @@
1
+ require 'hario'
2
+
3
+ class Brand < ActiveRecord::Base
4
+ include Hario::Filterable
5
+ end
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
@@ -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