acts_as_singleton 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/History.txt ADDED
@@ -0,0 +1,5 @@
1
+ === 0.0.1 / 2009-03-30
2
+
3
+ * 1 major enhancement
4
+
5
+ * 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/acts_as_singleton.rb
7
+ test/acts_as_singleton_test.rb
data/README.txt ADDED
@@ -0,0 +1,106 @@
1
+ = acts_as_singleton
2
+
3
+ http://github.com/stephencelis/acts_as_singleton
4
+
5
+
6
+ == DESCRIPTION
7
+
8
+ A lightweight singleton library for your Active Record models.
9
+
10
+ It just makes sense to store mutable, site-wide, admin-level settings in the
11
+ database. Right? A key-value table may be more flexible, but maybe we don't
12
+ want to be flexible!
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
+ From RubyForge:
55
+
56
+ % gem install acts_as_singleton
57
+
58
+
59
+ Or GitHub:
60
+
61
+ % gem install stephencelis-acts_as_singleton --source=http://gems.github.com
62
+
63
+
64
+ And configure:
65
+
66
+ config.gem "stephencelis-acts_as_singleton",
67
+ :lib => "acts_as_singleton",
68
+ :source => "http://gems.github.com"
69
+
70
+
71
+ === As a plugin
72
+
73
+ Traditional:
74
+
75
+ % script/plugin install git://github.com/stephencelis/acts_as_singleton.git
76
+
77
+
78
+ Or, as a submodule:
79
+
80
+ % git submodule add git://github.com/stephencelis/acts_as_singleton.git \
81
+ vendor/plugins/acts_as_singleton
82
+
83
+
84
+ == LICENSE
85
+
86
+ (The MIT License)
87
+
88
+ (c) 2009-* Stephen Celis, stephen@stephencelis.com.
89
+
90
+ Permission is hereby granted, free of charge, to any person obtaining a copy
91
+ of this software and associated documentation files (the "Software"), to deal
92
+ in the Software without restriction, including without limitation the rights
93
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
94
+ copies of the Software, and to permit persons to whom the Software is
95
+ furnished to do so, subject to the following conditions:
96
+
97
+ The above copyright notice and this permission notice shall be included in all
98
+ copies or substantial portions of the Software.
99
+
100
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
101
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
102
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
103
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
104
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
105
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
106
+ SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ $: << File.dirname(__FILE__) + "/lib"
2
+ require "rubygems"
3
+ require "hoe"
4
+ require "active_record"
5
+ require "acts_as_singleton"
6
+
7
+ Hoe.new("acts_as_singleton", ActiveRecord::Singleton::VERSION) do |p|
8
+ p.developer("Stephen Celis", "stephen@stephencelis.com")
9
+ p.remote_rdoc_dir = ''
10
+ end
11
+
12
+ require 'rake'
13
+ require 'rake/testtask'
14
+ require 'rake/rdoctask'
15
+
16
+ desc 'Default: run unit tests.'
17
+ task :default => :test
18
+
19
+ desc 'Test the acts_as_singleton plugin.'
20
+ Rake::TestTask.new(:test) do |t|
21
+ t.libs << 'lib'
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = true
25
+ end
26
+
27
+ desc 'Generate documentation for the acts_as_singleton plugin.'
28
+ Rake::RDocTask.new(:rdoc) do |rdoc|
29
+ rdoc.rdoc_dir = 'rdoc'
30
+ rdoc.title = 'acts_as_singleton'
31
+ rdoc.options << '--line-numbers' << '--inline-source'
32
+ rdoc.rdoc_files.include('README')
33
+ rdoc.rdoc_files.include('lib/**/*.rb')
34
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require "acts_as_singleton"
@@ -0,0 +1,76 @@
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.1"
21
+
22
+ # This pattern matches methods that should be made private because they
23
+ # should not be used in singleton classes.
24
+ PRIVATE = /^all(?:oc.*)?$|alloc|create|find|firs|mini|max|new|d_sco|upd/
25
+
26
+ def self.included(model)
27
+ model.class_eval do
28
+ private_class_method *methods.grep(PRIVATE) # Deny existent others.
29
+
30
+ class << self
31
+ def exists? # Refuse arguments.
32
+ super
33
+ end
34
+
35
+ # Returns the first (presumably only) record in the database, or
36
+ # creates one.
37
+ #
38
+ # If validation fails on creation, it will return an unsaved object.
39
+ #
40
+ # HomepageSettings.instance.update_attributes :welcome => "Hello!"
41
+ def instance
42
+ first || create
43
+ end
44
+
45
+ def inspect
46
+ super.sub(/id: .+?, /) {} # Irrelevant.
47
+ end
48
+ end
49
+
50
+ def clone
51
+ raise TypeError, "can't clone instance of singleton #{self.class}"
52
+ end
53
+
54
+ def dup
55
+ raise TypeError, "can't dup instance of singleton #{self.class}"
56
+ end
57
+
58
+ def inspect
59
+ super.sub(/id: .+?, /) {} # Irrelevant.
60
+ end
61
+ end
62
+ end
63
+ end
64
+
65
+ class << Base
66
+ # Class method to provide a more Rails-like experience. Merely includes
67
+ # the <tt>ActiveRecord::Singleton</tt> module.
68
+ #
69
+ # class HomepageSettings < ActiveRecord::Base
70
+ # acts_as_singleton # Equivalent to "include ActiveRecord::Singleton".
71
+ # end
72
+ def acts_as_singleton
73
+ include Singleton
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,68 @@
1
+ require 'test/unit'
2
+ require 'rubygems'
3
+ require 'active_record'
4
+ require "#{File.dirname(__FILE__)}/../init"
5
+ require 'active_support'
6
+ require 'active_support/test_case'
7
+
8
+ def setup_db
9
+ ActiveRecord::Base.establish_connection(:adapter => 'sqlite3', :dbfile => ':memory:')
10
+ ActiveRecord::Schema.define(:version => 1) do
11
+ create_table :homepage_settings do |t|
12
+ t.text :welcome_message
13
+ t.timestamp :published_at, :expired_at
14
+ end
15
+ end
16
+ end
17
+
18
+ def teardown_db
19
+ ActiveRecord::Base.connection.tables.each do |table|
20
+ ActiveRecord::Base.connection.drop_table(table)
21
+ end
22
+ end
23
+
24
+ setup_db
25
+ class HomepageSettings < ActiveRecord::Base
26
+ acts_as_singleton
27
+ end
28
+
29
+ class ActsAsSingletonTest < ActiveSupport::TestCase
30
+ setup do
31
+ setup_db
32
+ end
33
+
34
+ teardown do
35
+ teardown_db
36
+ end
37
+
38
+ test "should be provocative" do
39
+ assert_raise(NoMethodError) { HomepageSettings.new }
40
+ assert_raise(NoMethodError) { HomepageSettings.create }
41
+ assert_raise(NoMethodError) { HomepageSettings.update }
42
+ assert_raise(NoMethodError) { HomepageSettings.alloc }
43
+ assert_raise(NoMethodError) { HomepageSettings.all }
44
+ assert_raise(NoMethodError) { HomepageSettings.first }
45
+ assert_raise(NoMethodError) { HomepageSettings.find }
46
+ assert_raise(NoMethodError) { HomepageSettings.named_scope }
47
+ assert_raise(NoMethodError) { HomepageSettings.minimum }
48
+ assert_raise(NoMethodError) { HomepageSettings.maximum }
49
+ assert_raise(ArgumentError) { HomepageSettings.exists? 1 }
50
+ assert_raise(TypeError) { HomepageSettings.instance.clone }
51
+ assert_raise(TypeError) { HomepageSettings.instance.dup }
52
+ end
53
+
54
+ test "should be lazy" do
55
+ assert !HomepageSettings.exists?
56
+ assert_instance_of HomepageSettings, HomepageSettings.instance
57
+ assert HomepageSettings.exists?
58
+ end
59
+
60
+ test "should be equal" do
61
+ assert_equal HomepageSettings.instance, HomepageSettings.instance
62
+ end
63
+
64
+ test "should be unidentifiable" do
65
+ assert_no_match(/id: .+?,/, HomepageSettings.inspect)
66
+ assert_no_match(/id: .+?,/, HomepageSettings.instance.inspect)
67
+ end
68
+ end
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: acts_as_singleton
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Stephen Celis
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-01 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: hoe
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 1.11.0
24
+ version:
25
+ description: A lightweight singleton library for your Active Record models. It just makes sense to store mutable, site-wide, admin-level settings in the database. Right? A key-value table may be more flexible, but maybe we don't want to be flexible!
26
+ email:
27
+ - stephen@stephencelis.com
28
+ executables: []
29
+
30
+ extensions: []
31
+
32
+ extra_rdoc_files:
33
+ - History.txt
34
+ - Manifest.txt
35
+ - README.txt
36
+ files:
37
+ - History.txt
38
+ - Manifest.txt
39
+ - README.txt
40
+ - Rakefile
41
+ - init.rb
42
+ - lib/acts_as_singleton.rb
43
+ - test/acts_as_singleton_test.rb
44
+ has_rdoc: true
45
+ homepage: http://github.com/stephencelis/acts_as_singleton
46
+ post_install_message:
47
+ rdoc_options:
48
+ - --main
49
+ - README.txt
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project: acts_as_singleton
67
+ rubygems_version: 1.3.1
68
+ signing_key:
69
+ specification_version: 2
70
+ summary: A lightweight singleton library for your Active Record models
71
+ test_files: []
72
+