singleton_act 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/History.rdoc ADDED
@@ -0,0 +1,26 @@
1
+ === 0.0.4 / 2010-01-21
2
+
3
+ * 1 bugfix
4
+
5
+ * Allow 'find' calls from ActiveRecord.
6
+
7
+
8
+ === 0.0.3 / 2009-11-29
9
+
10
+ * 1 bugfix
11
+
12
+ * Don't make ActiveRecord::Base.allocate private.
13
+
14
+
15
+ === 0.0.2 / 2009-04-13
16
+
17
+ * 1 minor enhancement
18
+
19
+ * Update regular expression to allow for a needed method.
20
+
21
+
22
+ === 0.0.1 / 2009-03-30
23
+
24
+ * 1 major enhancement
25
+
26
+ * Birthday!
data/Manifest.txt ADDED
@@ -0,0 +1,7 @@
1
+ History.txt
2
+ Manifest.txt
3
+ README.txt
4
+ Rakefile
5
+ init.rb
6
+ lib/singleton_act.rb
7
+ test/acts_as_singleton_test.rb
data/README.rdoc ADDED
@@ -0,0 +1,100 @@
1
+ = Singleton Act
2
+
3
+
4
+ == DESCRIPTION
5
+
6
+ A lightweight singleton library for your Active Record models.
7
+
8
+ It just makes sense to store mutable, site-wide, admin-level settings in the
9
+ database. Right? A key-value table may be more flexible, but maybe we don't
10
+ want to be flexible!
11
+
12
+ If you truly want that flexibility: http://github.com/stephencelis/kvc
13
+
14
+
15
+ == FEATURES/PROBLEMS
16
+
17
+ * Lightning-fast queries! Doesn't get any faster than this! (Guaranteed.)
18
+ * Follows the ultra-cool "singleton" pattern! (Tell your friends.)
19
+ * What? That's not the "cool" singleton in Ruby? (Don't tell your friends?)
20
+
21
+
22
+ == SYNOPSIS
23
+
24
+ class HomepageSettings < ActiveRecord::Base
25
+ include ActiveRecord::Singleton
26
+ end
27
+
28
+
29
+ How Rubyish! Oh, you want it Railsish? Very well...
30
+
31
+ class HomepageSettings < ActiveRecord::Base
32
+ acts_as_singleton
33
+ end
34
+
35
+
36
+ Have your cake and eat your silly idiom, too! Just try to create a row!
37
+
38
+ HomepageSettings.instance # => #<HomepageSettings...>
39
+
40
+
41
+ Don't even try to access it otherwise. It won't work!
42
+
43
+
44
+ == REQUIREMENTS
45
+
46
+ * Active Record 2.3.2 or greater.
47
+ * A delicate palate.
48
+
49
+
50
+ == INSTALL
51
+
52
+ === As a gem
53
+
54
+ Configure:
55
+
56
+ # config/environment.rb
57
+ config.gem "acts_as_singleton"
58
+
59
+
60
+ And install:
61
+
62
+ % [sudo] rake gems:install
63
+
64
+
65
+ === As a plugin
66
+
67
+ Traditional:
68
+
69
+ % script/plugin install git://github.com/stephencelis/acts_as_singleton.git
70
+
71
+
72
+ Or, as a submodule:
73
+
74
+ % git submodule add git://github.com/stephencelis/acts_as_singleton.git \
75
+ vendor/plugins/acts_as_singleton
76
+
77
+
78
+ == LICENSE
79
+
80
+ (The MIT License)
81
+
82
+ (c) 2009-2010 Stephen Celis, stephen@stephencelis.com.
83
+
84
+ Permission is hereby granted, free of charge, to any person obtaining a copy
85
+ of this software and associated documentation files (the "Software"), to deal
86
+ in the Software without restriction, including without limitation the rights
87
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
88
+ copies of the Software, and to permit persons to whom the Software is
89
+ furnished to do so, subject to the following conditions:
90
+
91
+ The above copyright notice and this permission notice shall be included in all
92
+ copies or substantial portions of the Software.
93
+
94
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
95
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
96
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
97
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
98
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
99
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
100
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,17 @@
1
+ $: << File.expand_path('../lib', __FILE__)
2
+ require 'active_record'
3
+ require 'singleton_act'
4
+
5
+ require 'rake'
6
+ require 'rake/testtask'
7
+
8
+ desc 'Default: run unit tests.'
9
+ task :default => :test
10
+
11
+ desc 'Test the acts_as_singleton plugin.'
12
+ Rake::TestTask.new do |t|
13
+ t.libs << 'lib'
14
+ t.libs << 'test'
15
+ t.pattern = 'test/**/*_test.rb'
16
+ t.verbose = true
17
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "singleton_act"
@@ -0,0 +1,93 @@
1
+ module ActiveRecord
2
+ # A lightweight singleton library for your Active Record models.
3
+ #
4
+ # class HomepageSettings < ActiveRecord::Base
5
+ # include ActiveRecord::Singleton
6
+ # end
7
+ #
8
+ # HomepageSettings.instance # => #<Homepage...>
9
+ #
10
+ # Like Ruby's built-in module, it will not prevent you from instantiating
11
+ # more than one object, but it will make it more difficult.
12
+ #
13
+ # HomepageSettings.__send__(:new).new_record? # => true
14
+ #
15
+ # Most methods assuming more than one database record will be made private
16
+ # upon including this module. Make sure your public class methods do not
17
+ # clash with the <tt>ActiveRecord::Singleton::PRIVATE</tt> pattern, or
18
+ # define them after including the module.
19
+ module Singleton
20
+ VERSION = "0.0.7"
21
+
22
+ # This pattern matches methods that should be made private because they
23
+ # should not be used in singleton classes.
24
+ PRIVATE = \
25
+ /^all$|create(?!_reflection|_callback)|find(?!er|_callback)|firs|mini|max|new|d_sco|^upd/
26
+
27
+ def self.included(model)
28
+ model.class_eval do
29
+ private_class_method *methods.grep(PRIVATE) # Deny existent others.
30
+
31
+ class << self
32
+ def exists? # Refuse arguments.
33
+ super
34
+ end
35
+
36
+ # Returns the first (presumably only) record in the database, or
37
+ # creates one.
38
+ #
39
+ # If validation fails on creation, it will return an unsaved object.
40
+ #
41
+ # HomepageSettings.instance.update_attributes :welcome => "Hello!"
42
+ def instance
43
+ first || create
44
+ end
45
+
46
+ def inspect
47
+ super.sub(/id: .+?, /) {} # Irrelevant.
48
+ end
49
+
50
+ def find(*)
51
+ unless caller.first.include?("lib/active_record")
52
+ raise NoMethodError,
53
+ "private method `find' called for #{inspect}"
54
+ end
55
+ super
56
+ end
57
+
58
+ def find_by_sql(*)
59
+ unless caller.first.include?("lib/active_record")
60
+ raise NoMethodError,
61
+ "private method `find_by_sql' called for #{inspect}"
62
+ end
63
+ super
64
+ end
65
+ end
66
+
67
+ def clone
68
+ raise TypeError, "can't clone instance of singleton #{self.class}"
69
+ end
70
+
71
+ def dup
72
+ raise TypeError, "can't dup instance of singleton #{self.class}"
73
+ end
74
+
75
+ def inspect
76
+ super.sub(/id: .+?, /) {} # Irrelevant.
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ class << Base
83
+ # Class method to provide a more Rails-like experience. Merely includes
84
+ # the <tt>ActiveRecord::Singleton</tt> module.
85
+ #
86
+ # class HomepageSettings < ActiveRecord::Base
87
+ # acts_as_singleton # Equivalent to "include ActiveRecord::Singleton".
88
+ # end
89
+ def acts_as_singleton
90
+ include Singleton
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,106 @@
1
+ require 'test/unit'
2
+ require 'active_record'
3
+ require "#{File.dirname(__FILE__)}/../init"
4
+ require 'active_support'
5
+ require 'active_support/test_case'
6
+ ActiveRecord::Migration.verbose = false
7
+
8
+ def setup_db
9
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :database => ':memory:')
10
+ ActiveRecord::Schema.define(:version => 1) do
11
+ create_table :homepage_settings do |t|
12
+ t.string :type
13
+ t.text :welcome_message
14
+ t.timestamp :published_at, :expired_at
15
+ end
16
+
17
+ create_table :cottages do |t|
18
+ t.belongs_to :home_away_from_home, :polymorphic => true
19
+ end
20
+ end
21
+ end
22
+
23
+ def teardown_db
24
+ ActiveRecord::Base.connection.tables.each do |table|
25
+ ActiveRecord::Base.connection.drop_table(table)
26
+ end
27
+ end
28
+
29
+ setup_db
30
+ class HomepageSettings < ActiveRecord::Base
31
+ acts_as_singleton
32
+ has_many :cottages, :as => :home_away_from_home
33
+ end
34
+
35
+ class LoggedInSettings < HomepageSettings
36
+ end
37
+
38
+ class Cottage < ActiveRecord::Base
39
+ belongs_to :home_away_from_home, :polymorphic => true
40
+ validates_associated :home_away_from_home
41
+ end
42
+
43
+ class ActsAsSingletonTest < ActiveSupport::TestCase
44
+ setup do
45
+ setup_db
46
+ end
47
+
48
+ teardown do
49
+ teardown_db
50
+
51
+ HomepageSettings.instance_eval do
52
+ remove_instance_variable :@instance if defined? @instance
53
+ end
54
+ end
55
+
56
+ test "should be provocative" do
57
+ assert_raise(NoMethodError) { HomepageSettings.new }
58
+ assert_raise(NoMethodError) { HomepageSettings.create }
59
+ assert_raise(NoMethodError) { HomepageSettings.update }
60
+ assert_raise(NoMethodError) { HomepageSettings.alloc }
61
+ assert_raise(NoMethodError) { HomepageSettings.all }
62
+ assert_raise(NoMethodError) { HomepageSettings.first }
63
+ assert_raise(NoMethodError) { HomepageSettings.find }
64
+ assert_raise(NoMethodError) { HomepageSettings.find_by_sql("") }
65
+ assert_raise(NoMethodError) { HomepageSettings.named_scope }
66
+ assert_raise(NoMethodError) { HomepageSettings.minimum }
67
+ assert_raise(NoMethodError) { HomepageSettings.maximum }
68
+ assert_raise(ArgumentError) { HomepageSettings.exists? 1 }
69
+ assert_raise(TypeError) { HomepageSettings.instance.clone }
70
+ assert_raise(TypeError) { HomepageSettings.instance.dup }
71
+ end
72
+
73
+ test "should be lazy" do
74
+ assert !HomepageSettings.exists?
75
+ assert_instance_of HomepageSettings, HomepageSettings.instance
76
+ assert HomepageSettings.exists?
77
+ end
78
+
79
+ test "should be equal" do
80
+ assert_equal HomepageSettings.instance, HomepageSettings.instance
81
+ end
82
+
83
+ test "should be unidentifiable" do
84
+ assert_no_match(/id: .+?,/, HomepageSettings.inspect)
85
+ assert_no_match(/id: .+?,/, HomepageSettings.instance.inspect)
86
+ end
87
+
88
+ test "should be mutable" do
89
+ assert_nothing_raised do
90
+ HomepageSettings.instance.update_attributes! :welcome_message => "OH HAI"
91
+ end
92
+ end
93
+
94
+ test "should honor STI" do
95
+ ActiveRecord::Base.clear_active_connections!
96
+ HomepageSettings.instance # Fetch parent.
97
+ LoggedInSettings.instance # Fetch child.
98
+ assert_nothing_raised ActiveRecord::SubclassNotFound do
99
+ LoggedInSettings.instance # Fetch again.
100
+ end
101
+ end
102
+
103
+ test "should honor polymorphism" do
104
+ HomepageSettings.instance.cottages.create
105
+ end
106
+ end
metadata ADDED
@@ -0,0 +1,107 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: singleton_act
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Stephen Celis
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-05-07 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activerecord
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: 3.1.0
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: 3.1.0
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: sqlite3
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :development
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ description: ! 'It just makes sense to store mutable, site-wide, admin-level settings
63
+ in the database. Right? A key-value table may be more flexible, but maybe we don''t
64
+ want to be flexible! If you truly want that flexibility: http://github.com/stephencelis/kvc'
65
+ email: stephen@stephencelis.com
66
+ executables: []
67
+ extensions: []
68
+ extra_rdoc_files:
69
+ - History.rdoc
70
+ - Manifest.txt
71
+ - README.rdoc
72
+ files:
73
+ - History.rdoc
74
+ - Manifest.txt
75
+ - README.rdoc
76
+ - Rakefile
77
+ - init.rb
78
+ - lib/singleton_Act.rb
79
+ - test/acts_as_singleton_test.rb
80
+ homepage: http://github.com/kristianmandrup/acts_as_singleton
81
+ licenses: []
82
+ post_install_message:
83
+ rdoc_options:
84
+ - --main
85
+ - README.rdoc
86
+ require_paths:
87
+ - lib
88
+ required_ruby_version: !ruby/object:Gem::Requirement
89
+ none: false
90
+ requirements:
91
+ - - ! '>='
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
96
+ requirements:
97
+ - - ! '>='
98
+ - !ruby/object:Gem::Version
99
+ version: '0'
100
+ requirements: []
101
+ rubyforge_project:
102
+ rubygems_version: 1.8.24
103
+ signing_key:
104
+ specification_version: 3
105
+ summary: A lightweight singleton library for your Active Record models.
106
+ test_files: []
107
+ has_rdoc: true