elasticband 0.1.5 → 0.1.6

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- MWFmN2E3YzkzYjMxY2Y0NjFlMGFiODRhZjM4MmFlMzE2NGE5ODNjZA==
4
+ YjdlOTdhMWJmYzYzNWJhY2E2MjMyNDAyNzg2YTkyYzVlMzFjYWMxYQ==
5
5
  data.tar.gz: !binary |-
6
- NzcyMzlmZTg2YmU3ZmZjNTAxNDMxMTQ3OTFhZmY1ZDA5NWY4ODQwYQ==
6
+ Y2VmMzk3ZDAxYzE0OGIyYTcwNTFlMzdkMDRjYzU3MjVjYTJkMTMxMg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NGRmZDJlYzQ2NzliMzM0MWE0ZDY0MTgwY2NkYjEwYjAxNzk4YjVhNzA1ZTA3
10
- YWMxYzAwZDJlZDRkMTgyOWQyOGQwNjdmNWQ2MWI4N2QyODgwZDI4MDhkYjI1
11
- YWUyYTcyMmY1NjAzMTA3ZWI1NTU3ZjIxZGJjMGFlZDkzZTE2Yjk=
9
+ YTkzNjAwYTM4ZjcxZjZmMjdjOGJmYzdiMGFmODc2OWJlZDJjNzRlODhhZDdk
10
+ NTkxYmViY2NkM2Y5NDhiNzcxYmZkYWMyNDhkYTEwNTA3NWZhY2UyN2RkM2Mz
11
+ YmJjODdkZjMyNTJiYjhlYzkyNzdjZGEwMGNkNDgxNjNhYzg4NzE=
12
12
  data.tar.gz: !binary |-
13
- OGQyOWY1OGIxOWU2ZWQ3NzgxNzdlOTYxYTM3ODRkYTE3ZmUzMGVjMDU0YTQx
14
- ZjZhNGI2MThkMDFlODVhYTU0MWUwZjFiYzA1YWVjZWM3NTliMDVhZTE3NGNm
15
- ZWJkZmNkZGI1ZDYwOThiMTQwNjc0ZjgyMzZjNTQyZDdiNTBkMTI=
13
+ ZmRhZWRhZDg2YzQwYmNmZGNhZjMxYzM3ZWQ3ODY2MmIyMGFiOTA4MjAzZjkx
14
+ MTM3ZjNjMGRkODFjMTE5ZGY3ZmEzOTc2ZjcyZjNkMTZhZGQwYjAyODZiNDJk
15
+ ODEyNzI1YzhmYjhlYThjYzczZmVmMGEyOWMxMWRhM2FjODNmZWE=
data/.gitignore CHANGED
@@ -4,6 +4,7 @@
4
4
 
5
5
  /.bundle/
6
6
  /.yardoc
7
+ /Gemfile.lock
7
8
  /_yardoc/
8
9
  /coverage/
9
10
  /doc/
data/README.md CHANGED
@@ -1,5 +1,10 @@
1
1
  # Elasticband
2
- [![Build Status](https://travis-ci.org/LoveMondays/elasticband.svg)](https://travis-ci.org/LoveMondays/elasticband) [![Code Climate](https://codeclimate.com/github/LoveMondays/elasticband/badges/gpa.svg)](https://codeclimate.com/github/LoveMondays/elasticband) [![Test Coverage](https://codeclimate.com/github/LoveMondays/elasticband/badges/coverage.svg)](https://codeclimate.com/github/LoveMondays/elasticband/coverage)
2
+
3
+ [![Build Status](https://travis-ci.org/LoveMondays/elasticband.svg)](https://travis-ci.org/LoveMondays/elasticband)
4
+ [![Code Climate](https://codeclimate.com/github/LoveMondays/elasticband/badges/gpa.svg)](https://codeclimate.com/github/LoveMondays/elasticband)
5
+ [![Test Coverage](https://codeclimate.com/github/LoveMondays/elasticband/badges/coverage.svg)](https://codeclimate.com/github/LoveMondays/elasticband/coverage)
6
+ [![Gem Version](https://badge.fury.io/rb/elasticband.svg)](http://badge.fury.io/rb/elasticband)
7
+ [![Dependency Status](https://gemnasium.com/LoveMondays/elasticband.svg)](https://gemnasium.com/LoveMondays/elasticband)
3
8
 
4
9
  Query builder for [elasticsearch-rails gem](https://github.com/elastic/elasticsearch-rails)
5
10
 
@@ -12,7 +12,7 @@ module Elasticband
12
12
  attr_accessor :name
13
13
 
14
14
  def initialize(name)
15
- self.name = name.to_s.gsub(/\W/, '_').to_sym
15
+ self.name = name.to_s.gsub(/\W/, '_'.freeze).to_sym
16
16
  end
17
17
 
18
18
  def to_h(aggregation_hash = {})
@@ -9,32 +9,51 @@ module Elasticband
9
9
  #
10
10
  # #### Examples
11
11
  # ```
12
- # Sort.parse(sort: [{ name: :desc }])
12
+ # Sort.parse(sort: { name: :desc })
13
13
  # => { sort: [{ name: :desc }] }
14
14
  #
15
- # Sort.parse(sort: [{ name: :asc }])
16
- # => { sort: [{ name: :asc }] }
15
+ # Sort.parse(sort: [{ name: :desc }, { created_at: :asc }])
16
+ # => { sort: [{ name: :desc }, { created_at: :asc }] }
17
17
  #
18
- # Sort.parse(sort: ['+name'])
19
- # => { sort: [{ name: :asc }] }
18
+ # Sort.parse(sort: '-name,+created_at,id')
19
+ # => { sort: [{ name: :desc }, { created_at: :asc }, { id: :asc }] }
20
20
  #
21
- # Sort.parse(sort: ['-name'])
22
- # => { sort: [{ name: :desc }] }
21
+ # Sort.parse(sort: ['-name', '+created_at', 'id'])
22
+ # => { sort: [{ name: :desc }, { created_at: :asc }, { id: :asc }] }
23
23
  # ```
24
24
  def parse(options)
25
- options[:sort].map { |param| parse_param(param) }
25
+ new(options[:sort]).parse
26
26
  end
27
+ end
28
+
29
+ attr_reader :sort_by
27
30
 
28
- private
31
+ def initialize(sort_by)
32
+ @sort_by = sort_by
33
+ end
29
34
 
30
- def parse_param(param)
31
- return param unless param.is_a?(String)
35
+ def parse
36
+ return if sort_by.blank?
37
+ return [sort_by] if sort_by.is_a?(Hash)
32
38
 
33
- direction = param.start_with?('-') ? :desc : :asc
34
- field = param.start_with?('-') || param.start_with?('+') ? param[1..-1] : param
39
+ @sort_by = sort_by.split(','.freeze) if sort_by.is_a?(String)
40
+ sort_by.map { |param| parse_param(param) }
41
+ end
35
42
 
36
- { field.to_sym => direction }
37
- end
43
+ private
44
+
45
+ def parse_param(param)
46
+ return param if param.is_a?(Hash)
47
+
48
+ { field(param).to_sym => direction(param) }
49
+ end
50
+
51
+ def field(param)
52
+ param.start_with?('-'.freeze) || param.start_with?('+'.freeze) ? param[1..-1] : param
53
+ end
54
+
55
+ def direction(param)
56
+ param.start_with?('-'.freeze) ? :desc : :asc
38
57
  end
39
58
  end
40
59
  end
@@ -1,3 +1,3 @@
1
1
  module Elasticband
2
- VERSION = '0.1.5'
2
+ VERSION = '0.1.6'
3
3
  end
data/spec/sort_spec.rb CHANGED
@@ -4,28 +4,34 @@ RSpec.describe Elasticband::Sort do
4
4
  describe '.parse' do
5
5
  subject { described_class.parse(sort: sort_params) }
6
6
 
7
- context 'with name desc' do
8
- let(:sort_params) { [{ name: :desc }] }
7
+ context 'when sort param nil' do
8
+ let(:sort_params) { nil }
9
+
10
+ it { is_expected.to be nil }
11
+ end
12
+
13
+ context 'when sort is a hash' do
14
+ let(:sort_params) { { name: :desc } }
9
15
 
10
16
  it { is_expected.to eq([{ name: :desc }]) }
11
17
  end
12
18
 
13
- context 'with name asc short way' do
14
- let(:sort_params) { ['+name'] }
19
+ context 'when sort is an array of hashes' do
20
+ let(:sort_params) { [{ name: :desc }, { created_at: :asc }] }
15
21
 
16
- it { is_expected.to eq([{ name: :asc }]) }
22
+ it { is_expected.to eq([{ name: :desc }, { created_at: :asc }]) }
17
23
  end
18
24
 
19
- context 'with name desc short way' do
20
- let(:sort_params) { ['-name'] }
25
+ context 'when sort is a string' do
26
+ let(:sort_params) { '-name,+created_at,id' }
21
27
 
22
- it { is_expected.to eq([{ name: :desc }]) }
28
+ it { is_expected.to eq([{ name: :desc }, { created_at: :asc }, { id: :asc }]) }
23
29
  end
24
30
 
25
- context 'with name as string' do
26
- let(:sort_params) { ['name'] }
31
+ context 'when sort is array of strings' do
32
+ let(:sort_params) { ['-name', '+created_at', 'id'] }
27
33
 
28
- it { is_expected.to eq([{ name: :asc }]) }
34
+ it { is_expected.to eq([{ name: :desc }, { created_at: :asc }, { id: :asc }]) }
29
35
  end
30
36
  end
31
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elasticband
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.5
4
+ version: 0.1.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Glauber Campinho
@@ -109,7 +109,6 @@ files:
109
109
  - .travis.yml
110
110
  - CODE_OF_CONDUCT.md
111
111
  - Gemfile
112
- - Gemfile.lock
113
112
  - LICENSE
114
113
  - README.md
115
114
  - Rakefile
data/Gemfile.lock DELETED
@@ -1,73 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- elasticband (0.1.5)
5
- activesupport
6
-
7
- GEM
8
- remote: https://rubygems.org/
9
- specs:
10
- activesupport (4.2.4)
11
- i18n (~> 0.7)
12
- json (~> 1.7, >= 1.7.7)
13
- minitest (~> 5.1)
14
- thread_safe (~> 0.3, >= 0.3.4)
15
- tzinfo (~> 1.1)
16
- ast (2.0.0)
17
- astrolabe (1.3.0)
18
- parser (>= 2.2.0.pre.3, < 3.0)
19
- codeclimate-test-reporter (0.4.7)
20
- simplecov (>= 0.7.1, < 1.0.0)
21
- diff-lcs (1.2.5)
22
- docile (1.1.5)
23
- dotenv (2.0.2)
24
- i18n (0.7.0)
25
- json (1.8.3)
26
- minitest (5.8.0)
27
- parser (2.2.2.6)
28
- ast (>= 1.1, < 3.0)
29
- powerpack (0.1.1)
30
- rainbow (2.0.0)
31
- rake (10.4.2)
32
- rspec (3.3.0)
33
- rspec-core (~> 3.3.0)
34
- rspec-expectations (~> 3.3.0)
35
- rspec-mocks (~> 3.3.0)
36
- rspec-core (3.3.1)
37
- rspec-support (~> 3.3.0)
38
- rspec-expectations (3.3.0)
39
- diff-lcs (>= 1.2.0, < 2.0)
40
- rspec-support (~> 3.3.0)
41
- rspec-mocks (3.3.1)
42
- diff-lcs (>= 1.2.0, < 2.0)
43
- rspec-support (~> 3.3.0)
44
- rspec-support (3.3.0)
45
- rubocop (0.32.1)
46
- astrolabe (~> 1.3)
47
- parser (>= 2.2.2.5, < 3.0)
48
- powerpack (~> 0.1)
49
- rainbow (>= 1.99.1, < 3.0)
50
- ruby-progressbar (~> 1.4)
51
- ruby-progressbar (1.7.5)
52
- simplecov (0.10.0)
53
- docile (~> 1.1.0)
54
- json (~> 1.8)
55
- simplecov-html (~> 0.10.0)
56
- simplecov-html (0.10.0)
57
- thread_safe (0.3.5)
58
- tzinfo (1.2.2)
59
- thread_safe (~> 0.1)
60
-
61
- PLATFORMS
62
- ruby
63
-
64
- DEPENDENCIES
65
- codeclimate-test-reporter
66
- dotenv
67
- elasticband!
68
- rake
69
- rspec (~> 3.3.0)
70
- rubocop
71
-
72
- BUNDLED WITH
73
- 1.10.6