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.
@@ -1,72 +1,69 @@
1
+ require "rails"
2
+
1
3
  module NamedInstances
2
4
 
3
- def self.included base
4
- base.extend ClassMethods
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
- module ClassMethods
8
-
9
- def has_named_instances(name_method = :value, options={})
10
- @named_instances_loaded = false
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
- after_save do
15
- Rails.logger.info("Reloading #{self.class} named instances because of a save")
16
- self.class.load_named_instances
17
- end
18
- end
16
+ after_save { self.class.load_named_instances }
17
+ end
19
18
 
20
- def named_instances_loaded?
21
- !! @named_instances_loaded
22
- end
19
+ def named_instances_loaded?
20
+ !! @named_instances_loaded
21
+ end
23
22
 
24
- def name_method
25
- @named_instances_name_method
26
- end
23
+ def name_method
24
+ @named_instances_name_method
25
+ end
27
26
 
28
- def get *names
29
- load_named_instances unless @named_instances_loaded
27
+ def get *names
28
+ load_named_instances unless @named_instances_loaded
30
29
 
31
- name = names.map {|n| n.to_s }.join "_"
32
- key_name = "#{named_instances_prefix}_#{name}"
33
- result = Rails.cache.read(key_name)
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
- # gets are hard-coded and should always return something valid from the cache
36
- raise(ArgumentError, "NamedInstance does not exist: #{key_name}") unless result
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
- result
39
- end
37
+ result
38
+ end
40
39
 
41
- def named_instances_normalize name
42
- name.downcase.gsub(/[^a-z0-9]+/, '_').gsub(/_$/, '')
43
- end
40
+ def named_instances_normalize name
41
+ name.downcase.gsub(/[^a-z0-9]+/, '_').gsub(/_$/, '')
42
+ end
44
43
 
45
- def named_instances_prefix
46
- "named_instances_#{self}"
47
- end
44
+ def named_instances_prefix
45
+ "named_instances_#{self}"
46
+ end
48
47
 
49
- def load_named_instances
50
- begin
51
- all(@find_options).each do |instance|
52
- if (@named_instances_name_method.is_a? String) or (@named_instances_name_method.is_a? Symbol)
53
- field_name = named_instances_normalize instance.send(@named_instances_name_method)
54
- key_name = "#{named_instances_prefix}_#{field_name}"
55
- else
56
- key_name = @named_instances_name_method.inject(named_instances_prefix) do |key_name, name_method|
57
- field_name = named_instances_normalize instance.send(name_method)
58
- key_name + "_#{field_name}"
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
- @named_instances_loaded = true
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
@@ -1,3 +1,3 @@
1
1
  module NamedInstances
2
- VERSION = "0.0.4"
2
+ VERSION = "0.1.0"
3
3
  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: 23
4
+ hash: 27
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
+ - 1
8
9
  - 0
9
- - 4
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
@@ -1,7 +0,0 @@
1
- module NamedInstances
2
- class Railtie < Rails::Railtie
3
- initializer "named_instances.initialize" do |app|
4
- ActiveRecord::Base.send :include, NamedInstances
5
- end
6
- end
7
- end