quiescent 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ source "http://rubygems.org"
2
+ # Add dependencies required to use your gem here.
3
+ # Example:
4
+ # gem "activesupport", ">= 2.3.5"
5
+
6
+ # Add dependencies to develop your gem here.
7
+ # Include everything needed to run rake, tests, features, etc.
8
+ group :development do
9
+ gem "rspec", "~> 2.3.0"
10
+ gem "bundler", "~> 1.0.0"
11
+ gem "jeweler", "~> 1.5.2"
12
+ gem "rcov", ">= 0"
13
+ end
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,91 @@
1
+ = quiescent
2
+
3
+ This is a very simple mixin to support quiescing constants, which we call "quiescents" in Ruby. You may assign a value to a quiescing constant once during the execution of the program; however, a quiescing constant's value is fixed after the first time it is read. Quiescing constants may have default values (specified either as explicit values or argumentless blocks to compute that value) that take effect if they are not explicitly assigned to before their first use.
4
+
5
+ If you like the convenience of constants but might need to defer giving them values, you'll like quiescents. If you like single-assignment variables in languages like Prolog, you'll like quiescents. If you [suspect that write-once data is far more common than most people admit](http://www.springerlink.com/content/q530q33677843u74/), you'll like quiescents.
6
+
7
+ Here's a simple example:
8
+
9
+ require 'quiescent'
10
+
11
+ class Foo
12
+ include Quiescent
13
+
14
+ # This declares a constant named PostCode that
15
+ # quiesces to a default value of 53706 unless
16
+ # another is provided before the first time it
17
+ # is read
18
+ quiescent :PostCode, 53706
19
+
20
+ # Let's assume that the awesome features are off
21
+ # by default.
22
+ quiescent :EnableTotallyAwesomeFeature, false
23
+ quiescent :EnableSlightlyLessAwesomeFeature, false
24
+
25
+ # This declares a constant named LazyThrees that
26
+ # quiesces to a list of all natural numbers less than
27
+ # 100 that are divisible by three, as calculated
28
+ # in the block, unless another value is provided.
29
+ # The block argument will execute at most once.
30
+ quiescent :LazyThrees do
31
+ (1..100).to_a.select {|x| x % 3 == 0}
32
+ end
33
+
34
+ # In this method, we'll see how to force quiescents
35
+ # to quiesce by giving them values and reading their
36
+ # values.
37
+ def self.setup
38
+ # We only want to do this once
39
+ return if @setup_done
40
+ @setup_done = true
41
+
42
+ puts "The postal code is #{Foo::PostCode}"
43
+
44
+ # You can provide non-default values with the
45
+ # quiesce method...
46
+ Foo.quiesce(:EnableTotallyAwesomeFeature, "sometimes")
47
+
48
+ # ...or by using a special CONSTNAME= method, which
49
+ # will be intercepted by method_missing.
50
+ Foo.EnableSlightlyLessAwesomeFeature = true
51
+
52
+ # Note that this only works for names corresponding
53
+ # to declared quiescing constants...
54
+ begin
55
+ Foo.EnableCrummyFeature = true
56
+ rescue Exception
57
+ puts("whoa, failure in aisle 47")
58
+ end
59
+
60
+ # ...and only once for each quiescing constant.
61
+ begin
62
+ Foo.EnableSlightlyLessAwesomeFeature = false
63
+ rescue Exception
64
+ puts("nice try, pal")
65
+ end
66
+ end
67
+ end
68
+
69
+ == Potential gotchas
70
+
71
+ Because of how constant resolution works in Ruby, we don't have a way to support quiescents whose names are identical to constants declared in the global namespace. If you try and declare one, for example, `Foo::Kernel`, you'll get the toplevel `Kernel` when you try to access `Foo::Kernel`, along with the following message:
72
+
73
+ warning: toplevel constant Kernel referenced by Foo::Kernel
74
+
75
+ `Quiescent` adds `const_missing` and `method_missing` methods to classes that mix it in. These should play nicely with preexisting implementations of these (although these interactions are not exhaustively tested); however, classes that provide their own implementations of these methods should do so with care. Please report any problematic interactions with your own code.
76
+
77
+ == Contributing to quiescent
78
+
79
+ * Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
80
+ * Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
81
+ * Fork the project
82
+ * Start a feature/bugfix branch
83
+ * Commit and push until you are happy with your contribution
84
+ * Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
85
+ * Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
86
+
87
+ == Copyright
88
+
89
+ Copyright (c) 2011 Red Hat, Inc. See LICENSE.txt for
90
+ further details.
91
+
@@ -0,0 +1,50 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ # gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
15
+ gem.name = "quiescent"
16
+ gem.homepage = "http://github.com/willb/quiescent"
17
+ gem.license = "Apache v2"
18
+ gem.summary = %Q{Simple library for write-once ("quiescing") constants}
19
+ gem.description = %Q{This is a very simple mixin to support quiescing constants (which we call "quiescents") in Ruby. You may assign a value to a quiescing constant once during the execution of the program; however, a quiescing constant's value is fixed after the first time it is read. Quiescing constants may have default values (specified either as explicit values or argumentless blocks to compute that value) that take effect if they are not explicitly assigned to before their first use.}
20
+ gem.email = "willb@redhat.com"
21
+ gem.authors = ["Will Benton"]
22
+ # Include your dependencies below. Runtime dependencies are required when using your gem,
23
+ # and development dependencies are only needed for development (ie running rake tasks, tests, etc)
24
+ # gem.add_runtime_dependency 'jabber4r', '> 0.1'
25
+ # gem.add_development_dependency 'rspec', '> 1.2.3'
26
+ end
27
+ Jeweler::RubygemsDotOrgTasks.new
28
+
29
+ require 'rspec/core'
30
+ require 'rspec/core/rake_task'
31
+ RSpec::Core::RakeTask.new(:spec) do |spec|
32
+ spec.pattern = FileList['spec/**/*_spec.rb']
33
+ end
34
+
35
+ RSpec::Core::RakeTask.new(:rcov) do |spec|
36
+ spec.pattern = 'spec/**/*_spec.rb'
37
+ spec.rcov = true
38
+ end
39
+
40
+ task :default => :spec
41
+
42
+ require 'rake/rdoctask'
43
+ Rake::RDocTask.new do |rdoc|
44
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
45
+
46
+ rdoc.rdoc_dir = 'rdoc'
47
+ rdoc.title = "quiescent #{version}"
48
+ rdoc.rdoc_files.include('README*')
49
+ rdoc.rdoc_files.include('lib/**/*.rb')
50
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,59 @@
1
+ # Quiescing constants for Ruby.
2
+ #
3
+ # Copyright (c) 2011 Red Hat, Inc.
4
+ #
5
+ # Author: William Benton (willb@redhat.com)
6
+ #
7
+ # Licensed under the Apache License, Version 2.0 (the "License");
8
+ # you may not use this file except in compliance with the License.
9
+ # You may obtain a copy of the License at
10
+ #
11
+ # http://www.apache.org/licenses/LICENSE-2.0
12
+
13
+ module Quiescent
14
+ module CM
15
+ def quiescent(name, value=nil, &blk)
16
+ name = name.to_s
17
+ fail NameError.new("Invalid constant name '#{name}'") unless name =~ /^[A-Z]\w*$/
18
+ blk ||= lambda {value}
19
+ __qcs[name.to_sym] = blk
20
+ self
21
+ end
22
+
23
+ def __qcs
24
+ @__qcs ||= {}
25
+ end
26
+
27
+ def quiesce(name, val)
28
+ if __qcs[name]
29
+ const_set(name, val)
30
+ __qcs.delete(name)
31
+ return val
32
+ end
33
+ return nil
34
+ end
35
+
36
+ def const_missing(name)
37
+ name = name.to_sym
38
+ if __qcs[name]
39
+ quiesce(name, __qcs[name].call)
40
+ else
41
+ super
42
+ end
43
+ end
44
+
45
+ def method_missing(m, *args, &blk)
46
+ if m.to_s =~ /^([A-Z]\w*?)=$/ && __qcs[$1.to_sym]
47
+ quiesce($1.to_sym, *args)
48
+ else
49
+ super
50
+ end
51
+ end
52
+ end
53
+
54
+ def self.included(receiver)
55
+ receiver.extend CM
56
+ receiver.send(:private_class_method, :__qcs)
57
+ receiver.send(:private_class_method, :quiescent)
58
+ end
59
+ end
@@ -0,0 +1,64 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
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{quiescent}
8
+ s.version = "0.1.1"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Will Benton"]
12
+ s.date = %q{2011-09-02}
13
+ s.description = %q{This is a very simple mixin to support quiescing constants (which we call "quiescents") in Ruby. You may assign a value to a quiescing constant once during the execution of the program; however, a quiescing constant's value is fixed after the first time it is read. Quiescing constants may have default values (specified either as explicit values or argumentless blocks to compute that value) that take effect if they are not explicitly assigned to before their first use.}
14
+ s.email = %q{willb@redhat.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE.txt",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".rspec",
22
+ "Gemfile",
23
+ "LICENSE.txt",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "lib/quiescent.rb",
28
+ "quiescent.gemspec",
29
+ "spec/quiescent_spec.rb",
30
+ "spec/spec_helper.rb"
31
+ ]
32
+ s.homepage = %q{http://github.com/willb/quiescent}
33
+ s.licenses = ["Apache v2"]
34
+ s.require_paths = ["lib"]
35
+ s.rubygems_version = %q{1.3.7}
36
+ s.summary = %q{Simple library for write-once ("quiescing") constants}
37
+ s.test_files = [
38
+ "spec/quiescent_spec.rb",
39
+ "spec/spec_helper.rb"
40
+ ]
41
+
42
+ if s.respond_to? :specification_version then
43
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
44
+ s.specification_version = 3
45
+
46
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
47
+ s.add_development_dependency(%q<rspec>, ["~> 2.3.0"])
48
+ s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
49
+ s.add_development_dependency(%q<jeweler>, ["~> 1.5.2"])
50
+ s.add_development_dependency(%q<rcov>, [">= 0"])
51
+ else
52
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
53
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
54
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
55
+ s.add_dependency(%q<rcov>, [">= 0"])
56
+ end
57
+ else
58
+ s.add_dependency(%q<rspec>, ["~> 2.3.0"])
59
+ s.add_dependency(%q<bundler>, ["~> 1.0.0"])
60
+ s.add_dependency(%q<jeweler>, ["~> 1.5.2"])
61
+ s.add_dependency(%q<rcov>, [">= 0"])
62
+ end
63
+ end
64
+
@@ -0,0 +1,68 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe Quiescent do
4
+ it "successfully creates quiescents with default arguments" do
5
+ classid = gen_id("Foo")
6
+ k = Klass(classid, "include Quiescent", QConst("Bar", "12345"))
7
+ eval(k.to_s)
8
+ Kernel.const_get(classid)::Bar.should == 12345
9
+ end
10
+
11
+ it "successfully creates multiple quiescents with default arguments in a single class" do
12
+ classid = gen_id("Foo")
13
+ k = Klass(classid, "include Quiescent", QConst("Fred", ":a"), QConst("Barney", "54321"))
14
+ eval(k.to_s)
15
+ Kernel.const_get(classid)::Fred.should == :a
16
+ Kernel.const_get(classid)::Barney.should == 54321
17
+ end
18
+
19
+ it "allows assigning alternate values to quiescents with default arguments" do
20
+ classid = gen_id("Foo")
21
+ k = Klass(classid, "include Quiescent", QConst("Bar", "12345"))
22
+ eval(k.to_s)
23
+ Kernel.const_get(classid).Bar = 54321
24
+ Kernel.const_get(classid)::Bar.should == 54321
25
+ end
26
+
27
+ it "allows assigning alternate values to quiescents with default arguments only once" do
28
+ classid = gen_id("Foo")
29
+ k = Klass(classid, "include Quiescent", QConst("Bar", "12345"))
30
+ eval(k.to_s)
31
+ Kernel.const_get(classid).Bar = 54321
32
+ lambda {Kernel.const_get(classid).Bar = 12345}.should raise_error
33
+ Kernel.const_get(classid)::Bar.should == 54321
34
+ end
35
+
36
+ it "successfully creates quiescents with block arguments" do
37
+ classid = gen_id("Foo")
38
+ k = Klass(classid, "include Quiescent", QConst("Bar", Do("12345")))
39
+ eval(k.to_s)
40
+ Kernel.const_get(classid)::Bar.should == 12345
41
+ end
42
+
43
+ it "only evaluates quiescent block arguments once" do
44
+ $foo = Object.new
45
+ $foo.should_receive(:hash).once
46
+ classid = gen_id("Foo")
47
+ k = Klass(classid, "include Quiescent", QConst("Bar", Do("$foo.hash; 12345")))
48
+ eval(k.to_s)
49
+ Kernel.const_get(classid)::Bar.should == 12345
50
+ Kernel.const_get(classid)::Bar
51
+ end
52
+
53
+ it "only adds quiescents to the classes in which they are declared" do
54
+ classid1 = gen_id("Fred")
55
+ classid2 = gen_id("Barney")
56
+ [Klass(classid1, "include Quiescent", QConst("Bar", "12345")),
57
+ Klass(classid2, "include Quiescent", QConst("Foo", "54321"))].each do |k|
58
+ eval(k.to_s)
59
+ end
60
+
61
+ Kernel.const_get(classid1)::Bar.should == 12345
62
+ Kernel.const_get(classid2)::Foo.should == 54321
63
+
64
+ lambda {Kernel.const_get(classid1)::Foo}.should raise_error
65
+ lambda {Kernel.const_get(classid2)::Bar}.should raise_error
66
+ end
67
+
68
+ end
@@ -0,0 +1,70 @@
1
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
3
+ require 'rspec'
4
+ require 'quiescent'
5
+ require 'erb'
6
+ require 'digest/md5'
7
+
8
+ # Requires supporting files with custom matchers and macros, etc,
9
+ # in ./support/ and its subdirectories.
10
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
11
+
12
+ SClass = Struct.new(:name, :decls)
13
+ SQConst = Struct.new(:name, :default, :block)
14
+ SDo = Struct.new(:body)
15
+
16
+ class SDo
17
+ def to_s
18
+ "do ; #{body} ; end"
19
+ end
20
+ end
21
+
22
+ class SQConst
23
+ def to_s
24
+ "quiescent #{name.to_sym.inspect}#{(", " + default) if default} #{block if block}"
25
+ end
26
+ end
27
+
28
+ class SClass
29
+ TEMPLATE = ERB.new <<-END
30
+ class <%= name %>
31
+ <% decls.each do |decl| %> <%= decl.to_s %>
32
+ <% end %>
33
+ end
34
+ END
35
+ def to_s
36
+ TEMPLATE.result(binding)
37
+ end
38
+ end
39
+
40
+ def Klass(name, *decls)
41
+ SClass.new(name, decls)
42
+ end
43
+
44
+ def QConst(name, *values)
45
+ default, block = nil
46
+
47
+ if values.size == 1
48
+ if values[0].is_a?(SDo)
49
+ block = values[0]
50
+ else
51
+ default = values[0]
52
+ end
53
+ elsif values.size == 2
54
+ default, block = values
55
+ end
56
+
57
+ SQConst.new(name, default, block)
58
+ end
59
+
60
+ def Do(body)
61
+ SDo.new(body)
62
+ end
63
+
64
+ def gen_id(base)
65
+ "#{base}_#{Digest::MD5.hexdigest(Time.now.iso8601(11))}"
66
+ end
67
+
68
+ RSpec.configure do |config|
69
+
70
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: quiescent
3
+ version: !ruby/object:Gem::Version
4
+ hash: 25
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 1
10
+ version: 0.1.1
11
+ platform: ruby
12
+ authors:
13
+ - Will Benton
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-09-02 00:00:00 -05:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ requirement: &id001 !ruby/object:Gem::Requirement
23
+ none: false
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ hash: 3
28
+ segments:
29
+ - 2
30
+ - 3
31
+ - 0
32
+ version: 2.3.0
33
+ type: :development
34
+ name: rspec
35
+ prerelease: false
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
40
+ requirements:
41
+ - - ~>
42
+ - !ruby/object:Gem::Version
43
+ hash: 23
44
+ segments:
45
+ - 1
46
+ - 0
47
+ - 0
48
+ version: 1.0.0
49
+ type: :development
50
+ name: bundler
51
+ prerelease: false
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ requirement: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ~>
58
+ - !ruby/object:Gem::Version
59
+ hash: 7
60
+ segments:
61
+ - 1
62
+ - 5
63
+ - 2
64
+ version: 1.5.2
65
+ type: :development
66
+ name: jeweler
67
+ prerelease: false
68
+ version_requirements: *id003
69
+ - !ruby/object:Gem::Dependency
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :development
80
+ name: rcov
81
+ prerelease: false
82
+ version_requirements: *id004
83
+ description: This is a very simple mixin to support quiescing constants (which we call "quiescents") in Ruby. You may assign a value to a quiescing constant once during the execution of the program; however, a quiescing constant's value is fixed after the first time it is read. Quiescing constants may have default values (specified either as explicit values or argumentless blocks to compute that value) that take effect if they are not explicitly assigned to before their first use.
84
+ email: willb@redhat.com
85
+ executables: []
86
+
87
+ extensions: []
88
+
89
+ extra_rdoc_files:
90
+ - LICENSE.txt
91
+ - README.rdoc
92
+ files:
93
+ - .document
94
+ - .rspec
95
+ - Gemfile
96
+ - LICENSE.txt
97
+ - README.rdoc
98
+ - Rakefile
99
+ - VERSION
100
+ - lib/quiescent.rb
101
+ - quiescent.gemspec
102
+ - spec/quiescent_spec.rb
103
+ - spec/spec_helper.rb
104
+ has_rdoc: true
105
+ homepage: http://github.com/willb/quiescent
106
+ licenses:
107
+ - Apache v2
108
+ post_install_message:
109
+ rdoc_options: []
110
+
111
+ require_paths:
112
+ - lib
113
+ required_ruby_version: !ruby/object:Gem::Requirement
114
+ none: false
115
+ requirements:
116
+ - - ">="
117
+ - !ruby/object:Gem::Version
118
+ hash: 3
119
+ segments:
120
+ - 0
121
+ version: "0"
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ hash: 3
128
+ segments:
129
+ - 0
130
+ version: "0"
131
+ requirements: []
132
+
133
+ rubyforge_project:
134
+ rubygems_version: 1.3.7
135
+ signing_key:
136
+ specification_version: 3
137
+ summary: Simple library for write-once ("quiescing") constants
138
+ test_files:
139
+ - spec/quiescent_spec.rb
140
+ - spec/spec_helper.rb