chewy 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.
Files changed (58) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +17 -0
  3. data/.rspec +3 -0
  4. data/.rvmrc +1 -0
  5. data/.travis.yml +7 -0
  6. data/Gemfile +12 -0
  7. data/Guardfile +24 -0
  8. data/LICENSE.txt +22 -0
  9. data/README.md +208 -0
  10. data/Rakefile +6 -0
  11. data/chewy.gemspec +32 -0
  12. data/lib/chewy.rb +55 -0
  13. data/lib/chewy/config.rb +48 -0
  14. data/lib/chewy/fields/base.rb +49 -0
  15. data/lib/chewy/fields/default.rb +10 -0
  16. data/lib/chewy/fields/root.rb +10 -0
  17. data/lib/chewy/index.rb +71 -0
  18. data/lib/chewy/index/actions.rb +43 -0
  19. data/lib/chewy/index/client.rb +13 -0
  20. data/lib/chewy/index/search.rb +26 -0
  21. data/lib/chewy/query.rb +141 -0
  22. data/lib/chewy/query/criteria.rb +81 -0
  23. data/lib/chewy/query/loading.rb +27 -0
  24. data/lib/chewy/query/pagination.rb +39 -0
  25. data/lib/chewy/rspec.rb +1 -0
  26. data/lib/chewy/rspec/update_index.rb +121 -0
  27. data/lib/chewy/type.rb +22 -0
  28. data/lib/chewy/type/adapter/active_record.rb +27 -0
  29. data/lib/chewy/type/adapter/object.rb +22 -0
  30. data/lib/chewy/type/base.rb +41 -0
  31. data/lib/chewy/type/import.rb +67 -0
  32. data/lib/chewy/type/mapping.rb +50 -0
  33. data/lib/chewy/type/observe.rb +37 -0
  34. data/lib/chewy/type/wrapper.rb +35 -0
  35. data/lib/chewy/version.rb +3 -0
  36. data/spec/chewy/config_spec.rb +50 -0
  37. data/spec/chewy/fields/base_spec.rb +70 -0
  38. data/spec/chewy/fields/default_spec.rb +6 -0
  39. data/spec/chewy/fields/root_spec.rb +6 -0
  40. data/spec/chewy/index/actions_spec.rb +53 -0
  41. data/spec/chewy/index/client_spec.rb +18 -0
  42. data/spec/chewy/index/search_spec.rb +54 -0
  43. data/spec/chewy/index_spec.rb +65 -0
  44. data/spec/chewy/query/criteria_spec.rb +73 -0
  45. data/spec/chewy/query/loading_spec.rb +37 -0
  46. data/spec/chewy/query/pagination_spec.rb +40 -0
  47. data/spec/chewy/query_spec.rb +110 -0
  48. data/spec/chewy/rspec/update_index_spec.rb +149 -0
  49. data/spec/chewy/type/import_spec.rb +68 -0
  50. data/spec/chewy/type/mapping_spec.rb +54 -0
  51. data/spec/chewy/type/observe_spec.rb +55 -0
  52. data/spec/chewy/type/wrapper_spec.rb +35 -0
  53. data/spec/chewy/type_spec.rb +43 -0
  54. data/spec/chewy_spec.rb +36 -0
  55. data/spec/spec_helper.rb +48 -0
  56. data/spec/support/class_helpers.rb +16 -0
  57. data/spec/support/fail_helpers.rb +13 -0
  58. metadata +249 -0
@@ -0,0 +1,55 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Type::Import do
4
+ include ClassHelpers
5
+ before { Chewy.stub(observing_enabled: true) }
6
+
7
+ describe '.update_index' do
8
+ before do
9
+ stub_index(:dummies) do
10
+ define_type :dummy
11
+ end
12
+ end
13
+
14
+ let(:backreferenced) { 3.times.map { |i| double(id: i) } }
15
+
16
+ specify { expect { DummiesIndex.dummy.update_index(backreferenced) }
17
+ .to update_index(DummiesIndex.dummy).and_reindex(backreferenced) }
18
+ specify { expect { DummiesIndex.dummy.update_index([]) }
19
+ .not_to update_index(DummiesIndex.dummy) }
20
+ specify { expect { DummiesIndex.dummy.update_index(nil) }
21
+ .not_to update_index(DummiesIndex.dummy) }
22
+ end
23
+
24
+ context 'integration' do
25
+ before do
26
+ stub_model(:city) do
27
+ belongs_to :country
28
+ update_elasticsearch('cities#city') { self }
29
+ update_elasticsearch('countries#country') { country }
30
+ end
31
+
32
+ stub_model(:country) do
33
+ has_many :cities
34
+ update_elasticsearch('cities#city') { cities }
35
+ update_elasticsearch('countries#country') { self }
36
+ end
37
+
38
+ stub_index(:cities) do
39
+ define_type City
40
+ end
41
+
42
+ stub_index(:countries) do
43
+ define_type Country
44
+ end
45
+ end
46
+
47
+ let(:city) { City.create!(country: Country.create!) }
48
+ let(:country) { Country.create!(cities: 2.times.map { City.create! }) }
49
+
50
+ specify { expect { city.save! }.to update_index(CitiesIndex.city).and_reindex(city) }
51
+ specify { expect { city.save! }.to update_index(CountriesIndex.country).and_reindex(city.country) }
52
+ specify { expect { country.save! }.to update_index(CitiesIndex.city).and_reindex(country.cities) }
53
+ specify { expect { country.save! }.to update_index(CountriesIndex.country).and_reindex(country) }
54
+ end
55
+ end
@@ -0,0 +1,35 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Type::Wrapper do
4
+ include ClassHelpers
5
+
6
+ before do
7
+ stub_model(:city)
8
+ stub_index(:cities) do
9
+ define_type City
10
+ end
11
+ end
12
+
13
+ let(:city_type) { CitiesIndex::City }
14
+
15
+ subject { city_type.new(name: 'Martin', age: 42) }
16
+
17
+ it { should respond_to :name }
18
+ it { should respond_to :age }
19
+ its(:name) { should == 'Martin' }
20
+ its(:age) { should == 42 }
21
+
22
+ describe '#==' do
23
+ specify { city_type.new(id: 42).should == city_type.new(id: 42) }
24
+ specify { city_type.new(id: 42, age: 55).should == city_type.new(id: 42, age: 54) }
25
+ specify { city_type.new(id: 42).should_not == city_type.new(id: 43) }
26
+ specify { city_type.new(id: 42, age: 55).should_not == city_type.new(id: 43, age: 55) }
27
+ specify { city_type.new(age: 55).should == city_type.new(age: 55) }
28
+ specify { city_type.new(age: 55).should_not == city_type.new(age: 54) }
29
+
30
+ specify { city_type.new(id: '42').should == City.new.tap { |m| m.stub(id: 42) } }
31
+ specify { city_type.new(id: 42).should_not == City.new.tap { |m| m.stub(id: 43) } }
32
+
33
+ specify { city_type.new(id: 42).should_not == Class.new }
34
+ end
35
+ end
@@ -0,0 +1,43 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy::Type do
4
+ include ClassHelpers
5
+
6
+ describe '.new' do
7
+ before do
8
+ stub_index(:cities)
9
+ end
10
+
11
+ context 'Symbol' do
12
+ subject { described_class.new(CitiesIndex, :city) }
13
+
14
+ it { should be_a Class }
15
+ it { should be < Chewy::Type::Base }
16
+ its(:name) { should == 'CitiesIndex::City' }
17
+ its(:index) { should == CitiesIndex }
18
+ its(:type_name) { should == 'city' }
19
+ end
20
+
21
+ context 'ActiveRecord model' do
22
+ before { stub_model(:city) }
23
+ subject { described_class.new(CitiesIndex, City) }
24
+
25
+ it { should be_a Class }
26
+ it { should be < Chewy::Type::Base }
27
+ its(:name) { should == 'CitiesIndex::City' }
28
+ its(:index) { should == CitiesIndex }
29
+ its(:type_name) { should == 'city' }
30
+ end
31
+
32
+ context 'ActiveRecord scope' do
33
+ before { stub_model(:city) }
34
+ subject { described_class.new(CitiesIndex, City.includes(:country)) }
35
+
36
+ it { should be_a Class }
37
+ it { should be < Chewy::Type::Base }
38
+ its(:name) { should == 'CitiesIndex::City' }
39
+ its(:index) { should == CitiesIndex }
40
+ its(:type_name) { should == 'city' }
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ describe Chewy do
4
+ include ClassHelpers
5
+
6
+ it 'should have a version number' do
7
+ expect(Chewy::VERSION).not_to be nil
8
+ end
9
+
10
+ describe '.derive_type' do
11
+ before do
12
+ stub_const('SomeIndex', Class.new)
13
+
14
+ stub_index(:developers) do
15
+ define_type :developer
16
+ end
17
+
18
+ stub_index('namespace/autocomplete') do
19
+ define_type :developer
20
+ define_type :company
21
+ end
22
+ end
23
+
24
+ specify { expect { described_class.derive_type('developers_index#developers') }.to raise_error Chewy::UnderivableType, /DevelopersIndexIndex/ }
25
+ specify { expect { described_class.derive_type('some#developers') }.to raise_error Chewy::UnderivableType, /SomeIndex/ }
26
+ specify { expect { described_class.derive_type('borogoves#developers') }.to raise_error Chewy::UnderivableType, /Borogoves/ }
27
+ specify { expect { described_class.derive_type('developers#borogoves') }.to raise_error Chewy::UnderivableType, /DevelopersIndex.*borogoves/ }
28
+ specify { expect { described_class.derive_type('namespace/autocomplete') }.to raise_error Chewy::UnderivableType, /AutocompleteIndex.*namespace\/autocomplete#type_name/ }
29
+
30
+ specify { described_class.derive_type(DevelopersIndex.developer).should == DevelopersIndex.developer }
31
+ specify { described_class.derive_type('developers').should == DevelopersIndex.developer }
32
+ specify { described_class.derive_type('developers#developer').should == DevelopersIndex.developer }
33
+ specify { described_class.derive_type('namespace/autocomplete#developer').should == Namespace::AutocompleteIndex.developer }
34
+ specify { described_class.derive_type('namespace/autocomplete#company').should == Namespace::AutocompleteIndex.company }
35
+ end
36
+ end
@@ -0,0 +1,48 @@
1
+ require 'bundler'
2
+ Bundler.require
3
+
4
+ require 'active_record'
5
+ require 'database_cleaner'
6
+ require 'support/fail_helpers'
7
+ require 'support/class_helpers'
8
+
9
+ require 'chewy/rspec'
10
+
11
+ ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
12
+
13
+ ActiveRecord::Schema.define do
14
+ create_table :countries do |t|
15
+ t.column :name, :string
16
+ t.column :rating, :integer
17
+ end
18
+
19
+ create_table :cities do |t|
20
+ t.column :country_id, :integer
21
+ t.column :name, :string
22
+ t.column :rating, :integer
23
+ end
24
+ end
25
+
26
+ Kaminari::Hooks.init
27
+
28
+ Chewy.client_options = { port: 9250 }
29
+
30
+ RSpec.configure do |config|
31
+ config.mock_with :rspec
32
+
33
+ config.before(:suite) do
34
+ DatabaseCleaner.clean_with :truncation
35
+ DatabaseCleaner.strategy = :transaction
36
+ end
37
+
38
+ config.before do
39
+ DatabaseCleaner.start
40
+ end
41
+
42
+ config.after do
43
+ DatabaseCleaner.clean
44
+ end
45
+
46
+ config.include FailHelpers
47
+ config.include ClassHelpers
48
+ end
@@ -0,0 +1,16 @@
1
+ module ClassHelpers
2
+ extend ActiveSupport::Concern
3
+
4
+ def stub_model name, superclass = nil, &block
5
+ stub_class(name, superclass || ActiveRecord::Base, &block)
6
+ end
7
+
8
+ def stub_index name, superclass = nil, &block
9
+ stub_class("#{name.to_s.camelize}Index", superclass || Chewy::Index) { index_name = name }
10
+ .tap { |i| i.class_eval(&block) if block }
11
+ end
12
+
13
+ def stub_class name, superclass, &block
14
+ stub_const(name.to_s.camelize, Class.new(superclass, &block))
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ module FailHelpers
2
+ def fail
3
+ raise_error(RSpec::Expectations::ExpectationNotMetError)
4
+ end
5
+
6
+ def fail_with(message)
7
+ raise_error(RSpec::Expectations::ExpectationNotMetError, message)
8
+ end
9
+
10
+ def fail_matching(message)
11
+ raise_error(RSpec::Expectations::ExpectationNotMetError, /#{Regexp.escape(message)}/)
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,249 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chewy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - pyromaniac
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-12-11 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
70
+ name: kaminari
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: activerecord
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '3.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '3.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: database_cleaner
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - '>='
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - '>='
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activesupport
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '3.2'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - '>='
123
+ - !ruby/object:Gem::Version
124
+ version: '3.2'
125
+ - !ruby/object:Gem::Dependency
126
+ name: elasticsearch
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - '>='
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - '>='
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Chewy provides functionality for Elasticsearch index handling, documents
140
+ import mappings and chainable query DSL
141
+ email:
142
+ - kinwizard@gmail.com
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - .gitignore
148
+ - .rspec
149
+ - .rvmrc
150
+ - .travis.yml
151
+ - Gemfile
152
+ - Guardfile
153
+ - LICENSE.txt
154
+ - README.md
155
+ - Rakefile
156
+ - chewy.gemspec
157
+ - lib/chewy.rb
158
+ - lib/chewy/config.rb
159
+ - lib/chewy/fields/base.rb
160
+ - lib/chewy/fields/default.rb
161
+ - lib/chewy/fields/root.rb
162
+ - lib/chewy/index.rb
163
+ - lib/chewy/index/actions.rb
164
+ - lib/chewy/index/client.rb
165
+ - lib/chewy/index/search.rb
166
+ - lib/chewy/query.rb
167
+ - lib/chewy/query/criteria.rb
168
+ - lib/chewy/query/loading.rb
169
+ - lib/chewy/query/pagination.rb
170
+ - lib/chewy/rspec.rb
171
+ - lib/chewy/rspec/update_index.rb
172
+ - lib/chewy/type.rb
173
+ - lib/chewy/type/adapter/active_record.rb
174
+ - lib/chewy/type/adapter/object.rb
175
+ - lib/chewy/type/base.rb
176
+ - lib/chewy/type/import.rb
177
+ - lib/chewy/type/mapping.rb
178
+ - lib/chewy/type/observe.rb
179
+ - lib/chewy/type/wrapper.rb
180
+ - lib/chewy/version.rb
181
+ - spec/chewy/config_spec.rb
182
+ - spec/chewy/fields/base_spec.rb
183
+ - spec/chewy/fields/default_spec.rb
184
+ - spec/chewy/fields/root_spec.rb
185
+ - spec/chewy/index/actions_spec.rb
186
+ - spec/chewy/index/client_spec.rb
187
+ - spec/chewy/index/search_spec.rb
188
+ - spec/chewy/index_spec.rb
189
+ - spec/chewy/query/criteria_spec.rb
190
+ - spec/chewy/query/loading_spec.rb
191
+ - spec/chewy/query/pagination_spec.rb
192
+ - spec/chewy/query_spec.rb
193
+ - spec/chewy/rspec/update_index_spec.rb
194
+ - spec/chewy/type/import_spec.rb
195
+ - spec/chewy/type/mapping_spec.rb
196
+ - spec/chewy/type/observe_spec.rb
197
+ - spec/chewy/type/wrapper_spec.rb
198
+ - spec/chewy/type_spec.rb
199
+ - spec/chewy_spec.rb
200
+ - spec/spec_helper.rb
201
+ - spec/support/class_helpers.rb
202
+ - spec/support/fail_helpers.rb
203
+ homepage: ''
204
+ licenses:
205
+ - MIT
206
+ metadata: {}
207
+ post_install_message:
208
+ rdoc_options: []
209
+ require_paths:
210
+ - lib
211
+ required_ruby_version: !ruby/object:Gem::Requirement
212
+ requirements:
213
+ - - '>='
214
+ - !ruby/object:Gem::Version
215
+ version: '0'
216
+ required_rubygems_version: !ruby/object:Gem::Requirement
217
+ requirements:
218
+ - - '>='
219
+ - !ruby/object:Gem::Version
220
+ version: '0'
221
+ requirements: []
222
+ rubyforge_project:
223
+ rubygems_version: 2.1.11
224
+ signing_key:
225
+ specification_version: 4
226
+ summary: Elasticsearch ODM client wrapper
227
+ test_files:
228
+ - spec/chewy/config_spec.rb
229
+ - spec/chewy/fields/base_spec.rb
230
+ - spec/chewy/fields/default_spec.rb
231
+ - spec/chewy/fields/root_spec.rb
232
+ - spec/chewy/index/actions_spec.rb
233
+ - spec/chewy/index/client_spec.rb
234
+ - spec/chewy/index/search_spec.rb
235
+ - spec/chewy/index_spec.rb
236
+ - spec/chewy/query/criteria_spec.rb
237
+ - spec/chewy/query/loading_spec.rb
238
+ - spec/chewy/query/pagination_spec.rb
239
+ - spec/chewy/query_spec.rb
240
+ - spec/chewy/rspec/update_index_spec.rb
241
+ - spec/chewy/type/import_spec.rb
242
+ - spec/chewy/type/mapping_spec.rb
243
+ - spec/chewy/type/observe_spec.rb
244
+ - spec/chewy/type/wrapper_spec.rb
245
+ - spec/chewy/type_spec.rb
246
+ - spec/chewy_spec.rb
247
+ - spec/spec_helper.rb
248
+ - spec/support/class_helpers.rb
249
+ - spec/support/fail_helpers.rb