elastify 0.2.3 → 0.2.4

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,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9390c61d979851afb2b3ad0fbbdfc7f101ee60e7
4
- data.tar.gz: f7cb608d84f1f8be941b3f31ede57a22f354e628
3
+ metadata.gz: fa548bd07b0b3431f9fe21c4e1bec9dbfd0105ca
4
+ data.tar.gz: 8480f5cd58f69422dae23ca1255b143743c36d4a
5
5
  SHA512:
6
- metadata.gz: 2fce9c0bd9ff7830fa362ade0403963580a5428c2bc6a964178fa01756c84b995112df8cc0adc99aee56378c02af9aaf335ea1add2188a2bb739492a1a531aeb
7
- data.tar.gz: 752e3f93e04c8624eec5ef23f73af6fa2c06e1485e6de0988ca53bdd027bd3b3522cfa88c873153d826087998f44be28fbe00c3385c783937449c8ae53765561
6
+ metadata.gz: 63a937116d4d8109b629638f82e76ea1e1bf560f120f83124b1023d433dc1fd9ceee98c57f2eeb6e5cb556c58e710856bab31a0c106a2cff2d827e8068f9eeee
7
+ data.tar.gz: f7cee3663f6c6e7652e2a17b9e453b697d67e0d223d622d187019c1e36a14dbc743871cac7a07131fdfbeb79c1fe211b437630327e20d8eafd96f4f23cca09e5
data/.gitignore CHANGED
File without changes
data/CODE_OF_CONDUCT.md CHANGED
File without changes
data/Gemfile CHANGED
File without changes
data/LICENSE.txt CHANGED
File without changes
data/README.md CHANGED
File without changes
data/Rakefile CHANGED
File without changes
data/bin/console CHANGED
File without changes
data/bin/setup CHANGED
File without changes
data/elastify.gemspec CHANGED
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
7
7
  spec.name = "elastify"
8
8
  spec.version = Elastify::VERSION
9
9
  spec.authors = ["Bruno Bortolotti Ribeiro"]
10
- spec.email = ["b2bortolotti@gmail.com"]
10
+ spec.email = ["brunobortolotti@icloud.com"]
11
11
 
12
12
  spec.summary = %q{A gem to help with ActiveRecord-ElasticSeach integration}
13
13
  spec.description = %q{A gem to help with ActiveRecord-ElasticSeach integration}
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
17
17
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
18
  # to allow pushing to a single host or delete this section to allow pushing to any host.
19
19
  if spec.respond_to?(:metadata)
20
- spec.metadata['allowed_push_host'] = "TODO: Set to 'http://mygemserver.com'"
20
+ spec.metadata['allowed_push_host'] = "https://rubygems.org"
21
21
  else
22
22
  raise "RubyGems 2.0 or newer is required to protect against " \
23
23
  "public gem pushes."
@@ -8,29 +8,46 @@ module Elastify::ActiveRecordExtensions
8
8
  module ClassMethods
9
9
  extend ActiveSupport::Concern
10
10
 
11
- def elastify_setup(&block)
11
+ def elastify_model(model_name)
12
12
  include Elastify::ActiveRecordExtensions::LocalMethods
13
13
  cattr_accessor :elastify_options
14
14
  attr_accessor :elastify_serialized_document
15
15
 
16
- config = Elastify::Configurators::Model.new
17
- yield(config) if block_given?
16
+ model = Elastify.models[model_name.to_sym]
17
+ yield(model) if block_given?
18
18
  self.elastify_options = {} if self.elastify_options.blank?
19
- self.elastify_options[:base_url] = Elastify.configs[:base_url]
20
- self.elastify_options[:index] = config.opt_index if config.opt_index.present?
21
- self.elastify_options[:type] = config.opt_type if config.opt_type.present?
22
- self.elastify_options[:map] = config.opt_mapping if config.opt_mapping.present?
23
- self.elastify_options[:decode] = config.opt_decode if config.opt_decode.present?
24
- self.elastify_options[:encode] = config.opt_encode if config.opt_encode.present?
25
- self.elastify_options[:scroll_timeout] = config.opt_scroll_timeout if config.opt_scroll_timeout.present?
19
+ self.elastify_options[:base_url] = Elastify.configs.base_url
20
+ self.elastify_options[:index] = model.opt_index if model.opt_index.present?
21
+ self.elastify_options[:type] = model.opt_type if model.opt_type.present?
22
+ self.elastify_options[:map] = model.opt_mapping if model.opt_mapping.present?
23
+ self.elastify_options[:decode] = model.opt_decode if model.opt_decode.present?
24
+ self.elastify_options[:encode] = model.opt_encode if model.opt_encode.present?
25
+ self.elastify_options[:scroll_timeout] = model.opt_scroll_timeout if model.opt_scroll_timeout.present?
26
+ end
27
+
28
+ def elastify_setup
29
+ include Elastify::ActiveRecordExtensions::LocalMethods
30
+ cattr_accessor :elastify_options
31
+ attr_accessor :elastify_serialized_document
32
+
33
+ model = Elastify::Model.new
34
+ yield(model) if block_given?
35
+ self.elastify_options = {} if self.elastify_options.blank?
36
+ self.elastify_options[:base_url] = Elastify.configs.base_url
37
+ self.elastify_options[:index] = model.opt_index if model.opt_index.present?
38
+ self.elastify_options[:type] = model.opt_type if model.opt_type.present?
39
+ self.elastify_options[:map] = model.opt_mapping if model.opt_mapping.present?
40
+ self.elastify_options[:decode] = model.opt_decode if model.opt_decode.present?
41
+ self.elastify_options[:encode] = model.opt_encode if model.opt_encode.present?
42
+ self.elastify_options[:scroll_timeout] = model.opt_scroll_timeout if model.opt_scroll_timeout.present?
26
43
  end
27
44
 
28
45
  def elastify_search(dsl: nil, scroll_timeout: nil)
29
- return Elastify::Helpers::ElasticSearch::Document.new(self.elastify_options).search(dsl, scroll_timeout)
46
+ Elastify::Helpers::ElasticSearch::Document.new(self.elastify_options).search(dsl, scroll_timeout)
30
47
  end
31
48
 
32
49
  def elastify_scroll(scroll_id: nil, scroll_timeout: nil)
33
- return Elastify::Helpers::ElasticSearch::Document.new(self.elastify_options).scroll(scroll_id, scroll_timeout)
50
+ Elastify::Helpers::ElasticSearch::Document.new(self.elastify_options).scroll(scroll_id, scroll_timeout)
34
51
  end
35
52
  end
36
53
 
@@ -39,7 +56,7 @@ module Elastify::ActiveRecordExtensions
39
56
 
40
57
  def elastify_create
41
58
  run_callbacks(:elastify_sync) do
42
- if not self.elastify_serialized_document.blank?
59
+ unless self.elastify_serialized_document.blank?
43
60
  run_callbacks(:elastify_create) do
44
61
  Elastify::Helpers::ElasticSearch::Document.new(self.class.elastify_options).create(self.elastify_serialized_document)
45
62
  end
@@ -49,7 +66,7 @@ module Elastify::ActiveRecordExtensions
49
66
 
50
67
  def elastify_update
51
68
  run_callbacks(:elastify_sync) do
52
- if not self.elastify_serialized_document.blank?
69
+ unless self.elastify_serialized_document.blank?
53
70
  run_callbacks(:elastify_update) do
54
71
  Elastify::Helpers::ElasticSearch::Document.new(self.class.elastify_options).update(self.elastify_serialized_document)
55
72
  end
@@ -59,7 +76,7 @@ module Elastify::ActiveRecordExtensions
59
76
 
60
77
  def elastify_destroy
61
78
  run_callbacks(:elastify_sync) do
62
- if not self.elastify_serialized_document.blank?
79
+ unless self.elastify_serialized_document.blank?
63
80
  run_callbacks(:elastify_destroy) do
64
81
  Elastify::Helpers::ElasticSearch::Document.new(self.class.elastify_options).destroy(self.elastify_serialized_document)
65
82
  end
@@ -90,9 +107,9 @@ module Elastify::ActiveRecordExtensions
90
107
  if self.class.elastify_options[:encode].present?
91
108
  encoder = self.class.elastify_options[:encode]
92
109
  elsif self.respond_to?(:to_serial)
93
- encoder = Proc.new { |item| next item.to_serial }
110
+ encoder = Proc.new { |i| next i.to_serial }
94
111
  else
95
- encoder = Proc.new { |item| next item.serializable_hash }
112
+ encoder = Proc.new { |i| next i.serializable_hash }
96
113
  end
97
114
  item.elastify_serialized_document = encoder.call(item)
98
115
  end
@@ -0,0 +1,30 @@
1
+ module Elastify
2
+ class Config
3
+
4
+ attr_accessor :connection
5
+
6
+ def initialize
7
+ @connection = OpenStruct.new({protocol: 'http', host: 'localhost', port: '9200'})
8
+ end
9
+
10
+ def base_url=(uri)
11
+ m1 = /(http|https)\:\/\/([A-z0-9\_\-\.]+)\:([0-9]+)/.match(uri)
12
+ raise ArgumentError, 'Invalid URI' unless m1.present?
13
+ @connection.protocol = m1[1]
14
+ @connection.host = m1[2]
15
+ @connection.port = m1[3]
16
+ end
17
+
18
+ def base_url
19
+ "#{@connection.protocol}://#{@connection.host}:#{@connection.port}"
20
+ end
21
+
22
+
23
+ # OpenStruct.new({
24
+ # connection: ,
25
+ # indices: OpenStruct.new({default: ''}),
26
+ # mappings: OpenStruct.new({autoload: true, autoload_path: Rails.root.join('config/elastify/mappings')}),
27
+ # })
28
+
29
+ end
30
+ end
File without changes
@@ -1,32 +1 @@
1
- module Elastify
2
- module Configurators
3
- class Model
4
1
 
5
- attr_accessor :opt_index, :opt_type, :opt_mapping, :opt_encode, :opt_decode, :opt_scroll_timeout
6
-
7
- def index(index)
8
- @opt_index = index
9
- end
10
-
11
- def type(type)
12
- @opt_type = type
13
- end
14
-
15
- def mapping(&block)
16
- @opt_mapping = block.call() if block_given?
17
- end
18
-
19
- def encode(&block)
20
- @opt_encode = block if block_given?
21
- end
22
-
23
- def decode(&block)
24
- @opt_decode = block if block_given?
25
- end
26
-
27
- def scroll_timeout(scroll_timeout)
28
- @opt_scroll_timeout = scroll_timeout
29
- end
30
- end
31
- end
32
- end
File without changes
File without changes
File without changes
@@ -69,7 +69,6 @@ module Elastify
69
69
 
70
70
  def self.create_mapping(options)
71
71
  url = "#{options[:base_url]}/#{options[:index]}/_mappings/#{options[:type]}"
72
- puts options[:map]
73
72
  JSON.parse(RestClient.put(url, options[:map].squish, {})).to_hash
74
73
  end
75
74
  end
File without changes
File without changes
File without changes
@@ -0,0 +1,34 @@
1
+ module Elastify
2
+ class Model
3
+ attr_accessor :opt_index, :opt_type, :opt_mapping, :opt_encode, :opt_decode, :opt_scroll_timeout
4
+
5
+ def index(index)
6
+ @opt_index = index
7
+ end
8
+
9
+ def type(type)
10
+ @opt_type = type
11
+ end
12
+
13
+ def mapping(&block)
14
+ @opt_mapping = block.call() if block_given?
15
+ end
16
+
17
+ def mapping_file(file_name)
18
+ file_path = file_name.instance_of?(Pathname) ? file_name : Rails.root.join('config', 'elastify', 'mappings', file_name)
19
+ @opt_mapping = JSON.parse(File.read(file_path)).deep_symbolize_keys if File.exist?(file_path)
20
+ end
21
+
22
+ def encode(&block)
23
+ @opt_encode = block if block_given?
24
+ end
25
+
26
+ def decode(&block)
27
+ @opt_decode = block if block_given?
28
+ end
29
+
30
+ def scroll_timeout(scroll_timeout)
31
+ @opt_scroll_timeout = scroll_timeout
32
+ end
33
+ end
34
+ end
@@ -1,3 +1,3 @@
1
1
  module Elastify
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
data/lib/elastify.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'elastify/version'
2
+ require 'elastify/config'
3
+ require 'elastify/model'
2
4
  require 'elastify/errors/base'
3
5
  require 'elastify/errors/bad_request'
4
6
  require 'elastify/errors/connection'
@@ -11,35 +13,42 @@ require 'elastify/helpers/elastic_search/search_result_collection'
11
13
 
12
14
 
13
15
  module Elastify
14
-
16
+
15
17
  class << self
16
- def configure &block
17
- mappings = {}
18
- configs = OpenStruct.new({
19
- base_url: 'http://localhost:9200',
20
- mappings_path: Rails.root.join('config/elastify/mappings')
21
- })
22
- block.call(configs) if block_given?
23
- dir = configs.mappings_path
24
- if Dir.exist?(dir)
25
- Dir.glob("#{dir}/*.json") do |file_path|
26
- mappings[File.basename(file_path, '.json')] = JSON.parse(File.read(file_path))
27
- end
28
- end
29
- Rails.application.config.elastify = {
30
- configs: configs,
31
- mappings: mappings,
32
- }
18
+
19
+ def init(&block)
20
+ load_configs(block)
21
+ load_models
33
22
  end
34
23
 
35
24
  def configs
36
- return Rails.application.config.elastify[:configs]
37
- end
25
+ Rails.application.config.elastify_configs = Elastify::Config.new unless Rails.application.config.respond_to?(:elastify_configs)
26
+ Rails.application.config.elastify_configs
27
+ end
28
+
29
+ def models
30
+ Rails.application.config.elastify_models = {} unless Rails.application.config.respond_to?(:elastify_models)
31
+ Rails.application.config.elastify_models
32
+ end
38
33
 
39
- def mappings
40
- return Rails.application.config.elastify[:mappings]
41
- end
34
+ def register_model(model_name)
35
+ model = Elastify::Model.new
36
+ yield(model) if block_given?
37
+ models[model_name] = model
38
+ end
39
+
40
+ private
41
+ def load_configs(block)
42
+ block.call(configs) if block.present?
43
+ end
44
+
45
+ def load_models
46
+ path = Rails.root.join('config/elastify')
47
+ if Dir.exist?(path)
48
+ Dir.glob("#{path}/*.rb") do |file_path|
49
+ require file_path
50
+ end
51
+ end
52
+ end
42
53
  end
43
-
44
- class ElastifyError < StandardError; end
45
54
  end
metadata CHANGED
@@ -1,117 +1,117 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: elastify
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Bortolotti Ribeiro
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-29 00:00:00.000000000 Z
11
+ date: 2018-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: 3.0.5
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: 3.0.5
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: httparty
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: 0.6.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.6.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: backgrounded
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: 2.0.0
48
48
  type: :runtime
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: 2.0.0
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: multi_json
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: 1.0.0
62
62
  type: :runtime
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: 1.0.0
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: rest-client
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ~>
73
+ - - "~>"
74
74
  - !ruby/object:Gem::Version
75
75
  version: '2.0'
76
- - - '>='
76
+ - - ">="
77
77
  - !ruby/object:Gem::Version
78
78
  version: 2.0.1
79
79
  type: :runtime
80
80
  prerelease: false
81
81
  version_requirements: !ruby/object:Gem::Requirement
82
82
  requirements:
83
- - - ~>
83
+ - - "~>"
84
84
  - !ruby/object:Gem::Version
85
85
  version: '2.0'
86
- - - '>='
86
+ - - ">="
87
87
  - !ruby/object:Gem::Version
88
88
  version: 2.0.1
89
89
  - !ruby/object:Gem::Dependency
90
90
  name: bundler
91
91
  requirement: !ruby/object:Gem::Requirement
92
92
  requirements:
93
- - - ~>
93
+ - - "~>"
94
94
  - !ruby/object:Gem::Version
95
95
  version: '1.13'
96
96
  type: :development
97
97
  prerelease: false
98
98
  version_requirements: !ruby/object:Gem::Requirement
99
99
  requirements:
100
- - - ~>
100
+ - - "~>"
101
101
  - !ruby/object:Gem::Version
102
102
  version: '1.13'
103
103
  - !ruby/object:Gem::Dependency
104
104
  name: rake
105
105
  requirement: !ruby/object:Gem::Requirement
106
106
  requirements:
107
- - - ~>
107
+ - - "~>"
108
108
  - !ruby/object:Gem::Version
109
109
  version: '10.0'
110
110
  type: :development
111
111
  prerelease: false
112
112
  version_requirements: !ruby/object:Gem::Requirement
113
113
  requirements:
114
- - - ~>
114
+ - - "~>"
115
115
  - !ruby/object:Gem::Version
116
116
  version: '10.0'
117
117
  - !ruby/object:Gem::Dependency
@@ -158,12 +158,12 @@ dependencies:
158
158
  version: 0.9.9.3
159
159
  description: A gem to help with ActiveRecord-ElasticSeach integration
160
160
  email:
161
- - b2bortolotti@gmail.com
161
+ - brunobortolotti@icloud.com
162
162
  executables: []
163
163
  extensions: []
164
164
  extra_rdoc_files: []
165
165
  files:
166
- - .gitignore
166
+ - ".gitignore"
167
167
  - CODE_OF_CONDUCT.md
168
168
  - Gemfile
169
169
  - LICENSE.txt
@@ -174,6 +174,8 @@ files:
174
174
  - elastify.gemspec
175
175
  - lib/elastify.rb
176
176
  - lib/elastify/active_record_extensions.rb
177
+ - lib/elastify/config.rb
178
+ - lib/elastify/configurators/initialization.rb
177
179
  - lib/elastify/configurators/model.rb
178
180
  - lib/elastify/errors/bad_request.rb
179
181
  - lib/elastify/errors/base.rb
@@ -183,29 +185,30 @@ files:
183
185
  - lib/elastify/helpers/elastic_search/search_result.rb
184
186
  - lib/elastify/helpers/elastic_search/search_result_collection.rb
185
187
  - lib/elastify/helpers/query_builder.rb
188
+ - lib/elastify/model.rb
186
189
  - lib/elastify/version.rb
187
190
  homepage: https://github.com/brunobortolotti/elastify.git
188
191
  licenses:
189
192
  - MIT
190
193
  metadata:
191
- allowed_push_host: 'TODO: Set to ''http://mygemserver.com'''
194
+ allowed_push_host: https://rubygems.org
192
195
  post_install_message:
193
196
  rdoc_options: []
194
197
  require_paths:
195
198
  - lib
196
199
  required_ruby_version: !ruby/object:Gem::Requirement
197
200
  requirements:
198
- - - '>='
201
+ - - ">="
199
202
  - !ruby/object:Gem::Version
200
203
  version: '0'
201
204
  required_rubygems_version: !ruby/object:Gem::Requirement
202
205
  requirements:
203
- - - '>='
206
+ - - ">="
204
207
  - !ruby/object:Gem::Version
205
208
  version: '0'
206
209
  requirements: []
207
210
  rubyforge_project:
208
- rubygems_version: 2.0.14.1
211
+ rubygems_version: 2.5.1
209
212
  signing_key:
210
213
  specification_version: 4
211
214
  summary: A gem to help with ActiveRecord-ElasticSeach integration