Skip to content

Commit e5d9ffb

Browse files
committedMay 27, 2014
[STORE] Added the Naming module
Provides getting Ruby class from Elasticsearch type and vice versa, ID from the document (Hash), as well as setting the `klass` for the whole Repository instance.
1 parent 48d7132 commit e5d9ffb

File tree

5 files changed

+101
-0
lines changed

5 files changed

+101
-0
lines changed
 

‎elasticsearch-persistence/elasticsearch-persistence.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Gem::Specification.new do |s|
2222
s.rdoc_options = [ "--charset=UTF-8" ]
2323

2424
s.add_dependency "elasticsearch", '> 0.4'
25+
s.add_dependency "active_support"
2526

2627
s.add_development_dependency "bundler", "~> 1.5"
2728
s.add_development_dependency "rake"

‎elasticsearch-persistence/lib/elasticsearch/persistence.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
require 'elasticsearch'
22

3+
require 'active_support/inflector'
4+
35
require 'elasticsearch/persistence/version'
46

57
require 'elasticsearch/persistence/client'
8+
require 'elasticsearch/persistence/repository/naming'
69
require 'elasticsearch/persistence/repository'
710

811
require 'elasticsearch/persistence/repository/class'

‎elasticsearch-persistence/lib/elasticsearch/persistence/repository.rb

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ module Persistence
33

44
module Repository
55
include Elasticsearch::Persistence::Client
6+
include Elasticsearch::Persistence::Repository::Naming
67

78
def new(options={}, &block)
89
Elasticsearch::Persistence::Repository::Class.new options, &block
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module Elasticsearch
2+
module Persistence
3+
module Repository
4+
5+
module Naming
6+
def klass
7+
@klass
8+
end
9+
10+
def klass=klass
11+
@klass = klass
12+
end
13+
14+
def __get_klass_from_type(type)
15+
klass = type.classify
16+
klass.constantize
17+
rescue NameError => e
18+
raise NameError, "Attempted to get class '#{klass}' from the '#{type}' type, but no such class can be found."
19+
end
20+
21+
def __get_type_from_class(klass)
22+
klass.to_s.underscore
23+
end
24+
25+
def __get_id_from_document(document)
26+
document[:id] || document['id'] || document[:_id] || document['_id']
27+
end
28+
end
29+
30+
end
31+
end
32+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
require 'test_helper'
2+
3+
class Elasticsearch::Persistence::RepositoryNamingTest < Test::Unit::TestCase
4+
context "The repository naming" do
5+
# Fake class for the naming tests
6+
class ::Foobar; end
7+
class ::FooBar; end
8+
module ::Foo; class Bar; end; end
9+
10+
setup do
11+
@shoulda_subject = Class.new() { include Elasticsearch::Persistence::Repository::Naming }.new
12+
end
13+
14+
context "get Ruby class from the Elasticsearch type" do
15+
should "get a simple class" do
16+
assert_equal Foobar, subject.__get_klass_from_type('foobar')
17+
end
18+
should "get a camelcased class" do
19+
assert_equal FooBar, subject.__get_klass_from_type('foo_bar')
20+
end
21+
should "get a namespaced class" do
22+
assert_equal Foo::Bar, subject.__get_klass_from_type('foo/bar')
23+
end
24+
should "re-raise a NameError exception" do
25+
assert_raise NameError do
26+
subject.__get_klass_from_type('foobarbazbam')
27+
end
28+
end
29+
end
30+
31+
context "get Elasticsearch type from the Ruby class" do
32+
should "encode a simple class" do
33+
assert_equal 'foobar', subject.__get_type_from_class(Foobar)
34+
end
35+
should "encode a camelcased class" do
36+
assert_equal 'foo_bar', subject.__get_type_from_class(FooBar)
37+
end
38+
should "encode a namespaced class" do
39+
assert_equal 'foo/bar', subject.__get_type_from_class(Foo::Bar)
40+
end
41+
end
42+
43+
context "get an ID from the document" do
44+
should "get an ID from Hash" do
45+
assert_equal 1, subject.__get_id_from_document(id: 1)
46+
assert_equal 1, subject.__get_id_from_document(_id: 1)
47+
assert_equal 1, subject.__get_id_from_document('id' => 1)
48+
assert_equal 1, subject.__get_id_from_document('_id' => 1)
49+
end
50+
end
51+
52+
context " document class name" do
53+
should "be nil by default" do
54+
assert_nil subject.klass
55+
end
56+
57+
should "be settable" do
58+
subject.klass = Foobar
59+
assert_equal Foobar, subject.klass
60+
end
61+
end
62+
63+
end
64+
end

0 commit comments

Comments
 (0)
Please sign in to comment.