Skip to content

Commit 1d5f8b1

Browse files
committed
Use Singleton pattern for Repository
1 parent b65f605 commit 1d5f8b1

21 files changed

+1774
-469
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

+4
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ source 'https://rubygems.org'
22

33
# Specify your gem's dependencies in elasticsearch-persistence.gemspec
44
gemspec
5+
6+
group :development, :testing do
7+
gem 'rspec'
8+
end

elasticsearch-persistence/Rakefile

+5-2
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ task :test => 'test:unit'
77
# ----- Test tasks ------------------------------------------------------------
88

99
require 'rake/testtask'
10+
require 'rspec/core/rake_task'
11+
1012
namespace :test do
13+
1114
Rake::TestTask.new(:unit) do |test|
1215
test.libs << 'lib' << 'test'
1316
test.test_files = FileList["test/unit/**/*_test.rb"]
1417
test.verbose = false
1518
test.warning = false
1619
end
1720

21+
RSpec::Core::RakeTask.new(:spec)
1822
Rake::TestTask.new(:integration) do |test|
19-
test.libs << 'lib' << 'test'
20-
test.test_files = FileList["test/integration/**/*_test.rb"]
2123
test.verbose = false
2224
test.warning = false
25+
test.deps = [ :spec ]
2326
end
2427

2528
Rake::TestTask.new(:all) do |test|

elasticsearch-persistence/elasticsearch-persistence.gemspec

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ Gem::Specification.new do |s|
2323

2424
s.required_ruby_version = ">= 1.9.3"
2525

26-
s.add_dependency "elasticsearch", '~> 5'
27-
s.add_dependency "elasticsearch-model", '~> 5'
26+
s.add_dependency "elasticsearch", '~> 6'
27+
s.add_dependency "elasticsearch-model", '>= 5'
2828
s.add_dependency "activesupport", '> 4'
2929
s.add_dependency "activemodel", '> 4'
3030
s.add_dependency "hashie"
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.
Original file line numberDiff line numberDiff line change
@@ -1,77 +1 @@
1-
module Elasticsearch
2-
module Persistence
3-
4-
# Delegate methods to the repository (acting as a gateway)
5-
#
6-
module GatewayDelegation
7-
def method_missing(method_name, *arguments, &block)
8-
gateway.respond_to?(method_name) ? gateway.__send__(method_name, *arguments, &block) : super
9-
end
10-
11-
def respond_to?(method_name, include_private=false)
12-
gateway.respond_to?(method_name) || super
13-
end
14-
15-
def respond_to_missing?(method_name, *)
16-
gateway.respond_to?(method_name) || super
17-
end
18-
end
19-
20-
# When included, creates an instance of the {Repository::Class Repository} class as a "gateway"
21-
#
22-
# @example Include the repository in a custom class
23-
#
24-
# require 'elasticsearch/persistence'
25-
#
26-
# class MyRepository
27-
# include Elasticsearch::Persistence::Repository
28-
# end
29-
#
30-
module Repository
31-
def self.included(base)
32-
gateway = Elasticsearch::Persistence::Repository::Class.new host: base
33-
34-
# Define the instance level gateway
35-
#
36-
base.class_eval do
37-
define_method :gateway do
38-
@gateway ||= gateway
39-
end
40-
41-
include GatewayDelegation
42-
end
43-
44-
# Define the class level gateway
45-
#
46-
(class << base; self; end).class_eval do
47-
define_method :gateway do |&block|
48-
@gateway ||= gateway
49-
@gateway.instance_eval(&block) if block
50-
@gateway
51-
end
52-
53-
include GatewayDelegation
54-
end
55-
56-
# Catch repository methods (such as `serialize` and others) defined in the receiving class,
57-
# and overload the default definition in the gateway
58-
#
59-
def base.method_added(name)
60-
if :gateway != name && respond_to?(:gateway) && (gateway.public_methods - Object.public_methods).include?(name)
61-
gateway.define_singleton_method(name, self.new.method(name).to_proc)
62-
end
63-
end
64-
end
65-
66-
# Shortcut method to allow concise repository initialization
67-
#
68-
# @example Create a new default repository
69-
#
70-
# repository = Elasticsearch::Persistence::Repository.new
71-
#
72-
def new(options={}, &block)
73-
Elasticsearch::Persistence::Repository::Class.new( {index: 'repository'}.merge(options), &block )
74-
end; module_function :new
75-
end
76-
end
77-
end
1+
require 'elasticsearch/persistence/repository/base'

0 commit comments

Comments
 (0)