index-tanked 0.1.3 → 0.1.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.
data/lib/index-tanked.rb CHANGED
@@ -1,9 +1,11 @@
1
- begin
2
- require 'system_timer'
3
- IndexTankedTimer = SystemTimer
4
- rescue LoadError
5
- require 'timeout'
6
- IndexTankedTimer = Timeout
1
+ module IndexTanked
2
+ begin
3
+ require 'system_timer'
4
+ Timer = SystemTimer
5
+ rescue LoadError
6
+ require 'timeout'
7
+ Timer = Timeout
8
+ end
7
9
  end
8
10
 
9
11
  require 'indextank'
@@ -24,6 +24,81 @@ module IndexTanked
24
24
  lambda { |instance| "#{instance.class.name}:#{instance.id}"}
25
25
  end
26
26
 
27
+ def dependent_fields
28
+ if !@dependent_fields
29
+ @dependent_fields = @fields.map do |field|
30
+ field[2][:depends_on] || field[1]
31
+ end.flatten.compact.uniq
32
+
33
+ if !@dependent_fields.include?(:created_at) && model.column_names.include?("created_at")
34
+ @dependent_fields << :created_at
35
+ end
36
+ end
37
+
38
+ @dependent_fields
39
+ end
40
+
41
+ def dependent_fields_as_strings
42
+ @dependent_fields_as_strings ||= dependent_fields.map(&:to_s)
43
+ end
44
+
45
+ def dependent_fields_for_select(*additional_fields)
46
+ if additional_fields.empty?
47
+ dependent_fields_as_strings
48
+ else
49
+ (dependent_fields_as_strings + additional_fields.map(&:to_s)).uniq
50
+ end
51
+ end
52
+
53
+ def retry_on_error(options={}, &block)
54
+ times = options[:times] || 3
55
+ delay_multiplier = options[:delay_multiplier] || 0
56
+ excepts = [options[:except]].compact.flatten
57
+ count = 0
58
+ begin
59
+ instance_eval(&block)
60
+ rescue Timeout::Error, StandardError => e
61
+ if excepts.include?(e.class)
62
+ raise e
63
+ else
64
+ retry if times == :infinity
65
+ count += 1
66
+ sleep count * delay_multiplier
67
+ retry if count < times
68
+ raise e
69
+ end
70
+ end
71
+ end
72
+
73
+ def field(field_name, method=field_name, options={})
74
+ super
75
+
76
+ field = @fields.last
77
+ method = field[1]
78
+ method_without_sugar = method.to_s.sub(/[\?=]$/, '')
79
+ options = field[2]
80
+
81
+ if options[:depends_on].nil? || (options[:depends_on].is_a?(Array) && options[:depends_on].empty?)
82
+ case method
83
+ when Symbol
84
+ if !model.column_names.include?(method_without_sugar)
85
+ raise MissingFieldDependencyError, "The #{field_name} field requires a dependency to be specified"
86
+ end
87
+ when Proc
88
+ raise MissingFieldDependencyError, "The #{field_name} field requires a dependency to be specified"
89
+ end
90
+ end
91
+
92
+ dependencies = [options[:depends_on] || method_without_sugar].flatten.compact.map(&:to_s)
93
+ invalid_dependencies = dependencies - model.column_names
94
+
95
+ if !invalid_dependencies.empty?
96
+ raise InvalidFieldDependencyError, "The following field dependencies are invalid: #{invalid_dependencies.join(', ')}"
97
+ end
98
+
99
+ @fields
100
+ end
101
+
27
102
  end
28
103
  end
29
104
  end
@@ -16,7 +16,9 @@ module IndexTanked
16
16
  find_in_batches(:batch_size => batch_size) do |instances|
17
17
  documents = instances.map { |instance| instance.index_tanked.document_for_batch_addition }
18
18
  count += documents.size
19
- index_tanked.index.batch_insert(documents)
19
+ index_tanked.retry_on_error do
20
+ index_tanked.index.batch_insert(documents)
21
+ end
20
22
  end
21
23
  count
22
24
  end
@@ -1,11 +1,36 @@
1
1
  module IndexTanked
2
2
  module ActiveRecordDefaults
3
3
  class InstanceCompanion < IndexTanked::InstanceCompanion
4
+ if defined?(ActiveRecord::MissingAttributeError)
5
+ MissingAttributeError = ActiveRecord::MissingAttributeError
6
+ else
7
+ MissingAttributeError = ActiveModel::MissingAttributeError
8
+ end
9
+
10
+ def created_at
11
+ if @companion.respond_to?(:created_at)
12
+ @companion.created_at
13
+ else
14
+ Time.now
15
+ end
16
+ end
17
+
4
18
  def data
5
- field_data, other_data = *super
6
- field_data.merge!(:timestamp => @companion.created_at.to_i, :model => @companion.class.name)
19
+ begin
20
+ field_data, other_data = *super
21
+ field_data.merge!(:timestamp => created_at.to_i, :model => @companion.class.name)
22
+ rescue MissingAttributeError
23
+ @companion.reload
24
+ field_data, other_data = *super
25
+ field_data.merge!(:timestamp => created_at.to_i, :model => @companion.class.name)
26
+ end
7
27
  [field_data, other_data]
8
28
  end
29
+
30
+ def dependencies_changed?
31
+ @companion.class.index_tanked.dependent_fields.any?{|field| @companion.send("#{field}_changed?") }
32
+ end
33
+
9
34
  end
10
35
  end
11
36
  end
@@ -20,7 +20,7 @@ module IndexTanked
20
20
  IndexTanked::Configuration.timeout
21
21
  end
22
22
  if timeout
23
- IndexTankedTimer.timeout(timeout, TimeoutExceededError) do
23
+ IndexTanked::Timer.timeout(timeout, TimeoutExceededError) do
24
24
  sleep(timeout + 1) if $testing_index_tanked_timeout
25
25
  @index_tanked.index.document(doc_id).add(*data)
26
26
  end
@@ -52,7 +52,7 @@ module IndexTanked
52
52
  IndexTanked::Configuration.timeout
53
53
  end
54
54
  if timeout
55
- IndexTankedTimer.timeout(timeout, TimeoutExceededError) do
55
+ IndexTanked::Timer.timeout(timeout, TimeoutExceededError) do
56
56
  sleep(timeout + 1) if $testing_index_tanked_timeout
57
57
  @index_tanked.index.document(doc_id).delete
58
58
  end
@@ -7,6 +7,8 @@ module IndexTanked
7
7
  class URLNotProvidedError < IndexTankedError; end
8
8
  class IndexNameNotProvidedError < IndexTankedError; end
9
9
  class TimeoutExceededError < IndexTankedError; end
10
+ class MissingFieldDependencyError < IndexTankedError; end
11
+ class InvalidFieldDependencyError < IndexTankedError; end
10
12
 
11
13
  def self.included(base)
12
14
  base.class_eval do
@@ -26,9 +28,11 @@ module IndexTanked
26
28
  }
27
29
 
28
30
  after_save do |instance|
29
- instance.add_to_index_tank
30
- instance.class._ancestors_to_index.each do |ancestor|
31
- instance.becomes(ancestor).add_to_index_tank
31
+ if instance.index_tanked.dependencies_changed?
32
+ instance.add_to_index_tank
33
+ instance.class._ancestors_to_index.each do |ancestor|
34
+ instance.becomes(ancestor).add_to_index_tank
35
+ end
32
36
  end
33
37
  end
34
38
 
@@ -1,3 +1,3 @@
1
1
  module IndexTanked
2
- GEM_VERSION = '0.1.3'
2
+ GEM_VERSION = '0.1.4'
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: index-tanked
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
5
- prerelease:
4
+ hash: 19
5
+ prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 1
9
- - 3
10
- version: 0.1.3
9
+ - 4
10
+ version: 0.1.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Adam Kittelson
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2011-03-18 00:00:00 -05:00
19
+ date: 2011-03-22 00:00:00 -05:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -178,7 +178,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
178
178
  requirements: []
179
179
 
180
180
  rubyforge_project:
181
- rubygems_version: 1.5.0
181
+ rubygems_version: 1.3.7
182
182
  signing_key:
183
183
  specification_version: 3
184
184
  summary: Index Tank <http://indextank.com> integration library.