sqlcached_client 0.2.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 91ca1306befc4783825e8041edfad314f1d1cae0
4
- data.tar.gz: af0ed719bd8e6f8739a4e48590c559eef771dd54
3
+ metadata.gz: b748c4321404ba53adfea1f54149b8940e639b3e
4
+ data.tar.gz: 1aa280da099b3775b1f1573f3ff1f1a22d9db614
5
5
  SHA512:
6
- metadata.gz: e7b476e0945653b5b1c494876520d7b4424c156583575a1e53bbf519604f3e846bde5ea580a24dba93894b874044d6659b219856b703dab6e4fed5e9d00bedd3
7
- data.tar.gz: f94bfea52a4113c3a50adf986c6f843f1f7663d9f3760f19b73ef6707b69abbbdc592c9df93ca99f4d1e62718c4bf181fd1a563b0addd2481b07d59b6af359a7
6
+ metadata.gz: d19236cb3ede1bde65802ea4d6453bd4624a16c5d629a1bb91f65b59273701e9de81c66245ed947d40e86188ee43c9482c5f61ef397b7c9cb01786bcb316aed6
7
+ data.tar.gz: a12f68121d759e630c16873967b7413d1c2b5135e556a33dd49f366c365a4f0ac12663682dc86757c805b7c0dd161309d923416e5a628228ec9a425b1ea0dbc1
@@ -18,13 +18,33 @@ module SqlcachedClient
18
18
 
19
19
 
20
20
  class << self
21
- attr_reader :query_id
21
+
22
+ # Sets a prefix for each entity name, or returns the value previously set
23
+ # if no parameter is given
24
+ def entity_namespace(value = nil)
25
+ if value.nil?
26
+ @entity_namespace || try_ancestor(:entity_namespace)
27
+ else
28
+ @entity_namespace = value
29
+ end
30
+ end
22
31
 
23
32
  # Sets the name of this entity
33
+ # @param value [String]
24
34
  def entity_name(value)
25
35
  @query_id = value
26
36
  end
27
37
 
38
+ # Returns the identifier of the query template of the current entity
39
+ # @return [String]
40
+ def query_id
41
+ if prefix = entity_namespace.presence
42
+ "#{prefix}::#{@query_id}"
43
+ else
44
+ @query_id
45
+ end
46
+ end
47
+
28
48
  # Sets the query of this entity if a parameter is provided, otherwise
29
49
  # returns the value previously set.
30
50
  def query(*args, &block)
@@ -49,12 +69,7 @@ module SqlcachedClient
49
69
  # otherwise returns the server object previously set.
50
70
  def server(config = nil)
51
71
  if config.nil?
52
- @server ||
53
- if superclass = ancestors[1..-1].detect { |a| a.respond_to?(:server) }
54
- superclass.server
55
- else
56
- nil
57
- end
72
+ @server || try_ancestor(:server)
58
73
  else
59
74
  @server =
60
75
  if config.is_a?(Hash)
@@ -193,7 +208,7 @@ module SqlcachedClient
193
208
  def cache(seconds = nil)
194
209
  if seconds.nil?
195
210
  @cache ||
196
- if superclass = ancestors[1..-1].detect { |a| a.respond_to?(:cache) }
211
+ if superclass = ancestors_lookup(:cache)
197
212
  superclass.cache
198
213
  else
199
214
  true
@@ -275,6 +290,22 @@ module SqlcachedClient
275
290
  @registered_associations ||= []
276
291
  @registered_associations << association_struct
277
292
  end
293
+
294
+ # Finds the first ancestor that responds to the given method
295
+ # @param method_name [Symbol] the method to find
296
+ # @return [Class]
297
+ def ancestors_lookup(method_name)
298
+ ancestors[1..-1].detect { |a| a.respond_to?(method_name) }
299
+ end
300
+
301
+ # Calls the given method on the first ancestor that responds to the
302
+ # method
303
+ # @param method_name [Symbol]
304
+ # @return nil if no ancestor responds to the method, or the value
305
+ # returned by 'method_name'
306
+ def try_ancestor(method_name)
307
+ ancestors_lookup(method_name).try(method_name)
308
+ end
278
309
  end # class << self
279
310
 
280
311
  # Define the readers for the attribute names specified
@@ -1,3 +1,3 @@
1
1
  module SqlcachedClient
2
- VERSION = "0.2.3"
2
+ VERSION = "1.0.0"
3
3
  end
@@ -29,6 +29,27 @@ describe SqlcachedClient::Entity do
29
29
  end
30
30
 
31
31
 
32
+ describe :entity_namespace do
33
+ context "if a parameter is provided" do
34
+ it "sets @entity_namespace" do
35
+ entity_class = Class.new(SqlcachedClient::Entity) do
36
+ entity_namespace("foo")
37
+ end
38
+ expect(entity_class.instance_variable_get(:@entity_namespace)).to eq("foo")
39
+ end
40
+ end
41
+
42
+ context "if no parameter is given" do
43
+ it "returns the value previously set" do
44
+ entity_class = Class.new(SqlcachedClient::Entity) do
45
+ entity_namespace("foo")
46
+ end
47
+ expect(entity_class.entity_namespace).to eq("foo")
48
+ end
49
+ end
50
+ end
51
+
52
+
32
53
  describe :query do
33
54
  context "if a parameter is provided" do
34
55
  it "should set @query and strip spaces" do
@@ -50,6 +71,28 @@ describe SqlcachedClient::Entity do
50
71
  end
51
72
 
52
73
 
74
+ describe :query_id do
75
+ context "if an entity_namespace was set" do
76
+ it "is entity_namespace::entity_name" do
77
+ entity_class = Class.new(SqlcachedClient::Entity) do
78
+ entity_namespace("foo")
79
+ entity_name("bar")
80
+ end
81
+ expect(entity_class.query_id).to eq("foo::bar")
82
+ end
83
+ end
84
+
85
+ context "if no entity_namespace was set" do
86
+ it "is entity_name" do
87
+ entity_class = Class.new(SqlcachedClient::Entity) do
88
+ entity_name("bar")
89
+ end
90
+ expect(entity_class.query_id).to eq("bar")
91
+ end
92
+ end
93
+ end
94
+
95
+
53
96
  describe :server do
54
97
  context "if a parameter is provided" do
55
98
  it "should set @server" do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sqlcached_client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Maestroni