tire-erez 0.5.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (136) hide show
  1. data/.gitignore +14 -0
  2. data/.travis.yml +32 -0
  3. data/.yardopts +1 -0
  4. data/Gemfile +10 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.markdown +775 -0
  7. data/Rakefile +51 -0
  8. data/examples/rails-application-template.rb +263 -0
  9. data/examples/tire-dsl.rb +932 -0
  10. data/lib/tire.rb +59 -0
  11. data/lib/tire/alias.rb +296 -0
  12. data/lib/tire/configuration.rb +38 -0
  13. data/lib/tire/count.rb +85 -0
  14. data/lib/tire/dsl.rb +114 -0
  15. data/lib/tire/http/client.rb +62 -0
  16. data/lib/tire/http/clients/curb.rb +61 -0
  17. data/lib/tire/http/clients/faraday.rb +71 -0
  18. data/lib/tire/http/response.rb +27 -0
  19. data/lib/tire/index.rb +443 -0
  20. data/lib/tire/logger.rb +60 -0
  21. data/lib/tire/model/callbacks.rb +40 -0
  22. data/lib/tire/model/import.rb +27 -0
  23. data/lib/tire/model/indexing.rb +134 -0
  24. data/lib/tire/model/naming.rb +100 -0
  25. data/lib/tire/model/percolate.rb +99 -0
  26. data/lib/tire/model/persistence.rb +72 -0
  27. data/lib/tire/model/persistence/attributes.rb +148 -0
  28. data/lib/tire/model/persistence/finders.rb +54 -0
  29. data/lib/tire/model/persistence/storage.rb +77 -0
  30. data/lib/tire/model/search.rb +322 -0
  31. data/lib/tire/multi_search.rb +263 -0
  32. data/lib/tire/results/collection.rb +156 -0
  33. data/lib/tire/results/item.rb +94 -0
  34. data/lib/tire/results/pagination.rb +68 -0
  35. data/lib/tire/rubyext/hash.rb +8 -0
  36. data/lib/tire/rubyext/ruby_1_8.rb +1 -0
  37. data/lib/tire/rubyext/symbol.rb +11 -0
  38. data/lib/tire/rubyext/uri_escape.rb +74 -0
  39. data/lib/tire/search.rb +211 -0
  40. data/lib/tire/search/facet.rb +81 -0
  41. data/lib/tire/search/filter.rb +28 -0
  42. data/lib/tire/search/highlight.rb +37 -0
  43. data/lib/tire/search/queries/match.rb +40 -0
  44. data/lib/tire/search/query.rb +250 -0
  45. data/lib/tire/search/scan.rb +114 -0
  46. data/lib/tire/search/script_field.rb +23 -0
  47. data/lib/tire/search/sort.rb +25 -0
  48. data/lib/tire/tasks.rb +138 -0
  49. data/lib/tire/utils.rb +17 -0
  50. data/lib/tire/version.rb +18 -0
  51. data/test/fixtures/articles/1.json +1 -0
  52. data/test/fixtures/articles/2.json +1 -0
  53. data/test/fixtures/articles/3.json +1 -0
  54. data/test/fixtures/articles/4.json +1 -0
  55. data/test/fixtures/articles/5.json +1 -0
  56. data/test/integration/active_model_indexing_test.rb +51 -0
  57. data/test/integration/active_model_searchable_test.rb +114 -0
  58. data/test/integration/active_record_searchable_test.rb +620 -0
  59. data/test/integration/boolean_queries_test.rb +43 -0
  60. data/test/integration/boosting_queries_test.rb +32 -0
  61. data/test/integration/bulk_test.rb +86 -0
  62. data/test/integration/count_test.rb +64 -0
  63. data/test/integration/custom_score_queries_test.rb +89 -0
  64. data/test/integration/dis_max_queries_test.rb +68 -0
  65. data/test/integration/dsl_search_test.rb +30 -0
  66. data/test/integration/explanation_test.rb +44 -0
  67. data/test/integration/facets_test.rb +311 -0
  68. data/test/integration/filtered_queries_test.rb +66 -0
  69. data/test/integration/filters_test.rb +75 -0
  70. data/test/integration/fuzzy_queries_test.rb +20 -0
  71. data/test/integration/highlight_test.rb +64 -0
  72. data/test/integration/index_aliases_test.rb +122 -0
  73. data/test/integration/index_mapping_test.rb +43 -0
  74. data/test/integration/index_store_test.rb +112 -0
  75. data/test/integration/index_update_document_test.rb +121 -0
  76. data/test/integration/match_query_test.rb +79 -0
  77. data/test/integration/mongoid_searchable_test.rb +309 -0
  78. data/test/integration/multi_search_test.rb +114 -0
  79. data/test/integration/nested_query_test.rb +135 -0
  80. data/test/integration/percolator_test.rb +111 -0
  81. data/test/integration/persistent_model_test.rb +205 -0
  82. data/test/integration/prefix_query_test.rb +43 -0
  83. data/test/integration/query_return_version_test.rb +70 -0
  84. data/test/integration/query_string_test.rb +52 -0
  85. data/test/integration/range_queries_test.rb +36 -0
  86. data/test/integration/reindex_test.rb +56 -0
  87. data/test/integration/results_test.rb +58 -0
  88. data/test/integration/scan_test.rb +56 -0
  89. data/test/integration/script_fields_test.rb +38 -0
  90. data/test/integration/sort_test.rb +52 -0
  91. data/test/integration/text_query_test.rb +39 -0
  92. data/test/models/active_model_article.rb +31 -0
  93. data/test/models/active_model_article_with_callbacks.rb +49 -0
  94. data/test/models/active_model_article_with_custom_document_type.rb +7 -0
  95. data/test/models/active_model_article_with_custom_index_name.rb +7 -0
  96. data/test/models/active_record_models.rb +131 -0
  97. data/test/models/article.rb +15 -0
  98. data/test/models/mongoid_models.rb +85 -0
  99. data/test/models/persistent_article.rb +11 -0
  100. data/test/models/persistent_article_in_index.rb +16 -0
  101. data/test/models/persistent_article_in_namespace.rb +12 -0
  102. data/test/models/persistent_article_with_casting.rb +28 -0
  103. data/test/models/persistent_article_with_defaults.rb +12 -0
  104. data/test/models/persistent_article_with_percolation.rb +5 -0
  105. data/test/models/persistent_articles_with_custom_index_name.rb +10 -0
  106. data/test/models/supermodel_article.rb +17 -0
  107. data/test/models/validated_model.rb +11 -0
  108. data/test/test_helper.rb +118 -0
  109. data/test/unit/active_model_lint_test.rb +17 -0
  110. data/test/unit/configuration_test.rb +84 -0
  111. data/test/unit/count_test.rb +67 -0
  112. data/test/unit/http_client_test.rb +79 -0
  113. data/test/unit/http_response_test.rb +49 -0
  114. data/test/unit/index_alias_test.rb +335 -0
  115. data/test/unit/index_test.rb +1098 -0
  116. data/test/unit/logger_test.rb +125 -0
  117. data/test/unit/model_callbacks_test.rb +116 -0
  118. data/test/unit/model_import_test.rb +75 -0
  119. data/test/unit/model_initialization_test.rb +31 -0
  120. data/test/unit/model_persistence_test.rb +548 -0
  121. data/test/unit/model_search_test.rb +964 -0
  122. data/test/unit/multi_search_test.rb +304 -0
  123. data/test/unit/results_collection_test.rb +372 -0
  124. data/test/unit/results_item_test.rb +173 -0
  125. data/test/unit/rubyext_test.rb +66 -0
  126. data/test/unit/search_facet_test.rb +186 -0
  127. data/test/unit/search_filter_test.rb +42 -0
  128. data/test/unit/search_highlight_test.rb +46 -0
  129. data/test/unit/search_query_test.rb +419 -0
  130. data/test/unit/search_scan_test.rb +113 -0
  131. data/test/unit/search_script_field_test.rb +26 -0
  132. data/test/unit/search_sort_test.rb +50 -0
  133. data/test/unit/search_test.rb +556 -0
  134. data/test/unit/tire_test.rb +144 -0
  135. data/tire.gemspec +83 -0
  136. metadata +586 -0
@@ -0,0 +1,72 @@
1
+ module Tire
2
+ module Model
3
+
4
+ # Allows to use _Elasticsearch_ as a primary database (storage).
5
+ #
6
+ # Contains all the `Tire::Model::Search` features and provides
7
+ # an [_ActiveModel_](http://rubygems.org/gems/activemodel)-compatible
8
+ # interface for persistance.
9
+ #
10
+ # Usage:
11
+ #
12
+ # class Article
13
+ # include Tire::Model::Persistence
14
+ #
15
+ # property :title
16
+ # end
17
+ #
18
+ # Article.create :id => 1, :title => 'One'
19
+ #
20
+ # article = Article.find
21
+ #
22
+ # article.destroy
23
+ #
24
+ module Persistence
25
+
26
+ def self.included(base)
27
+
28
+ base.class_eval do
29
+ include ActiveModel::AttributeMethods
30
+ include ActiveModel::Validations
31
+ include ActiveModel::Serialization
32
+ include ActiveModel::Serializers::JSON
33
+ include ActiveModel::Naming
34
+ include ActiveModel::Conversion
35
+
36
+ extend ActiveModel::Callbacks
37
+ define_model_callbacks :save, :destroy
38
+
39
+ extend Persistence::Finders::ClassMethods
40
+ extend Persistence::Attributes::ClassMethods
41
+ include Persistence::Attributes::InstanceMethods
42
+ include Persistence::Storage
43
+
44
+ include Tire::Model::Search
45
+
46
+ ['_score', '_type', '_index', '_version', 'sort', 'highlight', '_explanation'].each do |attr|
47
+ define_method("#{attr}=") { |value| @attributes ||= {}; @attributes[attr] = value }
48
+ define_method("#{attr}") { @attributes[attr] }
49
+ end
50
+
51
+ def self.search(*args, &block)
52
+ args.last.update(:wrapper => self, :version => true) if args.last.is_a? Hash
53
+ args << { :wrapper => self, :version => true } unless args.any? { |a| a.is_a? Hash }
54
+
55
+ self.tire.search(*args, &block)
56
+ end
57
+
58
+ def self.multi_search(*args, &block)
59
+ args.last.update(:wrapper => self, :version => true) if args.last.is_a? Hash
60
+ args << { :wrapper => self, :version => true } unless args.any? { |a| a.is_a? Hash }
61
+
62
+ self.tire.multi_search(*args, &block)
63
+ end
64
+
65
+ end
66
+
67
+ end
68
+
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,148 @@
1
+ module Tire
2
+ module Model
3
+
4
+ module Persistence
5
+
6
+ # Provides infrastructure for declaring the model properties and accessing them.
7
+ #
8
+ module Attributes
9
+
10
+ module ClassMethods
11
+
12
+ # Define property of the model:
13
+ #
14
+ # class Article
15
+ # include Tire::Model::Persistence
16
+ #
17
+ # property :title, :analyzer => 'snowball'
18
+ # property :published, :type => 'date'
19
+ # property :tags, :analyzer => 'keywords', :default => []
20
+ # end
21
+ #
22
+ # You can pass mapping definition for Elasticsearch in the options Hash.
23
+ #
24
+ # You can define default property values.
25
+ #
26
+ def property(name, options = {})
27
+
28
+ # Define attribute reader:
29
+ define_method("#{name}") do
30
+ instance_variable_get(:"@#{name}")
31
+ end
32
+
33
+ # Define attribute writer:
34
+ define_method("#{name}=") do |value|
35
+ instance_variable_set(:"@#{name}", value)
36
+ end
37
+
38
+ # Save the property in properties array:
39
+ properties << name.to_s unless properties.include?(name.to_s)
40
+
41
+ # Define convenience <NAME>? method:
42
+ define_query_method name.to_sym
43
+
44
+ # ActiveModel compatibility. NEEDED?
45
+ define_attribute_methods [name.to_sym]
46
+
47
+ # Save property default value (when relevant):
48
+ unless (default_value = options.delete(:default)).nil?
49
+ property_defaults[name.to_sym] = default_value.respond_to?(:call) ? default_value.call : default_value
50
+ end
51
+
52
+ # Save property casting (when relevant):
53
+ property_types[name.to_sym] = options[:class] if options[:class]
54
+
55
+ # Define default value for colletions:
56
+ if options[:class].is_a?(Array)
57
+ property_defaults[name.to_sym] ||= []
58
+ end
59
+
60
+ # Store mapping for the property:
61
+ mapping[name] = options
62
+ self
63
+ end
64
+
65
+ def properties
66
+ @properties ||= []
67
+ end
68
+
69
+ def property_defaults
70
+ @property_defaults ||= {}
71
+ end
72
+
73
+ def property_types
74
+ @property_types ||= {}
75
+ end
76
+
77
+ private
78
+
79
+ def define_query_method name
80
+ define_method("#{name}?") { !! send(name) }
81
+ end
82
+
83
+ end
84
+
85
+ module InstanceMethods
86
+
87
+ attr_accessor :id
88
+
89
+ def initialize(attributes={})
90
+ # Make a copy of objects in the property defaults hash, so default values such as `[]` or `{ foo: [] }` are left intact
91
+ property_defaults = self.class.property_defaults.inject({}) do |hash, item|
92
+ key, value = item
93
+ hash[key.to_s] = value.class.respond_to?(:new) ? value.clone : value
94
+ hash
95
+ end
96
+
97
+ __update_attributes(property_defaults.merge(attributes))
98
+ end
99
+
100
+ def attributes
101
+ self.class.properties.
102
+ inject( self.id ? {'id' => self.id} : {} ) {|attributes, key| attributes[key] = send(key); attributes}
103
+ end
104
+
105
+ def attribute_names
106
+ self.class.properties.sort
107
+ end
108
+
109
+ def has_attribute?(name)
110
+ properties.include?(name.to_s)
111
+ end
112
+ alias :has_property? :has_attribute?
113
+
114
+ def __update_attributes(attributes)
115
+ attributes.each { |name, value| send "#{name}=", __cast_value(name, value) }
116
+ end
117
+
118
+ # Casts the values according to the <tt>:class</tt> option set when
119
+ # defining the property, cast Hashes as Hashr[http://rubygems.org/gems/hashr]
120
+ # instances and automatically convert UTC formatted strings to Time.
121
+ #
122
+ def __cast_value(name, value)
123
+ case
124
+
125
+ when klass = self.class.property_types[name.to_sym]
126
+ if klass.is_a?(Array) && value.is_a?(Array)
127
+ value.map { |v| klass.first.new(v) }
128
+ else
129
+ klass.new(value)
130
+ end
131
+
132
+ when value.is_a?(Hash)
133
+ Hashr.new(value)
134
+
135
+ else
136
+ # Strings formatted as <http://en.wikipedia.org/wiki/ISO8601> are automatically converted to Time
137
+ value = Time.parse(value) if value.is_a?(String) && value =~ /^\d{4}[\/\-]\d{2}[\/\-]\d{2}T\d{2}\:\d{2}\:\d{2}Z$/
138
+ value
139
+ end
140
+ end
141
+
142
+ end
143
+
144
+ end
145
+
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,54 @@
1
+ module Tire
2
+ module Model
3
+
4
+ module Persistence
5
+
6
+ # Provides infrastructure for an _ActiveRecord_-like interface for finding records.
7
+ #
8
+ module Finders
9
+
10
+ module ClassMethods
11
+
12
+ def find *args
13
+ # TODO: Options like `sort`
14
+ options = args.pop if args.last.is_a?(Hash)
15
+ args.flatten!
16
+ if args.size > 1
17
+ Tire::Search::Search.new(index.name, :wrapper => self) do |search|
18
+ search.query do |query|
19
+ query.ids(args, document_type)
20
+ end
21
+ search.size args.size
22
+ end.results
23
+ else
24
+ case args = args.pop
25
+ when Fixnum, String
26
+ index.retrieve document_type, args, :wrapper => self
27
+ when :all, :first
28
+ send(args)
29
+ else
30
+ raise ArgumentError, "Please pass either ID as Fixnum or String, or :all, :first as an argument"
31
+ end
32
+ end
33
+ end
34
+
35
+ def all
36
+ # TODO: Options like `sort`; Possibly `filters`
37
+ s = Tire::Search::Search.new(index.name, :type => document_type, :wrapper => self).query { all }
38
+ s.version(true).results
39
+ end
40
+
41
+ def first
42
+ # TODO: Options like `sort`; Possibly `filters`
43
+ s = Tire::Search::Search.new(index.name, :type => document_type, :wrapper => self).query { all }.size(1)
44
+ s.version(true).results.first
45
+ end
46
+
47
+ end
48
+
49
+ end
50
+
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,77 @@
1
+ module Tire
2
+ module Model
3
+
4
+ module Persistence
5
+
6
+ # Provides infrastructure for storing records in _Elasticsearch_.
7
+ #
8
+ module Storage
9
+ def self.included(base)
10
+ base.class_eval do
11
+ extend ClassMethods
12
+ include InstanceMethods
13
+ end
14
+ end
15
+
16
+ module ClassMethods
17
+ def create(args={})
18
+ document = new(args)
19
+ return false unless document.valid?
20
+ document.save
21
+ document
22
+ end
23
+ end
24
+
25
+ module InstanceMethods
26
+ def update_attribute(name, value)
27
+ __update_attributes name => value
28
+ save
29
+ end
30
+
31
+ def update_attributes(attributes={})
32
+ __update_attributes attributes
33
+ save
34
+ end
35
+
36
+ def update_index
37
+ run_callbacks :update_elasticsearch_index do
38
+ if destroyed?
39
+ index.remove self
40
+ else
41
+ response = index.store( self, {:percolate => percolator} )
42
+ self.id ||= response['_id']
43
+ self._index = response['_index']
44
+ self._type = response['_type']
45
+ self._version = response['_version']
46
+ self.gec | update = response['tire_matches']
47
+ self
48
+ end
49
+ end
50
+ end
51
+
52
+ def save
53
+ return false unless valid?
54
+ run_callbacks :save do
55
+ update_index
56
+ end
57
+ self
58
+ end
59
+
60
+ def destroy
61
+ run_callbacks :destroy do
62
+ @destroyed = true
63
+ update_index
64
+ end
65
+ self.freeze
66
+ end
67
+
68
+ def destroyed? ; !!@destroyed; end
69
+ def persisted? ; !!id && !!_version; end
70
+ def new_record? ; !persisted?; end
71
+ end
72
+ end
73
+
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,322 @@
1
+ module Tire
2
+ module Model
3
+
4
+ # Main module containing the search infrastructure for ActiveModel classes.
5
+ #
6
+ # By including this module, you'll provide the model with facilities to
7
+ # perform searches against index, define index settings and mappings,
8
+ # access the index object, etc.
9
+ #
10
+ # All the _Tire_ methods are accessible via the "proxy" class and instance
11
+ # methods of the model, named `tire`, eg. `Article.tire.search 'foo'`.
12
+ #
13
+ # When there's no clash with a method in the class (your own, defined by another gem, etc)
14
+ # _Tire_ will bring these methods to the top-level namespace of the class,
15
+ # eg. `Article.search 'foo'`.
16
+ #
17
+ # You'll find the relevant methods in the ClassMethods and InstanceMethods module.
18
+ #
19
+ #
20
+ module Search
21
+
22
+ # Alias for Tire::Model::Naming::ClassMethods.index_prefix
23
+ #
24
+ def self.index_prefix(*args)
25
+ Naming::ClassMethods.index_prefix(*args)
26
+ end
27
+
28
+ module ClassMethods
29
+
30
+ # Returns search results for a given query.
31
+ #
32
+ # Query can be passed simply as a String:
33
+ #
34
+ # Article.search 'love'
35
+ #
36
+ # Any options, such as pagination or sorting, can be passed as a second argument:
37
+ #
38
+ # Article.search 'love', :per_page => 25, :page => 2
39
+ # Article.search 'love', :sort => 'title'
40
+ #
41
+ # For more powerful query definition, use the query DSL passed as a block:
42
+ #
43
+ # Article.search do
44
+ # query { terms :tags, ['ruby', 'python'] }
45
+ # facet 'tags' { terms :tags }
46
+ # end
47
+ #
48
+ # You can pass options as the first argument, in this case:
49
+ #
50
+ # Article.search :per_page => 25, :page => 2 do
51
+ # query { string 'love' }
52
+ # end
53
+ #
54
+ # This methods returns a Tire::Results::Collection instance, containing instances
55
+ # of Tire::Results::Item, populated by the data available in _Elasticsearch, by default.
56
+ #
57
+ # If you'd like to load the "real" models from the database, you may use the `:load` option:
58
+ #
59
+ # Article.search 'love', :load => true
60
+ #
61
+ # You can pass options as a Hash to the model's `find` method:
62
+ #
63
+ # Article.search :load => { :include => 'comments' } do ... end
64
+ #
65
+ def search(*args, &block)
66
+ default_options = {:type => document_type, :index => index.name}
67
+
68
+ if block_given?
69
+ options = args.shift || {}
70
+ else
71
+ query, options = args
72
+ options ||= {}
73
+ end
74
+
75
+ options = default_options.update(options)
76
+ sort = Array( options.delete(:order) || options.delete(:sort) )
77
+
78
+ s = Tire::Search::Search.new(options.delete(:index), options)
79
+
80
+ page = options.delete(:page)
81
+ per_page = options.delete(:per_page) || Tire::Results::Pagination::default_per_page
82
+
83
+ s.size( per_page.to_i ) if per_page
84
+ s.from( page.to_i <= 1 ? 0 : (per_page.to_i * (page.to_i-1)) ) if page && per_page
85
+
86
+ s.sort do
87
+ sort.each do |t|
88
+ field_name, direction = t.split(':')
89
+ by field_name, direction
90
+ end
91
+ end unless sort.empty?
92
+
93
+ version = options.delete(:version)
94
+ s.version(version) if version
95
+
96
+ if block_given?
97
+ block.arity < 1 ? s.instance_eval(&block) : block.call(s)
98
+ else
99
+ s.query { string query }
100
+ # TODO: Actualy, allow passing all the valid options from
101
+ # <http://www.elasticsearch.org/guide/reference/api/search/uri-request.html>
102
+ s.fields Array(options[:fields]) if options[:fields]
103
+ end
104
+
105
+ s.results
106
+ end
107
+
108
+ def multi_search(options={}, &block)
109
+ default_options = {:type => document_type}
110
+ options = default_options.update(options)
111
+ Tire::Search::Multi::Search.new(index.name, options, &block).results
112
+ end
113
+
114
+ # Returns a Tire::Index instance for this model.
115
+ #
116
+ # Example usage: `Article.index.refresh`.
117
+ #
118
+ def index
119
+ name = index_name.respond_to?(:to_proc) ? klass.instance_eval(&index_name) : index_name
120
+ @index = Index.new(name)
121
+ end
122
+
123
+ end
124
+
125
+ module InstanceMethods
126
+
127
+ # Returns a Tire::Index instance for this instance of the model.
128
+ #
129
+ # Example usage: `@article.index.refresh`.
130
+ #
131
+ def index
132
+ instance.class.tire.index
133
+ end
134
+
135
+ # Updates the index in _Elasticsearch_.
136
+ #
137
+ # On model instance create or update, it will store its serialized representation in the index.
138
+ #
139
+ # On model destroy, it will remove the corresponding document from the index.
140
+ #
141
+ # It will also execute any `<after|before>_update_elasticsearch_index` callback hooks.
142
+ #
143
+ def update_index
144
+ instance.run_callbacks :update_elasticsearch_index do
145
+ if instance.destroyed?
146
+ index.remove instance
147
+ else
148
+ response = index.store( instance, {:percolate => percolator} )
149
+ instance.tire.tire_matches = response['tire_matches'] if instance.tire.respond_to?(:tire_matches=)
150
+ self
151
+ end
152
+ end
153
+ end
154
+ alias :update_elasticsearch_index :update_index
155
+ alias :update_elastic_search_index :update_index
156
+
157
+ # The default JSON serialization of the model, based on its `#to_hash` representation.
158
+ #
159
+ # If you don't define any mapping, the model is serialized as-is.
160
+ #
161
+ # If you do define the mapping for _Elasticsearch_, only attributes
162
+ # declared in the mapping are serialized.
163
+ #
164
+ # For properties declared with the `:as` option, the passed String or Proc
165
+ # is evaluated in the instance context. Other objects are indexed "as is".
166
+ #
167
+ def to_indexed_json
168
+ if instance.class.tire.mapping.empty?
169
+ # Reject the id and type keys
170
+ instance.to_hash.reject {|key,_| key.to_s == 'id' || key.to_s == 'type' }.to_json
171
+ else
172
+ mapping = instance.class.tire.mapping
173
+ # Reject keys not declared in mapping
174
+ hash = instance.to_hash.reject { |key, value| ! mapping.keys.map(&:to_s).include?(key.to_s) }
175
+
176
+ # Evalute the `:as` options
177
+ mapping.each do |key, options|
178
+ case options[:as]
179
+ when String
180
+ hash[key] = instance.instance_eval(options[:as])
181
+ when Proc
182
+ hash[key] = instance.instance_eval(&options[:as])
183
+ else
184
+ hash[key] = options[:as]
185
+ end if options[:as]
186
+ end
187
+
188
+ hash.to_json
189
+ end
190
+ end
191
+
192
+ def tire_matches
193
+ instance.instance_eval do
194
+ @tire__attributes ||= {}
195
+ @tire__attributes['tire_matches']
196
+ end
197
+ end
198
+
199
+ def tire_matches=(value)
200
+ instance.instance_eval do
201
+ @tire__attributes ||= {}
202
+ @tire__attributes['tire_matches'] = value
203
+ end
204
+ end
205
+
206
+ end
207
+
208
+ module Loader
209
+
210
+ # Load the "real" model from the database via the corresponding model's `find` method.
211
+ #
212
+ # Notice that there's an option to eagerly load models with the `:load` option
213
+ # for the search method.
214
+ #
215
+ def load(options=nil)
216
+ options ? self.class.find(self.id, options) : self.class.find(self.id)
217
+ end
218
+
219
+ end
220
+
221
+ # An object containing _Tire's_ model class methods, accessed as `Article.tire`.
222
+ #
223
+ class ClassMethodsProxy
224
+ include Tire::Model::Naming::ClassMethods
225
+ include Tire::Model::Import::ClassMethods
226
+ include Tire::Model::Indexing::ClassMethods
227
+ include Tire::Model::Percolate::ClassMethods
228
+ include ClassMethods
229
+
230
+ INTERFACE = public_instance_methods.map(&:to_sym) - Object.public_instance_methods.map(&:to_sym)
231
+
232
+ attr_reader :klass
233
+ def initialize(klass)
234
+ @klass = klass
235
+ end
236
+
237
+ end
238
+
239
+ # An object containing _Tire's_ model instance methods, accessed as `@article.tire`.
240
+ #
241
+ class InstanceMethodsProxy
242
+ include Tire::Model::Naming::InstanceMethods
243
+ include Tire::Model::Percolate::InstanceMethods
244
+ include InstanceMethods
245
+
246
+ INTERFACE = public_instance_methods.map(&:to_sym) - Object.public_instance_methods.map(&:to_sym)
247
+
248
+ attr_reader :instance
249
+ def initialize(instance)
250
+ @instance = instance
251
+ end
252
+ end
253
+
254
+ # A hook triggered by the `include Tire::Model::Search` statement in the model.
255
+ #
256
+ def self.included(base)
257
+ base.class_eval do
258
+
259
+ # Returns proxy to the _Tire's_ class methods.
260
+ #
261
+ def self.tire &block
262
+ @__tire__ ||= ClassMethodsProxy.new(self)
263
+
264
+ @__tire__.instance_eval(&block) if block_given?
265
+ @__tire__
266
+ end
267
+
268
+ # Returns proxy to the _Tire's_ instance methods.
269
+ #
270
+ def tire &block
271
+ @__tire__ ||= InstanceMethodsProxy.new(self)
272
+
273
+ @__tire__.instance_eval(&block) if block_given?
274
+ @__tire__
275
+ end
276
+
277
+ # Define _Tire's_ callbacks (<after|before>_update_elasticsearch_index).
278
+ #
279
+ define_model_callbacks(:update_elasticsearch_index, :only => [:after, :before]) if \
280
+ respond_to?(:define_model_callbacks)
281
+
282
+ # Serialize the model as a Hash.
283
+ #
284
+ # Uses `serializable_hash` representation of the model,
285
+ # unless implemented in the model already.
286
+ #
287
+ def to_hash
288
+ self.serializable_hash
289
+ end unless instance_methods.map(&:to_sym).include?(:to_hash)
290
+
291
+ end
292
+
293
+ # Alias _Tire's_ class methods in the top-level namespace of the model,
294
+ # unless there's a conflict with existing method.
295
+ #
296
+ ClassMethodsProxy::INTERFACE.each do |method|
297
+ base.class_eval <<-"end;", __FILE__, __LINE__ unless base.public_methods.map(&:to_sym).include?(method.to_sym)
298
+ def self.#{method}(*args, &block) # def search(*args, &block)
299
+ tire.__send__(#{method.inspect}, *args, &block) # tire.__send__(:search, *args, &block)
300
+ end # end
301
+ end;
302
+ end
303
+
304
+ # Alias _Tire's_ instance methods in the top-level namespace of the model,
305
+ # unless there's a conflict with existing method
306
+ InstanceMethodsProxy::INTERFACE.each do |method|
307
+ base.class_eval <<-"end;", __FILE__, __LINE__ unless base.instance_methods.map(&:to_sym).include?(method.to_sym)
308
+ def #{method}(*args, &block) # def to_indexed_json(*args, &block)
309
+ tire.__send__(#{method.inspect}, *args, &block) # tire.__send__(:to_indexed_json, *args, &block)
310
+ end # end
311
+ end;
312
+ end
313
+
314
+ # Include the `load` functionality in Results::Item
315
+ #
316
+ Results::Item.send :include, Loader
317
+ end
318
+
319
+ end
320
+
321
+ end
322
+ end