named_instances 0.0.4 → 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/named_instances.rb +48 -51
- data/lib/named_instances/version.rb +1 -1
- metadata +3 -4
- data/lib/rails/named_instances.rb +0 -7
data/lib/named_instances.rb
CHANGED
@@ -1,72 +1,69 @@
|
|
1
|
+
require "rails"
|
2
|
+
|
1
3
|
module NamedInstances
|
2
4
|
|
3
|
-
|
4
|
-
|
5
|
+
class Railtie < Rails::Railtie
|
6
|
+
initializer "named_instances.initialize" do |app|
|
7
|
+
ActiveRecord::Base.send :extend, NamedInstances
|
8
|
+
end
|
5
9
|
end
|
6
10
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
@named_instances_name_method = name_method
|
12
|
-
@find_options = options[:find_options]
|
11
|
+
def has_named_instances(name_method = :value, options={})
|
12
|
+
@named_instances_loaded = false
|
13
|
+
@named_instances_name_method = name_method
|
14
|
+
@find_options = options[:find_options]
|
13
15
|
|
14
|
-
|
15
|
-
|
16
|
-
self.class.load_named_instances
|
17
|
-
end
|
18
|
-
end
|
16
|
+
after_save { self.class.load_named_instances }
|
17
|
+
end
|
19
18
|
|
20
|
-
|
21
|
-
|
22
|
-
|
19
|
+
def named_instances_loaded?
|
20
|
+
!! @named_instances_loaded
|
21
|
+
end
|
23
22
|
|
24
|
-
|
25
|
-
|
26
|
-
|
23
|
+
def name_method
|
24
|
+
@named_instances_name_method
|
25
|
+
end
|
27
26
|
|
28
|
-
|
29
|
-
|
27
|
+
def get *names
|
28
|
+
load_named_instances unless @named_instances_loaded
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
name = names.map {|n| n.to_s }.join "_"
|
31
|
+
key_name = "#{named_instances_prefix}_#{name}"
|
32
|
+
result = Rails.cache.read(key_name)
|
34
33
|
|
35
|
-
|
36
|
-
|
34
|
+
# gets are hard-coded and should always return something valid from the cache
|
35
|
+
raise(ArgumentError, "NamedInstance does not exist: #{key_name}") unless result
|
37
36
|
|
38
|
-
|
39
|
-
|
37
|
+
result
|
38
|
+
end
|
40
39
|
|
41
|
-
|
42
|
-
|
43
|
-
|
40
|
+
def named_instances_normalize name
|
41
|
+
name.downcase.gsub(/[^a-z0-9]+/, '_').gsub(/_$/, '')
|
42
|
+
end
|
44
43
|
|
45
|
-
|
46
|
-
|
47
|
-
|
44
|
+
def named_instances_prefix
|
45
|
+
"named_instances_#{self}"
|
46
|
+
end
|
48
47
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
end
|
48
|
+
def load_named_instances
|
49
|
+
begin
|
50
|
+
self.all(@find_options).each do |instance|
|
51
|
+
if (@named_instances_name_method.is_a? String) or (@named_instances_name_method.is_a? Symbol)
|
52
|
+
field_name = named_instances_normalize instance.send(@named_instances_name_method)
|
53
|
+
key_name = "#{named_instances_prefix}_#{field_name}"
|
54
|
+
else
|
55
|
+
key_name = @named_instances_name_method.inject(named_instances_prefix) do |key_name, name_method|
|
56
|
+
field_name = named_instances_normalize instance.send(name_method)
|
57
|
+
key_name + "_#{field_name}"
|
60
58
|
end
|
61
|
-
|
62
|
-
Rails.cache.write(key_name, instance)
|
63
59
|
end
|
64
60
|
|
65
|
-
|
66
|
-
rescue Exception => e
|
67
|
-
Rails.logger.info "Named Instances: exception ignored, class = #{self}, error = #{e.inspect}"
|
61
|
+
Rails.cache.write(key_name, instance)
|
68
62
|
end
|
69
|
-
end
|
70
63
|
|
64
|
+
@named_instances_loaded = true
|
65
|
+
rescue Exception => e
|
66
|
+
Rails.logger.error "Named Instances: exception ignored, class = #{self}, error = #{e.inspect}"
|
67
|
+
end
|
71
68
|
end
|
72
69
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: named_instances
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 27
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
+
- 1
|
8
9
|
- 0
|
9
|
-
|
10
|
-
version: 0.0.4
|
10
|
+
version: 0.1.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Jason Dew
|
@@ -31,7 +31,6 @@ extra_rdoc_files: []
|
|
31
31
|
files:
|
32
32
|
- lib/named_instances/version.rb
|
33
33
|
- lib/named_instances.rb
|
34
|
-
- lib/rails/named_instances.rb
|
35
34
|
- LICENSE
|
36
35
|
has_rdoc: true
|
37
36
|
homepage: http://github.com/jasondew/named_instances
|