Skip to content

Commit 8acd157

Browse files
committed
[STORE] Refactor Repository as mixin (elastic#824)
* [STORE] Refactor Repository * [STORE] Add more tests and update documentation * [STORE] Remove #with method * [STORE] Raise NotImplementedError if administrative index methods are called on a Repository class * [STORE] Put class-level index admin methods into a DSL module, to be included separately * [STORE] Fix documentation and minor typos in tests * [STORE] Add one more test for ArgumentError on #search * [STORE] Remove test unit files and define only test 'integration' and 'all' rake tasks
1 parent e051a87 commit 8acd157

36 files changed

+1922
-2112
lines changed

elasticsearch-persistence/.rspec

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
--tty
2+
--colour

elasticsearch-persistence/Gemfile

+5
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,8 @@ gemspec
66
gem 'elasticsearch-model', :path => File.expand_path("../../elasticsearch-model", __FILE__), :require => false
77

88
gem 'virtus'
9+
10+
group :development, :testing do
11+
gem 'rspec'
12+
gem 'pry-nav'
13+
end

elasticsearch-persistence/Rakefile

+8-8
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,24 @@ task :test => 'test:unit'
77
# ----- Test tasks ------------------------------------------------------------
88

99
require 'rake/testtask'
10+
require 'rspec/core/rake_task'
11+
1012
namespace :test do
13+
14+
RSpec::Core::RakeTask.new(:spec)
1115
Rake::TestTask.new(:unit) do |test|
12-
test.libs << 'lib' << 'test'
13-
test.test_files = FileList["test/unit/**/*_test.rb"]
14-
test.verbose = false
15-
test.warning = false
1616
end
1717

1818
Rake::TestTask.new(:integration) do |test|
19-
test.libs << 'lib' << 'test'
20-
test.test_files = FileList["test/integration/**/*_test.rb"]
2119
test.verbose = false
2220
test.warning = false
21+
test.deps = [ :spec ]
2322
end
2423

2524
Rake::TestTask.new(:all) do |test|
26-
test.libs << 'lib' << 'test'
27-
test.test_files = FileList["test/unit/**/*_test.rb", "test/integration/**/*_test.rb"]
25+
test.verbose = false
26+
test.warning = false
27+
test.deps = [ :spec ]
2828
end
2929
end
3030

Original file line numberDiff line numberDiff line change
@@ -1,116 +1,8 @@
11
require 'hashie/mash'
22

33
require 'elasticsearch'
4-
5-
require 'elasticsearch/model/hash_wrapper'
6-
require 'elasticsearch/model/indexing'
7-
require 'elasticsearch/model/searching'
8-
9-
require 'active_support/inflector'
4+
require 'elasticsearch/model'
105

116
require 'elasticsearch/persistence/version'
12-
13-
require 'elasticsearch/persistence/client'
14-
require 'elasticsearch/persistence/repository/response/results'
15-
require 'elasticsearch/persistence/repository/naming'
16-
require 'elasticsearch/persistence/repository/serialize'
17-
require 'elasticsearch/persistence/repository/store'
18-
require 'elasticsearch/persistence/repository/find'
19-
require 'elasticsearch/persistence/repository/search'
20-
require 'elasticsearch/persistence/repository/class'
217
require 'elasticsearch/persistence/repository'
22-
23-
module Elasticsearch
24-
25-
# Persistence for Ruby domain objects and models in Elasticsearch
26-
# ===============================================================
27-
#
28-
# `Elasticsearch::Persistence` contains modules for storing and retrieving Ruby domain objects and models
29-
# in Elasticsearch.
30-
#
31-
# == Repository
32-
#
33-
# The repository patterns allows to store and retrieve Ruby objects in Elasticsearch.
34-
#
35-
# require 'elasticsearch/persistence'
36-
#
37-
# class Note
38-
# def to_hash; {foo: 'bar'}; end
39-
# end
40-
#
41-
# repository = Elasticsearch::Persistence::Repository.new
42-
#
43-
# repository.save Note.new
44-
# # => {"_index"=>"repository", "_type"=>"note", "_id"=>"mY108X9mSHajxIy2rzH2CA", ...}
45-
#
46-
# Customize your repository by including the main module in a Ruby class
47-
# class MyRepository
48-
# include Elasticsearch::Persistence::Repository
49-
#
50-
# index 'my_notes'
51-
# klass Note
52-
#
53-
# client Elasticsearch::Client.new log: true
54-
# end
55-
#
56-
# repository = MyRepository.new
57-
#
58-
# repository.save Note.new
59-
# # 2014-04-04 22:15:25 +0200: POST http://localhost:9200/my_notes/note [status:201, request:0.009s, query:n/a]
60-
# # 2014-04-04 22:15:25 +0200: > {"foo":"bar"}
61-
# # 2014-04-04 22:15:25 +0200: < {"_index":"my_notes","_type":"note","_id":"-d28yXLFSlusnTxb13WIZQ", ...}
62-
#
63-
# == Model
64-
#
65-
# The active record pattern allows to use the interface familiar from ActiveRecord models:
66-
#
67-
# require 'elasticsearch/persistence'
68-
#
69-
# class Article
70-
# attribute :title, String, mapping: { analyzer: 'snowball' }
71-
# end
72-
#
73-
# article = Article.new id: 1, title: 'Test'
74-
# article.save
75-
#
76-
# Article.find(1)
77-
#
78-
# article.update_attributes title: 'Update'
79-
#
80-
# article.destroy
81-
#
82-
module Persistence
83-
84-
# :nodoc:
85-
module ClassMethods
86-
87-
# Get or set the default client for all repositories and models
88-
#
89-
# @example Set and configure the default client
90-
#
91-
# Elasticsearch::Persistence.client Elasticsearch::Client.new host: 'http://localhost:9200', tracer: true
92-
#
93-
# @example Perform an API request through the client
94-
#
95-
# Elasticsearch::Persistence.client.cluster.health
96-
# # => { "cluster_name" => "elasticsearch" ... }
97-
#
98-
def client client=nil
99-
@client = client || @client || Elasticsearch::Client.new
100-
end
101-
102-
# Set the default client for all repositories and models
103-
#
104-
# @example Set and configure the default client
105-
#
106-
# Elasticsearch::Persistence.client = Elasticsearch::Client.new host: 'http://localhost:9200', tracer: true
107-
# => #<Elasticsearch::Transport::Client:0x007f96a6dd0d80 @transport=... >
108-
#
109-
def client=(client)
110-
@client = client
111-
end
112-
end
113-
114-
extend ClassMethods
115-
end
116-
end
8+
require 'elasticsearch/persistence/repository/response/results'

elasticsearch-persistence/lib/elasticsearch/persistence/client.rb

-51
This file was deleted.

0 commit comments

Comments
 (0)