named_instances 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.gitignore ADDED
@@ -0,0 +1,6 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
6
+ test/test.db
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Jason Dew
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,18 @@
1
+ = Named Instances
2
+
3
+ Provides support for named instances in Rails.
4
+
5
+ Quick usage:
6
+
7
+ class Diagnosis < ActiveRecord::Base
8
+ has_named_instances :name
9
+ end
10
+
11
+ >> Diagnosis.get(:diabetes_mellitus)
12
+ => #<Diagnosis id: 52, name: "Diabetes Mellitus", created_at: "2009-10-07 15:41:50", updated_at: "2009-10-07 15:41:50">
13
+
14
+ Patches welcome, enjoy!
15
+
16
+ == Copyright
17
+
18
+ Copyright (c) 2009 Jason Dew. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "named_instances"
8
+ gem.summary = %Q{Named instances for ActiveRecord}
9
+ gem.description = %Q{Give cached access to ActiveRecord models for quicker and more readable data-based logic.}
10
+ gem.email = "jason.dew@gmail.com"
11
+ gem.homepage = "http://github.com/jasondew/named_instances"
12
+ gem.authors = ["Jason Dew"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ gem.add_development_dependency "mocha"
15
+ gem.add_development_dependency "activesupport"
16
+ end
17
+ Jeweler::GemcutterTasks.new
18
+ rescue LoadError
19
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
20
+ end
21
+
22
+ require 'rake/testtask'
23
+ Rake::TestTask.new(:test) do |test|
24
+ test.libs << 'lib' << 'test'
25
+ test.pattern = 'test/**/*_test.rb'
26
+ test.verbose = true
27
+ end
28
+
29
+ begin
30
+ require 'rcov/rcovtask'
31
+ Rcov::RcovTask.new do |test|
32
+ test.libs << 'test'
33
+ test.pattern = 'test/**/*_test.rb'
34
+ test.verbose = true
35
+ end
36
+ rescue LoadError
37
+ task :rcov do
38
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
39
+ end
40
+ end
41
+
42
+ task :test => :check_dependencies
43
+
44
+ task :default => :test
45
+
46
+ require 'rake/rdoctask'
47
+ Rake::RDocTask.new do |rdoc|
48
+ if File.exist?('VERSION')
49
+ version = File.read('VERSION')
50
+ else
51
+ version = ""
52
+ end
53
+
54
+ rdoc.rdoc_dir = 'rdoc'
55
+ rdoc.title = "named_instances #{version}"
56
+ rdoc.rdoc_files.include('README*')
57
+ rdoc.rdoc_files.include('lib/**/*.rb')
58
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1,72 @@
1
+ module NamedInstances
2
+
3
+ def self.included base
4
+ base.extend ClassMethods
5
+ end
6
+
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]
13
+
14
+ after_save do
15
+ Rails.logger.info("Reloading #{self} named instances because of a save")
16
+ self.load_named_instances
17
+ end
18
+ end
19
+
20
+ def named_instances_loaded?
21
+ !! @named_instances_loaded
22
+ end
23
+
24
+ def name_method
25
+ @named_instances_name_method
26
+ end
27
+
28
+ def get *names
29
+ load_named_instances unless @named_instances_loaded
30
+
31
+ name = names.map {|n| n.to_s }.join "_"
32
+ key_name = "#{named_instances_prefix}_#{name}"
33
+ result = Rails.cache.read(key_name)
34
+
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
37
+
38
+ result
39
+ end
40
+
41
+ def named_instances_normalize name
42
+ name.downcase.gsub(/[^a-z0-9]+/, '_').gsub(/_$/, '')
43
+ end
44
+
45
+ def named_instances_prefix
46
+ "named_instances_#{self}"
47
+ end
48
+
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
60
+ end
61
+
62
+ Rails.cache.write(key_name, instance)
63
+ end
64
+
65
+ @named_instances_loaded = true
66
+ rescue Exception => e
67
+ Rails.logger.info "Named Instances: exception ignored, class = #{self}, error = #{e.inspect}"
68
+ end
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,63 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{named_instances}
8
+ s.version = "0.0.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Jason Dew"]
12
+ s.date = %q{2009-10-08}
13
+ s.description = %q{Give cached access to ActiveRecord models for quicker and more readable data-based logic.}
14
+ s.email = %q{jason.dew@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/named_instances.rb",
27
+ "named_instances.gemspec",
28
+ "test/fixtures/diagnosis.rb",
29
+ "test/named_instances_test.rb",
30
+ "test/rails_crutch.rb",
31
+ "test/test_helper.rb"
32
+ ]
33
+ s.homepage = %q{http://github.com/jasondew/named_instances}
34
+ s.rdoc_options = ["--charset=UTF-8"]
35
+ s.require_paths = ["lib"]
36
+ s.rubygems_version = %q{1.3.5}
37
+ s.summary = %q{Named instances for ActiveRecord}
38
+ s.test_files = [
39
+ "test/fixtures/diagnosis.rb",
40
+ "test/named_instances_test.rb",
41
+ "test/rails_crutch.rb",
42
+ "test/test_helper.rb"
43
+ ]
44
+
45
+ if s.respond_to? :specification_version then
46
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
47
+ s.specification_version = 3
48
+
49
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
50
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
51
+ s.add_development_dependency(%q<mocha>, [">= 0"])
52
+ s.add_development_dependency(%q<activesupport>, [">= 0"])
53
+ else
54
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
55
+ s.add_dependency(%q<mocha>, [">= 0"])
56
+ s.add_dependency(%q<activesupport>, [">= 0"])
57
+ end
58
+ else
59
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
60
+ s.add_dependency(%q<mocha>, [">= 0"])
61
+ s.add_dependency(%q<activesupport>, [">= 0"])
62
+ end
63
+ end
@@ -0,0 +1,19 @@
1
+ require 'rubygems'
2
+ require 'active_record'
3
+
4
+ class Diagnosis < ActiveRecord::Base
5
+ include NamedInstances
6
+ has_named_instances :name
7
+ end
8
+
9
+ ActiveRecord::Base.establish_connection :adapter => 'sqlite3', :database => 'test/test.db'
10
+
11
+ ActiveRecord::Schema.define do
12
+ create_table :diagnoses, :force => true do |t|
13
+ t.string :name
14
+ t.timestamps
15
+ end
16
+
17
+ Diagnosis.create :name => "Hypertension"
18
+ Diagnosis.create :name => "Diabetes Mellitus"
19
+ end
@@ -0,0 +1,70 @@
1
+ require 'test_helper'
2
+
3
+ class NamedInstancesTest < Test::Unit::TestCase
4
+ context "Named instances of an AR model" do
5
+
6
+ should "be able get instances" do
7
+ result = Diagnosis.get(:diabetes_mellitus)
8
+ assert_equal Diagnosis.find_by_name("Diabetes Mellitus"), result
9
+ end
10
+
11
+ should "reload after a save" do
12
+ Diagnosis.get(:diabetes_mellitus)
13
+ new_behavior = Diagnosis.create(:name=>"test1")
14
+
15
+ result = Diagnosis.get(:test1)
16
+ assert_equal new_behavior, result
17
+ end
18
+
19
+ should "raise an error if it doesn't exist at all" do
20
+ assert_raise ArgumentError do
21
+ Diagnosis.get(:test2)
22
+ end
23
+ end
24
+
25
+ should "find deleted instances as well" do
26
+ Diagnosis.get(:diabetes_mellitus).destroy
27
+ Diagnosis.load_named_instances
28
+
29
+ assert Diagnosis.get(:diabetes_mellitus)
30
+ end
31
+
32
+ should "maintain the same class object id on get" do
33
+ result = Diagnosis.get(:diabetes_mellitus)
34
+ assert_equal Diagnosis.object_id, result.class.object_id
35
+ end
36
+
37
+ should "return the method used for naming" do
38
+ assert_equal(:name, Diagnosis.name_method)
39
+ end
40
+
41
+ should "accept an array of naming methods" do
42
+ Foo = Struct.new(:bar, :baz)
43
+ Foo.send(:include, NamedInstances)
44
+
45
+ foo1 = Foo.new("abc", "def")
46
+ foo2 = Foo.new("xyz", "zzz")
47
+
48
+ Foo.expects(:after_save)
49
+ Foo.expects(:all).returns([foo1, foo2])
50
+
51
+ Rails.cache.expects(:write).with("named_instances_NamedInstancesTest::Foo_abc_def", foo1)
52
+ Rails.cache.expects(:write).with("named_instances_NamedInstancesTest::Foo_xyz_zzz", foo2)
53
+
54
+ Foo.has_named_instances [:bar, :baz]
55
+ assert Foo.load_named_instances
56
+ end
57
+
58
+ should "not crash if the instances can't be loaded immediately" do
59
+ BadFoo = Struct.new(:bar, :baz)
60
+ BadFoo.send(:include, NamedInstances)
61
+
62
+ BadFoo.expects(:after_save)
63
+ BadFoo.expects(:all).raises(Exception)
64
+
65
+ BadFoo.has_named_instances [:bar, :baz]
66
+ assert_match /exception/, BadFoo.load_named_instances
67
+ end
68
+
69
+ end
70
+ end
@@ -0,0 +1,12 @@
1
+ require 'active_support'
2
+ require 'active_support/cache'
3
+
4
+ class Rails
5
+ def self.cache
6
+ @@cache ||= ActiveSupport::Cache::MemoryStore.new
7
+ end
8
+
9
+ def self.logger
10
+ @@logger ||= ActiveSupport::BufferedLogger.new(STDOUT)
11
+ end
12
+ end
@@ -0,0 +1,15 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+ require 'mocha'
5
+ require 'rails_crutch'
6
+
7
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
8
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), 'fixtures'))
9
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
10
+
11
+ require 'named_instances'
12
+ require 'diagnosis'
13
+
14
+ class Test::Unit::TestCase
15
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: named_instances
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jason Dew
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-10-08 00:00:00 -04:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ - !ruby/object:Gem::Dependency
26
+ name: mocha
27
+ type: :development
28
+ version_requirement:
29
+ version_requirements: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ version:
35
+ - !ruby/object:Gem::Dependency
36
+ name: activesupport
37
+ type: :development
38
+ version_requirement:
39
+ version_requirements: !ruby/object:Gem::Requirement
40
+ requirements:
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: "0"
44
+ version:
45
+ description: Give cached access to ActiveRecord models for quicker and more readable data-based logic.
46
+ email: jason.dew@gmail.com
47
+ executables: []
48
+
49
+ extensions: []
50
+
51
+ extra_rdoc_files:
52
+ - LICENSE
53
+ - README.rdoc
54
+ files:
55
+ - .document
56
+ - .gitignore
57
+ - LICENSE
58
+ - README.rdoc
59
+ - Rakefile
60
+ - VERSION
61
+ - lib/named_instances.rb
62
+ - named_instances.gemspec
63
+ - test/fixtures/diagnosis.rb
64
+ - test/named_instances_test.rb
65
+ - test/rails_crutch.rb
66
+ - test/test_helper.rb
67
+ has_rdoc: true
68
+ homepage: http://github.com/jasondew/named_instances
69
+ licenses: []
70
+
71
+ post_install_message:
72
+ rdoc_options:
73
+ - --charset=UTF-8
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: "0"
81
+ version:
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: "0"
87
+ version:
88
+ requirements: []
89
+
90
+ rubyforge_project:
91
+ rubygems_version: 1.3.5
92
+ signing_key:
93
+ specification_version: 3
94
+ summary: Named instances for ActiveRecord
95
+ test_files:
96
+ - test/fixtures/diagnosis.rb
97
+ - test/named_instances_test.rb
98
+ - test/rails_crutch.rb
99
+ - test/test_helper.rb