chewy 0.8.4 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (179) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +34 -0
  3. data/.rubocop_todo.yml +44 -0
  4. data/.travis.yml +20 -60
  5. data/Appraisals +15 -40
  6. data/CHANGELOG.md +42 -0
  7. data/Gemfile +1 -0
  8. data/Guardfile +5 -5
  9. data/README.md +155 -6
  10. data/Rakefile +11 -1
  11. data/chewy.gemspec +5 -7
  12. data/gemfiles/rails.3.2.activerecord.gemfile +1 -0
  13. data/gemfiles/rails.3.2.activerecord.kaminari.gemfile +1 -0
  14. data/gemfiles/rails.3.2.activerecord.will_paginate.gemfile +1 -0
  15. data/gemfiles/rails.4.2.activerecord.gemfile +1 -0
  16. data/gemfiles/rails.4.2.activerecord.kaminari.gemfile +1 -0
  17. data/gemfiles/rails.4.2.activerecord.will_paginate.gemfile +1 -0
  18. data/gemfiles/{rails.4.2.mongoid.4.0.0.gemfile → rails.4.2.mongoid.4.0.gemfile} +2 -1
  19. data/gemfiles/{rails.4.2.mongoid.4.0.0.kaminari.gemfile → rails.4.2.mongoid.4.0.kaminari.gemfile} +2 -1
  20. data/gemfiles/{rails.4.2.mongoid.4.0.0.will_paginate.gemfile → rails.4.2.mongoid.4.0.will_paginate.gemfile} +2 -1
  21. data/gemfiles/{rails.4.2.mongoid.5.1.0.gemfile → rails.4.2.mongoid.5.1.gemfile} +2 -1
  22. data/gemfiles/{rails.4.2.mongoid.5.1.0.kaminari.gemfile → rails.4.2.mongoid.5.1.kaminari.gemfile} +2 -1
  23. data/gemfiles/{rails.4.2.mongoid.5.1.0.will_paginate.gemfile → rails.4.2.mongoid.5.1.will_paginate.gemfile} +2 -1
  24. data/gemfiles/{rails.4.0.activerecord.gemfile → rails.5.0.activerecord.gemfile} +4 -2
  25. data/gemfiles/{rails.4.0.mongoid.4.0.0.kaminari.gemfile → rails.5.0.activerecord.kaminari.gemfile} +4 -2
  26. data/gemfiles/{rails.4.0.mongoid.4.0.0.will_paginate.gemfile → rails.5.0.activerecord.will_paginate.gemfile} +4 -2
  27. data/gemfiles/{sequel.4.31.gemfile → sequel.4.38.gemfile} +3 -2
  28. data/lib/chewy.rb +24 -16
  29. data/lib/chewy/backports/deep_dup.rb +1 -1
  30. data/lib/chewy/backports/duplicable.rb +1 -0
  31. data/lib/chewy/config.rb +13 -7
  32. data/lib/chewy/errors.rb +4 -4
  33. data/lib/chewy/fields/base.rb +19 -14
  34. data/lib/chewy/fields/root.rb +11 -9
  35. data/lib/chewy/index.rb +38 -25
  36. data/lib/chewy/index/actions.rb +17 -17
  37. data/lib/chewy/index/settings.rb +3 -4
  38. data/lib/chewy/journal.rb +107 -0
  39. data/lib/chewy/journal/apply.rb +31 -0
  40. data/lib/chewy/journal/clean.rb +24 -0
  41. data/lib/chewy/journal/entry.rb +83 -0
  42. data/lib/chewy/journal/query.rb +87 -0
  43. data/lib/chewy/log_subscriber.rb +8 -8
  44. data/lib/chewy/minitest.rb +1 -0
  45. data/lib/chewy/minitest/helpers.rb +77 -0
  46. data/lib/chewy/minitest/search_index_receiver.rb +80 -0
  47. data/lib/chewy/query.rb +116 -60
  48. data/lib/chewy/query/compose.rb +5 -6
  49. data/lib/chewy/query/criteria.rb +26 -16
  50. data/lib/chewy/query/filters.rb +9 -9
  51. data/lib/chewy/query/loading.rb +2 -2
  52. data/lib/chewy/query/nodes/and.rb +3 -3
  53. data/lib/chewy/query/nodes/base.rb +1 -1
  54. data/lib/chewy/query/nodes/bool.rb +6 -4
  55. data/lib/chewy/query/nodes/equal.rb +6 -6
  56. data/lib/chewy/query/nodes/exists.rb +2 -2
  57. data/lib/chewy/query/nodes/expr.rb +2 -2
  58. data/lib/chewy/query/nodes/field.rb +35 -31
  59. data/lib/chewy/query/nodes/has_child.rb +1 -0
  60. data/lib/chewy/query/nodes/has_parent.rb +1 -0
  61. data/lib/chewy/query/nodes/has_relation.rb +11 -13
  62. data/lib/chewy/query/nodes/match_all.rb +1 -1
  63. data/lib/chewy/query/nodes/missing.rb +2 -2
  64. data/lib/chewy/query/nodes/not.rb +3 -3
  65. data/lib/chewy/query/nodes/or.rb +3 -3
  66. data/lib/chewy/query/nodes/prefix.rb +4 -3
  67. data/lib/chewy/query/nodes/query.rb +3 -3
  68. data/lib/chewy/query/nodes/range.rb +11 -11
  69. data/lib/chewy/query/nodes/raw.rb +1 -1
  70. data/lib/chewy/query/nodes/regexp.rb +15 -11
  71. data/lib/chewy/query/nodes/script.rb +6 -6
  72. data/lib/chewy/query/pagination/will_paginate.rb +2 -2
  73. data/lib/chewy/railtie.rb +3 -3
  74. data/lib/chewy/rake_helper.rb +51 -30
  75. data/lib/chewy/repository.rb +2 -2
  76. data/lib/chewy/rspec.rb +1 -1
  77. data/lib/chewy/rspec/update_index.rb +46 -47
  78. data/lib/chewy/runtime/version.rb +4 -4
  79. data/lib/chewy/search.rb +7 -5
  80. data/lib/chewy/strategy.rb +10 -8
  81. data/lib/chewy/strategy/atomic.rb +2 -2
  82. data/lib/chewy/strategy/base.rb +4 -4
  83. data/lib/chewy/strategy/bypass.rb +1 -2
  84. data/lib/chewy/strategy/sidekiq.rb +2 -0
  85. data/lib/chewy/strategy/urgent.rb +1 -1
  86. data/lib/chewy/type.rb +51 -45
  87. data/lib/chewy/type/adapter/active_record.rb +23 -12
  88. data/lib/chewy/type/adapter/base.rb +4 -4
  89. data/lib/chewy/type/adapter/mongoid.rb +6 -6
  90. data/lib/chewy/type/adapter/object.rb +15 -12
  91. data/lib/chewy/type/adapter/orm.rb +24 -15
  92. data/lib/chewy/type/adapter/sequel.rb +11 -7
  93. data/lib/chewy/type/crutch.rb +4 -3
  94. data/lib/chewy/type/import.rb +51 -32
  95. data/lib/chewy/type/mapping.rb +17 -17
  96. data/lib/chewy/type/observe.rb +9 -7
  97. data/lib/chewy/type/witchcraft.rb +62 -23
  98. data/lib/chewy/type/wrapper.rb +20 -14
  99. data/lib/chewy/version.rb +1 -1
  100. data/lib/generators/chewy/install_generator.rb +3 -3
  101. data/lib/tasks/chewy.rake +28 -23
  102. data/spec/chewy/config_spec.rb +33 -12
  103. data/spec/chewy/fields/base_spec.rb +143 -154
  104. data/spec/chewy/fields/root_spec.rb +22 -20
  105. data/spec/chewy/fields/time_fields_spec.rb +11 -9
  106. data/spec/chewy/index/actions_spec.rb +27 -4
  107. data/spec/chewy/index/aliases_spec.rb +2 -2
  108. data/spec/chewy/index/settings_spec.rb +72 -50
  109. data/spec/chewy/index_spec.rb +95 -43
  110. data/spec/chewy/journal/apply_spec.rb +120 -0
  111. data/spec/chewy/journal/entry_spec.rb +237 -0
  112. data/spec/chewy/journal_spec.rb +173 -0
  113. data/spec/chewy/minitest/helpers_spec.rb +90 -0
  114. data/spec/chewy/minitest/search_index_receiver_spec.rb +120 -0
  115. data/spec/chewy/query/criteria_spec.rb +504 -237
  116. data/spec/chewy/query/filters_spec.rb +94 -66
  117. data/spec/chewy/query/loading_spec.rb +76 -40
  118. data/spec/chewy/query/nodes/and_spec.rb +3 -7
  119. data/spec/chewy/query/nodes/bool_spec.rb +5 -13
  120. data/spec/chewy/query/nodes/equal_spec.rb +20 -20
  121. data/spec/chewy/query/nodes/exists_spec.rb +7 -7
  122. data/spec/chewy/query/nodes/has_child_spec.rb +42 -23
  123. data/spec/chewy/query/nodes/has_parent_spec.rb +42 -23
  124. data/spec/chewy/query/nodes/match_all_spec.rb +2 -2
  125. data/spec/chewy/query/nodes/missing_spec.rb +6 -5
  126. data/spec/chewy/query/nodes/not_spec.rb +3 -7
  127. data/spec/chewy/query/nodes/or_spec.rb +3 -7
  128. data/spec/chewy/query/nodes/prefix_spec.rb +6 -6
  129. data/spec/chewy/query/nodes/query_spec.rb +3 -3
  130. data/spec/chewy/query/nodes/range_spec.rb +19 -19
  131. data/spec/chewy/query/nodes/raw_spec.rb +2 -2
  132. data/spec/chewy/query/nodes/regexp_spec.rb +31 -19
  133. data/spec/chewy/query/nodes/script_spec.rb +5 -5
  134. data/spec/chewy/query/pagination/kaminari_spec.rb +2 -2
  135. data/spec/chewy/query/pagination/will_paginage_spec.rb +6 -7
  136. data/spec/chewy/query/pagination_spec.rb +2 -3
  137. data/spec/chewy/query_spec.rb +208 -145
  138. data/spec/chewy/repository_spec.rb +8 -8
  139. data/spec/chewy/rspec/update_index_spec.rb +180 -111
  140. data/spec/chewy/search_spec.rb +8 -8
  141. data/spec/chewy/strategy/active_job_spec.rb +2 -2
  142. data/spec/chewy/strategy/atomic_spec.rb +4 -1
  143. data/spec/chewy/strategy/resque_spec.rb +2 -2
  144. data/spec/chewy/strategy/sidekiq_spec.rb +2 -2
  145. data/spec/chewy/type/actions_spec.rb +1 -1
  146. data/spec/chewy/type/adapter/active_record_spec.rb +255 -149
  147. data/spec/chewy/type/adapter/mongoid_spec.rb +169 -108
  148. data/spec/chewy/type/adapter/object_spec.rb +56 -40
  149. data/spec/chewy/type/adapter/sequel_spec.rb +248 -163
  150. data/spec/chewy/type/import_spec.rb +78 -47
  151. data/spec/chewy/type/mapping_spec.rb +6 -6
  152. data/spec/chewy/type/observe_spec.rb +20 -14
  153. data/spec/chewy/type/witchcraft_spec.rb +89 -43
  154. data/spec/chewy/type_spec.rb +4 -3
  155. data/spec/chewy_spec.rb +10 -8
  156. data/spec/spec_helper.rb +3 -0
  157. data/spec/support/active_record.rb +1 -1
  158. data/spec/support/class_helpers.rb +10 -11
  159. data/spec/support/mongoid.rb +2 -2
  160. data/spec/support/sequel.rb +1 -1
  161. metadata +65 -35
  162. data/gemfiles/rails.4.0.activerecord.kaminari.gemfile +0 -14
  163. data/gemfiles/rails.4.0.activerecord.will_paginate.gemfile +0 -14
  164. data/gemfiles/rails.4.0.mongoid.4.0.0.gemfile +0 -15
  165. data/gemfiles/rails.4.0.mongoid.5.1.0.gemfile +0 -15
  166. data/gemfiles/rails.4.0.mongoid.5.1.0.kaminari.gemfile +0 -14
  167. data/gemfiles/rails.4.0.mongoid.5.1.0.will_paginate.gemfile +0 -14
  168. data/gemfiles/rails.4.1.activerecord.gemfile +0 -15
  169. data/gemfiles/rails.4.1.activerecord.kaminari.gemfile +0 -14
  170. data/gemfiles/rails.4.1.activerecord.will_paginate.gemfile +0 -14
  171. data/gemfiles/rails.4.1.mongoid.4.0.0.gemfile +0 -15
  172. data/gemfiles/rails.4.1.mongoid.4.0.0.kaminari.gemfile +0 -14
  173. data/gemfiles/rails.4.1.mongoid.4.0.0.will_paginate.gemfile +0 -14
  174. data/gemfiles/rails.4.1.mongoid.5.1.0.gemfile +0 -15
  175. data/gemfiles/rails.4.1.mongoid.5.1.0.kaminari.gemfile +0 -14
  176. data/gemfiles/rails.4.1.mongoid.5.1.0.will_paginate.gemfile +0 -14
  177. data/gemfiles/rails.5.0.0.beta3.activerecord.gemfile +0 -16
  178. data/gemfiles/rails.5.0.0.beta3.activerecord.kaminari.gemfile +0 -16
  179. data/gemfiles/rails.5.0.0.beta3.activerecord.will_paginate.gemfile +0 -15
@@ -31,8 +31,8 @@ module Chewy
31
31
  # end
32
32
  # end
33
33
  #
34
- def root options = {}, &block
35
- raise "Root is already defined" if root_object
34
+ def root(options = {}, &block)
35
+ raise 'Root is already defined' if root_object
36
36
  build_root(options, &block)
37
37
  end
38
38
 
@@ -96,7 +96,7 @@ module Chewy
96
96
  # field :en, value: ->(name) { name['en'] }
97
97
  # end
98
98
  #
99
- # Off course it is possible to define object fields contents dynamically
99
+ # Of course it is possible to define object fields contents dynamically
100
100
  # but make sure evaluation proc returns hash:
101
101
  #
102
102
  # field :name, type: 'object', value: -> { name_translations }
@@ -109,7 +109,7 @@ module Chewy
109
109
  # field :sorted, analyzer: 'sorted'
110
110
  # end
111
111
  #
112
- def field *args, &block
112
+ def field(*args, &block)
113
113
  options = args.extract_options!
114
114
  build_root
115
115
 
@@ -136,10 +136,9 @@ module Chewy
136
136
  # end
137
137
  # end
138
138
  # end
139
- def agg *args, &block
140
- options = args.extract_options!
139
+ def agg(name, &block)
141
140
  build_root
142
- self._agg_defs = _agg_defs.merge(args.first => block)
141
+ self._agg_defs = _agg_defs.merge(name => block)
143
142
  end
144
143
  alias_method :aggregation, :agg
145
144
 
@@ -148,7 +147,7 @@ module Chewy
148
147
  # class CarsIndex < Chewy::Index
149
148
  # define_type Car do
150
149
  # template 'model.*', type: 'string', analyzer: 'special'
151
- # field 'model', type: 'object' # here we can put { ru: 'Мерседес', en: 'Mercedes' }
150
+ # field 'model', type: 'object' # here we can put { de: 'Der Mercedes', en: 'Mercedes' }
152
151
  # # and template will be applyed to this field
153
152
  # end
154
153
  # end
@@ -163,8 +162,8 @@ module Chewy
163
162
  # template /tit.+/, 'string', mapping_hash # "match_mapping_type" as the optionsl second argument
164
163
  # template template42: {match: 'hello*', mapping: {type: 'object'}} # or even pass a template as is
165
164
  #
166
- def template *args
167
- build_root.dynamic_template *args
165
+ def template(*args)
166
+ build_root.dynamic_template(*args)
168
167
  end
169
168
  alias_method :dynamic_template, :template
170
169
 
@@ -176,20 +175,21 @@ module Chewy
176
175
 
177
176
  private
178
177
 
179
- def expand_nested field, &block
178
+ def expand_nested(field, &block)
180
179
  if @_current_field
181
180
  field.parent = @_current_field
182
181
  @_current_field.children.push(field)
183
182
  end
184
183
 
185
- if block
186
- previous_field, @_current_field = @_current_field, field
187
- block.call
188
- @_current_field = previous_field
189
- end
184
+ return unless block
185
+
186
+ previous_field = @_current_field
187
+ @_current_field = field
188
+ yield
189
+ @_current_field = previous_field
190
190
  end
191
191
 
192
- def build_root options = {}, &block
192
+ def build_root(options = {}, &block)
193
193
  return root_object if root_object
194
194
  self.root_object = Chewy::Fields::Root.new(type_name, options)
195
195
  expand_nested(root_object, &block)
@@ -8,7 +8,7 @@ module Chewy
8
8
  options = args.extract_options!
9
9
  method = args.first
10
10
 
11
- Proc.new do
11
+ proc do
12
12
  backreference = if method && method.to_s == 'self'
13
13
  self
14
14
  elsif method
@@ -18,9 +18,11 @@ module Chewy
18
18
  end
19
19
 
20
20
  reference = if type_name.is_a?(Proc)
21
- type_name.arity == 0 ?
22
- instance_exec(&type_name) :
21
+ if type_name.arity.zero?
22
+ instance_exec(&type_name)
23
+ else
23
24
  type_name.call(self)
25
+ end
24
26
  else
25
27
  type_name
26
28
  end
@@ -31,11 +33,11 @@ module Chewy
31
33
 
32
34
  def extract_callback_options!(args)
33
35
  options = args.extract_options!
34
- options.each_key.with_object({}) { |key, hash|
36
+ result = options.each_key.with_object({}) do |key, hash|
35
37
  hash[key] = options.delete(key) if [:if, :unless].include?(key)
36
- }.tap {
37
- args.push(options) unless options.empty?
38
- }
38
+ end
39
+ args.push(options) unless options.empty?
40
+ result
39
41
  end
40
42
  end
41
43
 
@@ -1,10 +1,11 @@
1
- def try_require lib
2
- require lib
1
+ begin
2
+ require 'method_source'
3
+ require 'parser/current'
4
+ require 'unparser'
3
5
  rescue LoadError
6
+ nil
4
7
  end
5
8
 
6
- %w[method_source parser/current unparser].each { |lib| try_require lib }
7
-
8
9
  module Chewy
9
10
  class Type
10
11
  module Witchcraft
@@ -22,9 +23,9 @@ module Chewy
22
23
 
23
24
  def check_requirements!
24
25
  messages = []
25
- messages << "MethodSource gem is required for the Witchcraft™, please add `gem 'method_source'` to your Gemfile" unless Proc.method_defined?(:source)
26
- messages << "Parser gem is required for the Witchcraft™, please add `gem 'parser'` to your Gemfile" unless '::Parser'.safe_constantize
27
- messages << "Unparser gem is required for the Witchcraft™, please add `gem 'unparser'` to your Gemfile" unless '::Unparser'.safe_constantize
26
+ messages << "MethodSource gem is required for the Witchcraft, please add `gem 'method_source'` to your Gemfile" unless Proc.method_defined?(:source)
27
+ messages << "Parser gem is required for the Witchcraft, please add `gem 'parser'` to your Gemfile" unless '::Parser'.safe_constantize
28
+ messages << "Unparser gem is required for the Witchcraft, please add `gem 'unparser'` to your Gemfile" unless '::Unparser'.safe_constantize
28
29
  messages = messages.join("\n")
29
30
 
30
31
  raise messages if messages.present?
@@ -40,19 +41,22 @@ module Chewy
40
41
  end
41
42
 
42
43
  class Cauldron
44
+ attr_reader :locals
45
+
43
46
  def initialize(type)
44
47
  @type = type
48
+ @locals = []
45
49
  end
46
50
 
47
51
  def brew(object, crutches = nil)
48
- alicorn.call(object, crutches).as_json
52
+ alicorn.call(locals, object, crutches).as_json
49
53
  end
50
54
 
51
55
  private
52
56
 
53
57
  def alicorn
54
58
  @alicorn ||= class_eval <<-RUBY
55
- -> (object0, crutches) do
59
+ -> (locals, object0, crutches) do
56
60
  #{composed_values(@type.root_object, 0)}
57
61
  end
58
62
  RUBY
@@ -94,21 +98,21 @@ module Chewy
94
98
  <<-RUBY
95
99
  (if #{object}.is_a?(Hash)
96
100
  {
97
- #{non_proc_fields.map do |field|
98
- fetcher = "#{object}.has_key?(:#{field.name}) ? #{object}[:#{field.name}] : #{object}['#{field.name}']"
99
- "#{field.name}: #{composed_value(field, fetcher, nesting)}"
101
+ #{non_proc_fields.map do |f|
102
+ fetcher = "#{object}.has_key?(:#{f.name}) ? #{object}[:#{f.name}] : #{object}['#{f.name}']"
103
+ "#{f.name}: #{composed_value(f, fetcher, nesting)}"
100
104
  end.join(', ')}
101
105
  }
102
106
  else
103
107
  {
104
- #{non_proc_fields.map do |field|
105
- "#{field.name}: #{composed_value(field, "#{object}.#{field.name}", nesting)}"
108
+ #{non_proc_fields.map do |f|
109
+ "#{f.name}: #{composed_value(f, "#{object}.#{f.name}", nesting)}"
106
110
  end.join(', ')}
107
111
  }
108
112
  end)
109
113
  RUBY
110
114
  else
111
- "{}"
115
+ '{}'
112
116
  end
113
117
  end
114
118
 
@@ -118,13 +122,13 @@ module Chewy
118
122
  if proc_fields.present?
119
123
  <<-RUBY
120
124
  {
121
- #{proc_fields.map do |field|
122
- "#{field.name}: (#{composed_value(field, source_for(field.value, nesting), nesting)})"
125
+ #{proc_fields.map do |f|
126
+ "#{f.name}: (#{composed_value(f, source_for(f.value, nesting), nesting)})"
123
127
  end.join(', ')}
124
128
  }
125
129
  RUBY
126
130
  else
127
- "{}"
131
+ '{}'
128
132
  end
129
133
  end
130
134
 
@@ -145,26 +149,36 @@ module Chewy
145
149
  raise "No lambdas found, try to reformat your code:\n`#{proc.source}`" unless lambdas
146
150
 
147
151
  source = lambdas.first
148
- if proc.arity == 0
152
+ proc_params = proc.parameters.map(&:second)
153
+
154
+ if proc.arity.zero?
149
155
  source = replace_self(source, :"object#{nesting}")
150
156
  source = replace_send(source, :"object#{nesting}")
151
157
  elsif proc.arity < 0
152
158
  raise "Splat arguments are unsupported by witchcraft:\n`#{proc.source}"
153
159
  else
154
160
  (nesting + 1).times do |n|
155
- source = replace_lvar(source, proc.parameters[n][1], :"object#{n}") if proc.parameters[n]
161
+ source = replace_lvar(source, proc_params[n], :"object#{n}") if proc_params[n]
162
+ end
163
+ source = replace_lvar(source, proc_params[nesting + 1], :crutches) if proc_params[nesting + 1]
164
+
165
+ binding_variable_list(source).each do |variable|
166
+ locals.push(proc.binding.eval(variable.to_s))
167
+ source = replace_local(source, variable, locals.size - 1)
156
168
  end
157
- source = replace_lvar(source, proc.parameters[nesting + 1][1], :crutches) if proc.parameters[nesting + 1]
169
+
158
170
  end
171
+
159
172
  Unparser.unparse(source)
160
173
  end
161
174
 
162
175
  def exctract_lambdas(node)
176
+ return unless node.is_a?(Parser::AST::Node)
163
177
  if node.type == :block && node.children[0].type == :send && node.children[0].to_a == [nil, :lambda]
164
- node.children[2]
178
+ [node.children[2]]
165
179
  else
166
180
  node.children.map { |child| exctract_lambdas(child) }.flatten.compact
167
- end if node.is_a?(Parser::AST::Node)
181
+ end
168
182
  end
169
183
 
170
184
  def replace_lvar(node, old_variable, new_variable)
@@ -202,6 +216,31 @@ module Chewy
202
216
  node
203
217
  end
204
218
  end
219
+
220
+ def replace_local(node, variable, local_index)
221
+ if node.is_a?(Parser::AST::Node)
222
+ if node.type == :send && node.children.to_a == [nil, variable]
223
+ node.updated(nil, [
224
+ Parser::AST::Node.new(:lvar, [:locals]),
225
+ :[],
226
+ Parser::AST::Node.new(:int, [local_index])
227
+ ])
228
+ else
229
+ node.updated(nil, node.children.map { |child| replace_local(child, variable, local_index) })
230
+ end
231
+ else
232
+ node
233
+ end
234
+ end
235
+
236
+ def binding_variable_list(node)
237
+ return unless node.is_a?(Parser::AST::Node)
238
+ if node.type == :send && node.children[0].nil?
239
+ node.children[1]
240
+ else
241
+ node.children.map { |child| binding_variable_list(child) }.flatten.compact.uniq
242
+ end
243
+ end
205
244
  end
206
245
  end
207
246
  end
@@ -19,34 +19,40 @@ module Chewy
19
19
  end
20
20
  end
21
21
 
22
- def method_missing(method_name, *args, &block)
23
- method_name.to_s.match(/_highlight\z/) do |match|
24
- return highlight(match.pre_match) if highlight?(match.pre_match)
22
+ def method_missing(method, *args, &block)
23
+ m = method.to_s
24
+ if (name = highlight_name(m))
25
+ highlight(name)
26
+ elsif @attributes.key?(m)
27
+ @attributes[m]
28
+ elsif attribute_defined?(m)
29
+ nil
30
+ else
31
+ super
25
32
  end
26
- return @attributes[method_name.to_s] if @attributes.key?(method_name.to_s)
27
- return nil if attribute_defined?(method_name.to_s)
28
- super
29
33
  end
30
34
 
31
- def respond_to_missing?(method_name, include_private = false)
32
- method_name.to_s.match(/_highlight\z/) { |m| highlight?(m.pre_match) } ||
33
- @attributes.key?(method_name.to_s) ||
34
- attribute_defined?(method_name.to_s) ||
35
- super
35
+ def respond_to_missing?(method, include_private = false)
36
+ m = method.to_s
37
+ highlight_name(m) || @attributes.key?(m) || attribute_defined?(m) || super
36
38
  end
37
39
 
38
- private
40
+ private
41
+
42
+ def highlight_name(method)
43
+ method.sub(/_highlight\z/, '') if method.end_with?('_highlight')
44
+ end
39
45
 
40
46
  def attribute_defined?(attribute)
41
47
  self.class.root_object && self.class.root_object.children.find { |a| a.name.to_s == attribute }.present?
42
48
  end
43
49
 
44
50
  def highlight(attribute)
45
- _data["highlight"][attribute].first
51
+ _data['highlight'][attribute].first
46
52
  end
47
53
 
48
54
  def highlight?(attribute)
49
- _data.key?("highlight") && _data["highlight"].key?(attribute)
55
+ _data.key?('highlight') && _data['highlight'].key?(attribute)
50
56
  end
51
57
  end
52
58
  end
@@ -1,3 +1,3 @@
1
1
  module Chewy
2
- VERSION = '0.8.4'
2
+ VERSION = '0.9.0'.freeze
3
3
  end
@@ -1,11 +1,11 @@
1
1
  module Chewy
2
2
  module Generators
3
3
  class InstallGenerator < Rails::Generators::Base
4
- source_root File.expand_path("../../templates", __FILE__)
4
+ source_root File.expand_path('../../templates', __FILE__)
5
5
 
6
6
  def copy_configuration
7
- template "chewy.yml", "config/chewy.yml"
7
+ template 'chewy.yml', 'config/chewy.yml'
8
8
  end
9
9
  end
10
10
  end
11
- end
11
+ end
@@ -2,48 +2,53 @@ require 'chewy/rake_helper'
2
2
 
3
3
  namespace :chewy do
4
4
  desc 'Destroy, recreate and import data to specified index'
5
- task :reset, [:index] => :environment do |task, args|
5
+ task reset: :environment do |_task, args|
6
6
  Chewy::RakeHelper.subscribed_task_stats do
7
+ indexes = args.extras
7
8
 
8
- if args[:index].present?
9
- Chewy::RakeHelper.reset_index(args[:index])
9
+ if indexes.empty? || indexes.first.tr!('-', '')
10
+ Chewy::RakeHelper.reset_all(indexes)
10
11
  else
11
- Chewy::RakeHelper.reset_all
12
+ Chewy::RakeHelper.reset_index(indexes)
12
13
  end
13
14
  end
14
15
  end
15
16
 
16
- namespace :reset do
17
- desc 'Destroy, recreate and import data for all found indexes'
18
- task all: :environment do
19
- ActiveSupport::Deprecation.warn('`rake chewy:reset:all` is deprecated and will be removed soon. Use `rake chewy:reset` instead')
17
+ desc 'Updates data specified index'
18
+ task update: :environment do |_task, args|
19
+ Chewy::RakeHelper.subscribed_task_stats do
20
+ indexes = args.extras
20
21
 
21
- Chewy::RakeHelper.subscribed_task_stats do
22
- Chewy::RakeHelper.reset_all
22
+ if indexes.empty? || indexes.first.tr!('-', '')
23
+ Chewy::RakeHelper.update_all(indexes)
24
+ else
25
+ Chewy::RakeHelper.update_index(indexes)
23
26
  end
24
27
  end
25
28
  end
26
29
 
27
- desc 'Updates data specified index'
28
- task :update, [:index] => :environment do |task, args|
30
+ desc 'Applies changes that were done from specified moment (as a timestamp)'
31
+ task apply_changes_from: :environment do |_task, args|
29
32
  Chewy::RakeHelper.subscribed_task_stats do
33
+ params = args.extras
30
34
 
31
- if args[:index].present?
32
- Chewy::RakeHelper.update_index(args[:index])
35
+ if params.empty?
36
+ puts 'Please specify a timestamp like chewy:apply_changes_from[1469528705]'
33
37
  else
34
- Chewy::RakeHelper.update_all
38
+ timestamp, retries = params
39
+ time = Time.at(timestamp.to_i)
40
+ Chewy::Journal::Apply.since(time, retries: (retries.to_i if retries))
35
41
  end
36
42
  end
37
43
  end
38
44
 
39
- namespace :update do
40
- desc 'Updates data for all found indexes'
41
- task all: :environment do
42
- ActiveSupport::Deprecation.warn('`rake chewy:update:all` is deprecated and will be removed soon. Use `rake chewy:update` instead')
43
-
44
- Chewy::RakeHelper.subscribed_task_stats do
45
- Chewy::RakeHelper.update_all
46
- end
45
+ desc 'Cleans journal index. It accepts timestamp until which journal will be cleaned'
46
+ task clean_journal: :environment do |_task, args|
47
+ timestamp = args.extras.first
48
+ if timestamp
49
+ Chewy::Journal::Clean.until(Time.at(timestamp.to_i))
50
+ else
51
+ Chewy::Journal.delete!
47
52
  end
48
53
  end
49
54
  end