Skip to content

Commit 44c9952

Browse files
committed
[STORE] Added the method_added hook to allow defining gateway methods directly in the class
Instead of calling the `gateway` method with a block, to redefine the serialize/deserialize methods: class NoteRepository gateway do def serialize(document) Hash[document.to_hash.map() { |k,v| v.upcase! if k == :title; [k,v] }] end def deserialize(document) MyNote.new ActiveSupport::HashWithIndifferentAccess.new(document['_source']).deep_symbolize_keys end end end Define them directly in the class, and they will be intercepted by the hook, and (re)defined directly on the gateway: class NoteRepository def serialize(document) Hash[document.to_hash.map() { |k,v| v.upcase! if k == :title; [k,v] }] end def deserialize(document) MyNote.new ActiveSupport::HashWithIndifferentAccess.new(document['_source']).deep_symbolize_keys end end See: http://www.ruby-doc.org/core-2.1.1/Module.html#method-i-method_added (COMITTED WITH FINGERS CROSSED :)
1 parent cbbff17 commit 44c9952

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

Diff for: elasticsearch-persistence/lib/elasticsearch/persistence/repository.rb

+6
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ def self.included(base)
3535

3636
include GatewayDelegation
3737
end
38+
39+
def base.method_added(name)
40+
if :gateway != name && respond_to?(:gateway) && (gateway.public_methods - Object.public_methods).include?(name)
41+
gateway.define_singleton_method(name, self.new.method(name).to_proc)
42+
end
43+
end
3844
end
3945

4046
def new(options={}, &block)

Diff for: elasticsearch-persistence/test/unit/repository_module_test.rb

+27-3
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,8 @@ class DummyRepository
8686
assert_equal 'snowball', repository.mappings.to_hash[:my_dummy_model][:properties][:title][:analyzer]
8787
end
8888

89-
should "allow to define gateway methods in the class definition" do
90-
class DummyRepository
89+
should "allow to define gateway methods in the class definition via block passed to the gateway method" do
90+
class DummyRepositoryWithGatewaySerialize
9191
include Elasticsearch::Persistence::Repository
9292

9393
gateway do
@@ -97,7 +97,7 @@ def serialize(document)
9797
end
9898
end
9999

100-
repository = DummyRepository.new
100+
repository = DummyRepositoryWithGatewaySerialize.new
101101
repository.client.transport.logger = Logger.new(STDERR)
102102

103103
client = mock
@@ -113,6 +113,30 @@ def serialize(document)
113113
end
114114
end
115115

116+
should "allow to define gateway methods in the class definition via regular method definition" do
117+
class DummyRepositoryWithDirectSerialize
118+
include Elasticsearch::Persistence::Repository
119+
120+
def serialize(document)
121+
'FAKE IN CLASS!'
122+
end
123+
end
124+
125+
repository = DummyRepositoryWithDirectSerialize.new
126+
repository.client.transport.logger = Logger.new(STDERR)
127+
128+
client = mock
129+
client.expects(:index).with do |arguments|
130+
assert_equal('xxx', arguments[:id])
131+
assert_equal('FAKE IN CLASS!', arguments[:body])
132+
end
133+
repository.gateway.expects(:client).returns(client)
134+
135+
repository.gateway.expects(:__get_id_from_document).returns('xxx')
136+
137+
repository.save( id: '123', foo: 'bar' )
138+
end
139+
116140
should "configure the index name in the shortcut initializer" do
117141
assert_equal 'repository', Elasticsearch::Persistence::Repository.new.index_name
118142
end

0 commit comments

Comments
 (0)