nazrin 2.1.2 → 2.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 4811b450a57c8fcc72c815cc642d9e0ce7359dfa
4
- data.tar.gz: 910cca00b2effa6bfe5d3884b39cfbf560515355
3
+ metadata.gz: b4f029ada212e4e84f9330f78ed97b3b06442d17
4
+ data.tar.gz: 7928db6029d307c725cbbd20c8483e3387f3b0cb
5
5
  SHA512:
6
- metadata.gz: 4c052105d5458141a09041a0ac2baaf266e5a8b1270c2d1a8e1843a276dafad1cd0a28c1d5b46595f2b4b9cb2f4b8d72894529b2b93ebf606dc12387cc4d0a94
7
- data.tar.gz: d40a60de6db1046c563a019e5d474f7d96c5b69478285b2c365269dd80f56a6dccd4bcaf15f2d97671d0913a0b0d6ba899f24e2c69b906ab79362901806983d9
6
+ metadata.gz: 069789e5426be62c5967a54cca18cb05e1a9f5c1a3c0a833e4919b2984004263b07d8edcc3b247cd40cd4a6ed6d8f55f4661fdfd40cd0db34cf4079eea8ccdd8
7
+ data.tar.gz: f3b4e13c3f1260e1a6b0af4d237f354d9a020771f9c37e684ca87496d6d2da21504368d91b24fe2810a35526cb87da987c5e41dce71abfdd96ededf05d7ab2a3
@@ -1,4 +1,6 @@
1
1
  language: ruby
2
+ services:
3
+ - mongodb
2
4
  rvm:
3
5
  - 2.2.3
4
6
  before_install: gem install bundler -v 1.10.3
@@ -1,3 +1,19 @@
1
+ ## [2.2.0](https://github.com/tsuwatch/nazrin/compare/v2.1.2...v2.2.0)
2
+
3
+ ### Breaking changes:
4
+
5
+ * Include `Nazrin::Searchable` instead of `Nazrin::ActiveRecord::Searchable` [#19](https://github.com/tsuwatch/nazrin/pull/19)
6
+
7
+ ### Features:
8
+
9
+ * Support Mongoid [#17](https://github.com/tsuwatch/nazrin/pull/17) [#20](https://github.com/tsuwatch/nazrin/pull/20) - [@yefim](https://github.com/yefim)
10
+
11
+ ## [2.1.2](https://github.com/tsuwatch/nazrin/compare/v2.1.1...v2.1.2)
12
+
13
+ ### Fixes:
14
+
15
+ * Require aws-sdk gem in nazrin [#14](https://github.com/tsuwatch/nazrin/pull/14)
16
+
1
17
  ## [2.1.1](https://github.com/tsuwatch/nazrin/compare/v2.1.0...v2.1.1)
2
18
 
3
19
  ### Fixes:
data/README.md CHANGED
@@ -4,7 +4,7 @@
4
4
  [![Coverage Status](https://coveralls.io/repos/tsuwatch/nazrin/badge.svg?branch=readme&service=github)](https://coveralls.io/github/tsuwatch/nazrin?branch=readme)
5
5
  [![Code Climate](https://codeclimate.com/github/tsuwatch/nazrin/badges/gpa.svg)](https://codeclimate.com/github/tsuwatch/nazrin)
6
6
 
7
- Nazrin is a Ruby wrapper for Amazon CloudSearch (aws-sdk), with optional ActiveRecord support for easy integration with your Rails application.
7
+ Nazrin is a Ruby wrapper for Amazon CloudSearch (aws-sdk), with ActiveRecord, Mongoid support for easy integration with your Rails application.
8
8
 
9
9
  >*Nazrin has the ability of the extent which find what you're looking for...*
10
10
 
@@ -44,7 +44,7 @@ end
44
44
 
45
45
  ```ruby
46
46
  class Post < ActiveRecord::Base
47
- include Nazrin::ActiveRecord::Searchable
47
+ include Nazrin::Searchable
48
48
 
49
49
  searchable do
50
50
  fields [:content]
@@ -66,6 +66,7 @@ Post.search(where: :foo, includes: :bar).size(1).start(0).query("(and 'content')
66
66
  If you want to use other supported pagination libraries, for example, `nazrin-kaminari` generates `Kaminari::PaginatableArray` instead of `Nazrin::PaginatedArray`.
67
67
 
68
68
  ```ruby
69
+ gem 'nazrin'
69
70
  gem 'nazrin-kaminari'
70
71
  ```
71
72
 
@@ -1,7 +1,7 @@
1
1
  require 'aws-sdk'
2
2
 
3
- require 'nazrin/active_record/searchable'
4
- require 'nazrin/active_record/data_accessor'
3
+ require 'nazrin/searchable'
4
+ require 'nazrin/data_accessor'
5
5
  require 'nazrin/config'
6
6
  require 'nazrin/search_client'
7
7
  require 'nazrin/document_client'
@@ -0,0 +1,87 @@
1
+ module Nazrin
2
+ class DataAccessor
3
+ class NoAccessorError < StandardError; end
4
+
5
+ class << self
6
+ def for(clazz)
7
+ accessor = registered_accessor_for(clazz) || register_accessor(clazz)
8
+ return accessor if accessor
9
+ raise NoAccessorError, "No accessor for #{clazz.name}"
10
+ end
11
+
12
+ def registered_accessor_for(clazz)
13
+ return nil if clazz.name.nil? || clazz.name.empty?
14
+ accessors[clazz.name.to_sym]
15
+ end
16
+
17
+ def register_accessor(clazz)
18
+ clazz.ancestors.each do |ancestor_class|
19
+ if accessor = accessor_for(ancestor_class)
20
+ register(accessor, clazz)
21
+ return accessor
22
+ end
23
+ end
24
+ nil
25
+ end
26
+
27
+ def accessor_for(clazz)
28
+ return nil if clazz.name.nil? || clazz.name.empty?
29
+ if defined?(::ActiveRecord::Base) && clazz.ancestors.include?(::ActiveRecord::Base)
30
+ require 'nazrin/data_accessor/active_record'
31
+ return Nazrin::DataAccessor::ActiveRecord
32
+ elsif defined?(::Mongoid::Document) && clazz.ancestors.include?(::Mongoid::Document)
33
+ require 'nazrin/data_accessor/mongoid'
34
+ return Nazrin::DataAccessor::Mongoid
35
+ end
36
+ nil
37
+ end
38
+
39
+ def register(accessor, clazz)
40
+ accessors[clazz.name.to_sym] = accessor
41
+ end
42
+
43
+ def accessors
44
+ @accessor ||= {}
45
+ end
46
+ end
47
+
48
+ def initialize(model, options)
49
+ @model = model
50
+ @options = options
51
+ end
52
+
53
+ def results(client)
54
+ @client = client
55
+
56
+ res = @client.search
57
+ collection = load_all(res.data.hits.hit.map(&:id))
58
+
59
+ if @client.parameters[:size] && @client.parameters[:start]
60
+ total_count = res.data.hits.found
61
+
62
+ Nazrin::PaginationGenerator.generate(
63
+ collection,
64
+ current_page: current_page,
65
+ per_page: @client.parameters[:size],
66
+ total_count: total_count,
67
+ last_page: last_page(total_count))
68
+ else
69
+ collection
70
+ end
71
+ end
72
+
73
+ def load_all
74
+ raise NotImplementedError
75
+ end
76
+
77
+ private
78
+
79
+ def last_page(total_count)
80
+ (total_count / @client.parameters[:size].to_f).ceil
81
+ end
82
+
83
+ def current_page
84
+ (@client.parameters[:start] / @client.parameters[:size].to_f).ceil + 1
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,19 @@
1
+ module Nazrin
2
+ class DataAccessor
3
+ class ActiveRecord < Nazrin::DataAccessor
4
+ # load from activerecord
5
+ def load_all(ids)
6
+ records_table = {}
7
+ @options.each do |k, v|
8
+ @model = @model.send(k, v)
9
+ end
10
+ @model.where(id: ids).each do |record|
11
+ records_table[record.id] = record
12
+ end
13
+ ids.map do |id|
14
+ records_table.select { |k, _| k == id.to_i }[id.to_i]
15
+ end.reject(&:nil?)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module Nazrin
2
+ class DataAccessor
3
+ class Mongoid < Nazrin::DataAccessor
4
+ def load_all(ids)
5
+ documents_table = {}
6
+ @options.each do |k, v|
7
+ @model = if v.nil?
8
+ @model.send(k)
9
+ else
10
+ @model.send(k, v)
11
+ end
12
+ end
13
+ @model.where('_id' => { '$in' => ids }).each do |document|
14
+ documents_table[document._id.to_s] = document
15
+ end
16
+ ids.map do |id|
17
+ documents_table[id]
18
+ end.reject(&:nil?)
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,92 @@
1
+ require 'active_support/concern'
2
+
3
+ module Nazrin
4
+ module Searchable
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ alias_method :delete_from_index, :nazrin_delete_from_index unless method_defined? :delete_from_index
9
+ alias_method :add_to_index, :nazrin_add_to_index unless method_defined? :add_to_index
10
+ alias_method :update_in_index, :nazrin_update_in_index unless method_defined? :update_in_index
11
+ end
12
+
13
+ def nazrin_delete_from_index
14
+ self.class.nazrin_delete_document(self)
15
+ end
16
+
17
+ def nazrin_add_to_index
18
+ self.class.nazrin_add_document(self)
19
+ end
20
+
21
+ def nazrin_update_in_index
22
+ self.class.nazrin_update_document(self)
23
+ end
24
+
25
+ module ClassMethods
26
+ def self.extended(base)
27
+ class << base
28
+ alias_method :search, :nazrin_search unless method_defined? :search
29
+ alias_method :searchable, :nazrin_searchable unless method_defined? :searchable
30
+ alias_method :fields, :nazrin_fields unless method_defined? :fields
31
+ alias_method :field, :nazrin_field unless method_defined? :field
32
+ end
33
+ end
34
+
35
+ def nazrin_search(options = {})
36
+ client = Nazrin::SearchClient.new
37
+ client.data_accessor = Nazrin::DataAccessor.for(self).new(self, options)
38
+ client
39
+ end
40
+
41
+ def nazrin_searchable(&block)
42
+ class_variable_set(
43
+ :@@nazrin_doc_client,
44
+ Nazrin::DocumentClient.new)
45
+ class_variable_set(:@@nazrin_search_field_data, {})
46
+ block.call
47
+ end
48
+
49
+ def nazrin_fields(fields)
50
+ field_data = class_variable_get(:@@nazrin_search_field_data)
51
+ fields.each do |field|
52
+ field_data[field] = proc { public_send(field) }
53
+ end
54
+ class_variable_set(:@@nazrin_search_field_data, field_data)
55
+ end
56
+
57
+ def nazrin_field(field, &block)
58
+ field_data = class_variable_get(:@@nazrin_search_field_data)
59
+ field_data[field] = block
60
+ class_variable_set(:@@nazrin_search_field_data, field_data)
61
+ end
62
+
63
+ def nazrin_doc_client
64
+ class_variable_get(:@@nazrin_doc_client)
65
+ end
66
+
67
+ def nazrin_eval_field_data(obj)
68
+ data = {}
69
+ class_variable_get(
70
+ :@@nazrin_search_field_data).each do |field, block|
71
+ data[field] = obj.instance_eval(&block)
72
+ data[field] = data[field].remove(
73
+ /[[:cntrl:]]/) if data[field].is_a?(String)
74
+ end
75
+ data
76
+ end
77
+
78
+ def nazrin_add_document(obj)
79
+ nazrin_doc_client.add_document(
80
+ obj.send(:id), nazrin_eval_field_data(obj))
81
+ end
82
+
83
+ def nazrin_update_document(obj)
84
+ nazrin_add_document(obj)
85
+ end
86
+
87
+ def nazrin_delete_document(obj)
88
+ nazrin_doc_client.delete_document(obj.send(:id))
89
+ end
90
+ end
91
+ end
92
+ end
@@ -1,3 +1,3 @@
1
1
  module Nazrin
2
- VERSION = '2.1.2'
2
+ VERSION = '2.2.0'
3
3
  end
@@ -28,4 +28,5 @@ Gem::Specification.new do |spec|
28
28
  spec.add_development_dependency 'sqlite3'
29
29
  spec.add_development_dependency 'activerecord'
30
30
  spec.add_development_dependency 'database_cleaner'
31
+ spec.add_development_dependency 'mongoid'
31
32
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nazrin
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.1.2
4
+ version: 2.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Tomohiro Suwa
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-04-17 00:00:00.000000000 Z
11
+ date: 2017-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk
@@ -150,6 +150,20 @@ dependencies:
150
150
  - - ">="
151
151
  - !ruby/object:Gem::Version
152
152
  version: '0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: mongoid
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - ">="
158
+ - !ruby/object:Gem::Version
159
+ version: '0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ version: '0'
153
167
  description: Ruby wrapper for Amazon CloudSearch (aws-sdk), with optional ActiveRecord
154
168
  support for easy integration with your Rails application
155
169
  email:
@@ -170,13 +184,15 @@ files:
170
184
  - lib/generators/nazrin/config_generator.rb
171
185
  - lib/generators/nazrin/templates/nazrin_config.rb
172
186
  - lib/nazrin.rb
173
- - lib/nazrin/active_record/data_accessor.rb
174
- - lib/nazrin/active_record/searchable.rb
175
187
  - lib/nazrin/config.rb
188
+ - lib/nazrin/data_accessor.rb
189
+ - lib/nazrin/data_accessor/active_record.rb
190
+ - lib/nazrin/data_accessor/mongoid.rb
176
191
  - lib/nazrin/document_client.rb
177
192
  - lib/nazrin/paginated_array.rb
178
193
  - lib/nazrin/pagination_generator.rb
179
194
  - lib/nazrin/search_client.rb
195
+ - lib/nazrin/searchable.rb
180
196
  - lib/nazrin/version.rb
181
197
  - nazrin.gemspec
182
198
  homepage: https://github.com/tsuwatch/nazrin
@@ -1,55 +0,0 @@
1
- module Nazrin
2
- module ActiveRecord
3
- class DataAccessor
4
- def initialize(model, options)
5
- @model = model
6
- @options = options
7
- end
8
-
9
- # a list of all matching AR model objects
10
- def results(client)
11
- @client = client
12
-
13
- res = @client.search
14
- collection = load_all(res.data.hits.hit.map(&:id))
15
-
16
- if @client.parameters[:size] && @client.parameters[:start]
17
- total_count = res.data.hits.found
18
-
19
- Nazrin::PaginationGenerator.generate(
20
- collection,
21
- current_page: current_page,
22
- per_page: @client.parameters[:size],
23
- total_count: total_count,
24
- last_page: last_page(total_count))
25
- else
26
- collection
27
- end
28
- end
29
-
30
- # load from activerecord
31
- def load_all(ids)
32
- records_table = {}
33
- @options.each do |k, v|
34
- @model = @model.send(k, v)
35
- end
36
- @model.where(id: ids).each do |record|
37
- records_table[record.id] = record
38
- end
39
- ids.map do |id|
40
- records_table.select { |k, _| k == id.to_i }[id.to_i]
41
- end.reject(&:nil?)
42
- end
43
-
44
- private
45
-
46
- def last_page(total_count)
47
- (total_count / @client.parameters[:size].to_f).ceil
48
- end
49
-
50
- def current_page
51
- (@client.parameters[:start] / @client.parameters[:size].to_f).ceil + 1
52
- end
53
- end
54
- end
55
- end
@@ -1,95 +0,0 @@
1
- require 'active_support/concern'
2
-
3
- module Nazrin
4
- module ActiveRecord
5
- module Searchable
6
- extend ActiveSupport::Concern
7
-
8
- included do
9
- alias_method :delete_from_index, :nazrin_delete_from_index unless method_defined? :delete_from_index
10
- alias_method :add_to_index, :nazrin_add_to_index unless method_defined? :add_to_index
11
- alias_method :update_in_index, :nazrin_update_in_index unless method_defined? :update_in_index
12
- end
13
-
14
- def nazrin_delete_from_index
15
- self.class.nazrin_delete_document(self)
16
- end
17
-
18
- def nazrin_add_to_index
19
- self.class.nazrin_add_document(self)
20
- end
21
-
22
- def nazrin_update_in_index
23
- self.class.nazrin_update_document(self)
24
- end
25
-
26
- module ClassMethods
27
- def self.extended(base)
28
- class << base
29
- alias_method :search, :nazrin_search unless method_defined? :search
30
- alias_method :searchable, :nazrin_searchable unless method_defined? :searchable
31
- alias_method :fields, :nazrin_fields unless method_defined? :fields
32
- alias_method :field, :nazrin_field unless method_defined? :field
33
- end
34
- end
35
-
36
- def nazrin_search(options = {})
37
- client = Nazrin::SearchClient.new
38
- client.data_accessor = Nazrin::ActiveRecord::DataAccessor.new(
39
- self, options)
40
- client
41
- end
42
-
43
- def nazrin_searchable(&block)
44
- class_variable_set(
45
- :@@nazrin_doc_client,
46
- Nazrin::DocumentClient.new)
47
- class_variable_set(:@@nazrin_search_field_data, {})
48
- block.call
49
- end
50
-
51
- def nazrin_fields(fields)
52
- field_data = class_variable_get(:@@nazrin_search_field_data)
53
- fields.each do |field|
54
- field_data[field] = proc { public_send(field) }
55
- end
56
- class_variable_set(:@@nazrin_search_field_data, field_data)
57
- end
58
-
59
- def nazrin_field(field, &block)
60
- field_data = class_variable_get(:@@nazrin_search_field_data)
61
- field_data[field] = block
62
- class_variable_set(:@@nazrin_search_field_data, field_data)
63
- end
64
-
65
- def nazrin_doc_client
66
- class_variable_get(:@@nazrin_doc_client)
67
- end
68
-
69
- def nazrin_eval_field_data(obj)
70
- data = {}
71
- class_variable_get(
72
- :@@nazrin_search_field_data).each do |field, block|
73
- data[field] = obj.instance_eval(&block)
74
- data[field] = data[field].remove(
75
- /[[:cntrl:]]/) if data[field].is_a?(String)
76
- end
77
- data
78
- end
79
-
80
- def nazrin_add_document(obj)
81
- nazrin_doc_client.add_document(
82
- obj.send(:id), nazrin_eval_field_data(obj))
83
- end
84
-
85
- def nazrin_update_document(obj)
86
- nazrin_add_document(obj)
87
- end
88
-
89
- def nazrin_delete_document(obj)
90
- nazrin_doc_client.delete_document(obj.send(:id))
91
- end
92
- end
93
- end
94
- end
95
- end