Skip to content

[STORE] Use Singleton pattern for Repository #822

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions elasticsearch-persistence/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--tty
--colour
5 changes: 5 additions & 0 deletions elasticsearch-persistence/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,8 @@ source 'https://rubygems.org'

# Specify your gem's dependencies in elasticsearch-persistence.gemspec
gemspec

group :development, :testing do
gem 'rspec'
gem 'pry-nav'
end
9 changes: 6 additions & 3 deletions elasticsearch-persistence/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,22 @@ task :test => 'test:unit'
# ----- Test tasks ------------------------------------------------------------

require 'rake/testtask'
require 'rspec/core/rake_task'

namespace :test do

Rake::TestTask.new(:unit) do |test|
test.libs << 'lib' << 'test'
test.test_files = FileList["test/unit/**/*_test.rb"]
#test.test_files = FileList["test/unit/**/*_test.rb"]
test.verbose = false
test.warning = false
end

RSpec::Core::RakeTask.new(:spec)
Rake::TestTask.new(:integration) do |test|
test.libs << 'lib' << 'test'
test.test_files = FileList["test/integration/**/*_test.rb"]
test.verbose = false
test.warning = false
test.deps = [ :spec ]
end

Rake::TestTask.new(:all) do |test|
Expand Down
4 changes: 2 additions & 2 deletions elasticsearch-persistence/elasticsearch-persistence.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Gem::Specification.new do |s|

s.required_ruby_version = ">= 1.9.3"

s.add_dependency "elasticsearch", '~> 5'
s.add_dependency "elasticsearch-model", '~> 5'
s.add_dependency "elasticsearch", '>= 5'
s.add_dependency "elasticsearch-model", '>= 5'
s.add_dependency "activesupport", '> 4'
s.add_dependency "activemodel", '> 4'
s.add_dependency "hashie"
Expand Down
112 changes: 2 additions & 110 deletions elasticsearch-persistence/lib/elasticsearch/persistence.rb
Original file line number Diff line number Diff line change
@@ -1,116 +1,8 @@
require 'hashie/mash'

require 'elasticsearch'

require 'elasticsearch/model/hash_wrapper'
require 'elasticsearch/model/indexing'
require 'elasticsearch/model/searching'

require 'active_support/inflector'
require 'elasticsearch/model'

require 'elasticsearch/persistence/version'

require 'elasticsearch/persistence/client'
require 'elasticsearch/persistence/repository/response/results'
require 'elasticsearch/persistence/repository/naming'
require 'elasticsearch/persistence/repository/serialize'
require 'elasticsearch/persistence/repository/store'
require 'elasticsearch/persistence/repository/find'
require 'elasticsearch/persistence/repository/search'
require 'elasticsearch/persistence/repository/class'
require 'elasticsearch/persistence/repository'

module Elasticsearch

# Persistence for Ruby domain objects and models in Elasticsearch
# ===============================================================
#
# `Elasticsearch::Persistence` contains modules for storing and retrieving Ruby domain objects and models
# in Elasticsearch.
#
# == Repository
#
# The repository patterns allows to store and retrieve Ruby objects in Elasticsearch.
#
# require 'elasticsearch/persistence'
#
# class Note
# def to_hash; {foo: 'bar'}; end
# end
#
# repository = Elasticsearch::Persistence::Repository.new
#
# repository.save Note.new
# # => {"_index"=>"repository", "_type"=>"note", "_id"=>"mY108X9mSHajxIy2rzH2CA", ...}
#
# Customize your repository by including the main module in a Ruby class
# class MyRepository
# include Elasticsearch::Persistence::Repository
#
# index 'my_notes'
# klass Note
#
# client Elasticsearch::Client.new log: true
# end
#
# repository = MyRepository.new
#
# repository.save Note.new
# # 2014-04-04 22:15:25 +0200: POST http://localhost:9200/my_notes/note [status:201, request:0.009s, query:n/a]
# # 2014-04-04 22:15:25 +0200: > {"foo":"bar"}
# # 2014-04-04 22:15:25 +0200: < {"_index":"my_notes","_type":"note","_id":"-d28yXLFSlusnTxb13WIZQ", ...}
#
# == Model
#
# The active record pattern allows to use the interface familiar from ActiveRecord models:
#
# require 'elasticsearch/persistence'
#
# class Article
# attribute :title, String, mapping: { analyzer: 'snowball' }
# end
#
# article = Article.new id: 1, title: 'Test'
# article.save
#
# Article.find(1)
#
# article.update_attributes title: 'Update'
#
# article.destroy
#
module Persistence

# :nodoc:
module ClassMethods

# Get or set the default client for all repositories and models
#
# @example Set and configure the default client
#
# Elasticsearch::Persistence.client Elasticsearch::Client.new host: 'http://localhost:9200', tracer: true
#
# @example Perform an API request through the client
#
# Elasticsearch::Persistence.client.cluster.health
# # => { "cluster_name" => "elasticsearch" ... }
#
def client client=nil
@client = client || @client || Elasticsearch::Client.new
end

# Set the default client for all repositories and models
#
# @example Set and configure the default client
#
# Elasticsearch::Persistence.client = Elasticsearch::Client.new host: 'http://localhost:9200', tracer: true
# => #<Elasticsearch::Transport::Client:0x007f96a6dd0d80 @transport=... >
#
def client=(client)
@client = client
end
end

extend ClassMethods
end
end
require 'elasticsearch/persistence/repository/response/results'
51 changes: 0 additions & 51 deletions elasticsearch-persistence/lib/elasticsearch/persistence/client.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,77 +1 @@
module Elasticsearch
module Persistence

# Delegate methods to the repository (acting as a gateway)
#
module GatewayDelegation
def method_missing(method_name, *arguments, &block)
gateway.respond_to?(method_name) ? gateway.__send__(method_name, *arguments, &block) : super
end

def respond_to?(method_name, include_private=false)
gateway.respond_to?(method_name) || super
end

def respond_to_missing?(method_name, *)
gateway.respond_to?(method_name) || super
end
end

# When included, creates an instance of the {Repository::Class Repository} class as a "gateway"
#
# @example Include the repository in a custom class
#
# require 'elasticsearch/persistence'
#
# class MyRepository
# include Elasticsearch::Persistence::Repository
# end
#
module Repository
def self.included(base)
gateway = Elasticsearch::Persistence::Repository::Class.new host: base

# Define the instance level gateway
#
base.class_eval do
define_method :gateway do
@gateway ||= gateway
end

include GatewayDelegation
end

# Define the class level gateway
#
(class << base; self; end).class_eval do
define_method :gateway do |&block|
@gateway ||= gateway
@gateway.instance_eval(&block) if block
@gateway
end

include GatewayDelegation
end

# Catch repository methods (such as `serialize` and others) defined in the receiving class,
# and overload the default definition in the gateway
#
def base.method_added(name)
if :gateway != name && respond_to?(:gateway) && (gateway.public_methods - Object.public_methods).include?(name)
gateway.define_singleton_method(name, self.new.method(name).to_proc)
end
end
end

# Shortcut method to allow concise repository initialization
#
# @example Create a new default repository
#
# repository = Elasticsearch::Persistence::Repository.new
#
def new(options={}, &block)
Elasticsearch::Persistence::Repository::Class.new( {index: 'repository'}.merge(options), &block )
end; module_function :new
end
end
end
require 'elasticsearch/persistence/repository/base'
Loading