hario 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/hario/behaviours/filter.rb +1 -1
- data/lib/hario/behaviours/pluck.rb +1 -1
- data/lib/hario/version.rb +1 -1
- data/lib/hario.rb +20 -20
- data/test/hario_test_class.rb +1 -1
- data/test/models.rb +3 -1
- data/test/pluck_test.rb +19 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 879fa179f9fab01c486396fc9afd19c19b98c674
|
4
|
+
data.tar.gz: 180dd6a66f7b4fd8a63403a70a955a8f9a5da1da
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4dd7b5574c72a7157316dd8a59f5f8f809c8c12d896a563627eddf8eed6e76fc8feafb7766edd45137a7b8a468dd314649592ef2eedb3d369cfbdd41a26cbad6
|
7
|
+
data.tar.gz: e972fde3bfb04f2b42e7ff87d2958ac4d6d442da443c61aaa7907d4452712bb748a8efe0116c6583e15f4de8f266d58c59fda32f82a913c1ec3b951f63178f1d
|
data/README.md
CHANGED
@@ -20,7 +20,7 @@ class FilterParser
|
|
20
20
|
association_chain, attribute, operator = parse_descriptor(descriptor)
|
21
21
|
condition = build_condition(association_chain, attribute, operator, value)
|
22
22
|
|
23
|
-
nested_associations = (association_chain << {}).reverse.inject { |v, key| { key => v } }
|
23
|
+
nested_associations = (association_chain.dup << {}).reverse.inject { |v, key| { key => v } }
|
24
24
|
joins = m[0].deep_merge(nested_associations)
|
25
25
|
wheres = m[1] + [condition]
|
26
26
|
[joins, wheres]
|
@@ -24,7 +24,7 @@ class PluckParser
|
|
24
24
|
ns.each do |p|
|
25
25
|
association_chain, attribute = parse_namespace(p)
|
26
26
|
|
27
|
-
nested_associations = (association_chain << {}).reverse.inject { |v, key| { key => v } }
|
27
|
+
nested_associations = (association_chain.dup << {}).reverse.inject { |v, key| { key => v } }
|
28
28
|
@join_clause.deep_merge!(nested_associations)
|
29
29
|
|
30
30
|
attribute_table = table_name_from_association_chain(association_chain)
|
data/lib/hario/version.rb
CHANGED
data/lib/hario.rb
CHANGED
@@ -4,33 +4,33 @@ require "hario/behaviours/pluck"
|
|
4
4
|
|
5
5
|
module Hario
|
6
6
|
module Filterable
|
7
|
-
|
7
|
+
def search(filters, pluck = nil)
|
8
|
+
s = all
|
9
|
+
s = s.apply_filters(filters) if filters
|
10
|
+
s = s.apply_pluck(pluck) if pluck
|
11
|
+
s
|
12
|
+
end
|
8
13
|
|
9
|
-
|
10
|
-
|
11
|
-
s = all
|
12
|
-
s = s.apply_filters(filters) if filters
|
13
|
-
s = s.apply_pluck(pluck) if pluck
|
14
|
-
s
|
15
|
-
end
|
14
|
+
def apply_filters(filters)
|
15
|
+
fp = FilterParser.new(filters, self)
|
16
16
|
|
17
|
-
|
18
|
-
|
17
|
+
fp.where_clauses.reduce(joins(fp.join_clause), &:where)
|
18
|
+
end
|
19
19
|
|
20
|
-
|
21
|
-
|
20
|
+
def apply_pluck(pluck)
|
21
|
+
pp = PluckParser.new(pluck, self)
|
22
22
|
|
23
|
-
|
24
|
-
pp = PluckParser.new(pluck, self)
|
23
|
+
results = joins(pp.join_clause).hash_pluck(*pp.pluck_clause)
|
25
24
|
|
26
|
-
|
25
|
+
remove_local_table_prefix(results)
|
26
|
+
end
|
27
27
|
|
28
|
-
|
29
|
-
|
28
|
+
def remove_local_table_prefix(results)
|
29
|
+
results.map{ |r| r.transform_keys!{ |k| k.gsub(/^#{table_name}\./, '') } }
|
30
|
+
end
|
30
31
|
|
31
|
-
|
32
|
-
|
33
|
-
end
|
32
|
+
def hash_pluck(*keys)
|
33
|
+
pluck(*keys).map{ |vals| Hash[keys.zip(Array(vals))] }
|
34
34
|
end
|
35
35
|
end
|
36
36
|
end
|
data/test/hario_test_class.rb
CHANGED
data/test/models.rb
CHANGED
@@ -1,12 +1,14 @@
|
|
1
1
|
require 'hario'
|
2
2
|
|
3
3
|
class Brand < ActiveRecord::Base
|
4
|
-
|
4
|
+
extend Hario::Filterable
|
5
5
|
|
6
6
|
has_many :products
|
7
7
|
end
|
8
8
|
|
9
9
|
class Product < ActiveRecord::Base
|
10
|
+
extend Hario::Filterable
|
11
|
+
|
10
12
|
belongs_to :brand
|
11
13
|
belongs_to :category, class_name: "ProductCategory"
|
12
14
|
end
|
data/test/pluck_test.rb
CHANGED
@@ -0,0 +1,19 @@
|
|
1
|
+
require_relative 'test_helper'
|
2
|
+
|
3
|
+
class PluckTest < Hario::Test
|
4
|
+
def test_simple_pluck
|
5
|
+
brands = Brand.search(nil, ["name"])
|
6
|
+
|
7
|
+
assert_equal ["id", "name"], brands.flat_map(&:keys).uniq,
|
8
|
+
"Pluck not returning correct attributes"
|
9
|
+
end
|
10
|
+
|
11
|
+
def test_pluck_through_association
|
12
|
+
# TODO: fix this so that it returns the association_name.attribute as
|
13
|
+
# the hash key, rather than table_name.attribute (different to input)
|
14
|
+
products = Product.search(nil, ["name", "brand.name"])
|
15
|
+
|
16
|
+
assert_equal ["id", "name", "brands.name"], products.flat_map(&:keys).uniq,
|
17
|
+
"Pluck not returning correct attributes with association pluck"
|
18
|
+
end
|
19
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: hario
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mike Campbell
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-07-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|