has_named_bootstraps 0.0.2

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/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.swp
2
+ has_named_bootstraps_test.db
3
+ rdoc
4
+ has_named_bootstraps.gemspec
5
+ pkg
data/COPYRIGHT ADDED
@@ -0,0 +1,14 @@
1
+ Copyright (C) 2010, Children's Hospital Boston
2
+
3
+ This library is free software: you can redistribute it and/or modify
4
+ it under the terms of the GNU Lesser General Public License as published by
5
+ the Free Software Foundation, either version 3 of the License, or
6
+ (at your option) any later version.
7
+
8
+ This program is distributed in the hope that it will be useful,
9
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
10
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
+ GNU Lesser General Public License for more details.
12
+
13
+ You should have received a copy of the GNU Lesser General Public License
14
+ along with this program. If not, see <http://www.gnu.org/licenses/>.
data/LGPL-V3-LICENSE ADDED
@@ -0,0 +1,165 @@
1
+ GNU LESSER GENERAL PUBLIC LICENSE
2
+ Version 3, 29 June 2007
3
+
4
+ Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
5
+ Everyone is permitted to copy and distribute verbatim copies
6
+ of this license document, but changing it is not allowed.
7
+
8
+
9
+ This version of the GNU Lesser General Public License incorporates
10
+ the terms and conditions of version 3 of the GNU General Public
11
+ License, supplemented by the additional permissions listed below.
12
+
13
+ 0. Additional Definitions.
14
+
15
+ As used herein, "this License" refers to version 3 of the GNU Lesser
16
+ General Public License, and the "GNU GPL" refers to version 3 of the GNU
17
+ General Public License.
18
+
19
+ "The Library" refers to a covered work governed by this License,
20
+ other than an Application or a Combined Work as defined below.
21
+
22
+ An "Application" is any work that makes use of an interface provided
23
+ by the Library, but which is not otherwise based on the Library.
24
+ Defining a subclass of a class defined by the Library is deemed a mode
25
+ of using an interface provided by the Library.
26
+
27
+ A "Combined Work" is a work produced by combining or linking an
28
+ Application with the Library. The particular version of the Library
29
+ with which the Combined Work was made is also called the "Linked
30
+ Version".
31
+
32
+ The "Minimal Corresponding Source" for a Combined Work means the
33
+ Corresponding Source for the Combined Work, excluding any source code
34
+ for portions of the Combined Work that, considered in isolation, are
35
+ based on the Application, and not on the Linked Version.
36
+
37
+ The "Corresponding Application Code" for a Combined Work means the
38
+ object code and/or source code for the Application, including any data
39
+ and utility programs needed for reproducing the Combined Work from the
40
+ Application, but excluding the System Libraries of the Combined Work.
41
+
42
+ 1. Exception to Section 3 of the GNU GPL.
43
+
44
+ You may convey a covered work under sections 3 and 4 of this License
45
+ without being bound by section 3 of the GNU GPL.
46
+
47
+ 2. Conveying Modified Versions.
48
+
49
+ If you modify a copy of the Library, and, in your modifications, a
50
+ facility refers to a function or data to be supplied by an Application
51
+ that uses the facility (other than as an argument passed when the
52
+ facility is invoked), then you may convey a copy of the modified
53
+ version:
54
+
55
+ a) under this License, provided that you make a good faith effort to
56
+ ensure that, in the event an Application does not supply the
57
+ function or data, the facility still operates, and performs
58
+ whatever part of its purpose remains meaningful, or
59
+
60
+ b) under the GNU GPL, with none of the additional permissions of
61
+ this License applicable to that copy.
62
+
63
+ 3. Object Code Incorporating Material from Library Header Files.
64
+
65
+ The object code form of an Application may incorporate material from
66
+ a header file that is part of the Library. You may convey such object
67
+ code under terms of your choice, provided that, if the incorporated
68
+ material is not limited to numerical parameters, data structure
69
+ layouts and accessors, or small macros, inline functions and templates
70
+ (ten or fewer lines in length), you do both of the following:
71
+
72
+ a) Give prominent notice with each copy of the object code that the
73
+ Library is used in it and that the Library and its use are
74
+ covered by this License.
75
+
76
+ b) Accompany the object code with a copy of the GNU GPL and this license
77
+ document.
78
+
79
+ 4. Combined Works.
80
+
81
+ You may convey a Combined Work under terms of your choice that,
82
+ taken together, effectively do not restrict modification of the
83
+ portions of the Library contained in the Combined Work and reverse
84
+ engineering for debugging such modifications, if you also do each of
85
+ the following:
86
+
87
+ a) Give prominent notice with each copy of the Combined Work that
88
+ the Library is used in it and that the Library and its use are
89
+ covered by this License.
90
+
91
+ b) Accompany the Combined Work with a copy of the GNU GPL and this license
92
+ document.
93
+
94
+ c) For a Combined Work that displays copyright notices during
95
+ execution, include the copyright notice for the Library among
96
+ these notices, as well as a reference directing the user to the
97
+ copies of the GNU GPL and this license document.
98
+
99
+ d) Do one of the following:
100
+
101
+ 0) Convey the Minimal Corresponding Source under the terms of this
102
+ License, and the Corresponding Application Code in a form
103
+ suitable for, and under terms that permit, the user to
104
+ recombine or relink the Application with a modified version of
105
+ the Linked Version to produce a modified Combined Work, in the
106
+ manner specified by section 6 of the GNU GPL for conveying
107
+ Corresponding Source.
108
+
109
+ 1) Use a suitable shared library mechanism for linking with the
110
+ Library. A suitable mechanism is one that (a) uses at run time
111
+ a copy of the Library already present on the user's computer
112
+ system, and (b) will operate properly with a modified version
113
+ of the Library that is interface-compatible with the Linked
114
+ Version.
115
+
116
+ e) Provide Installation Information, but only if you would otherwise
117
+ be required to provide such information under section 6 of the
118
+ GNU GPL, and only to the extent that such information is
119
+ necessary to install and execute a modified version of the
120
+ Combined Work produced by recombining or relinking the
121
+ Application with a modified version of the Linked Version. (If
122
+ you use option 4d0, the Installation Information must accompany
123
+ the Minimal Corresponding Source and Corresponding Application
124
+ Code. If you use option 4d1, you must provide the Installation
125
+ Information in the manner specified by section 6 of the GNU GPL
126
+ for conveying Corresponding Source.)
127
+
128
+ 5. Combined Libraries.
129
+
130
+ You may place library facilities that are a work based on the
131
+ Library side by side in a single library together with other library
132
+ facilities that are not Applications and are not covered by this
133
+ License, and convey such a combined library under terms of your
134
+ choice, if you do both of the following:
135
+
136
+ a) Accompany the combined library with a copy of the same work based
137
+ on the Library, uncombined with any other library facilities,
138
+ conveyed under the terms of this License.
139
+
140
+ b) Give prominent notice with the combined library that part of it
141
+ is a work based on the Library, and explaining where to find the
142
+ accompanying uncombined form of the same work.
143
+
144
+ 6. Revised Versions of the GNU Lesser General Public License.
145
+
146
+ The Free Software Foundation may publish revised and/or new versions
147
+ of the GNU Lesser General Public License from time to time. Such new
148
+ versions will be similar in spirit to the present version, but may
149
+ differ in detail to address new problems or concerns.
150
+
151
+ Each version is given a distinguishing version number. If the
152
+ Library as you received it specifies that a certain numbered version
153
+ of the GNU Lesser General Public License "or any later version"
154
+ applies to it, you have the option of following the terms and
155
+ conditions either of that published version or of any later version
156
+ published by the Free Software Foundation. If the Library as you
157
+ received it does not specify a version number of the GNU Lesser
158
+ General Public License, you may choose any version of the GNU Lesser
159
+ General Public License ever published by the Free Software Foundation.
160
+
161
+ If the Library as you received it specifies that a proxy can decide
162
+ whether future versions of the GNU Lesser General Public License shall
163
+ apply, that proxy's public statement of acceptance of any version is
164
+ permanent authorization for you to choose that version for the
165
+ Library.
data/README ADDED
@@ -0,0 +1,39 @@
1
+ = HasNamedBootstraps
2
+
3
+ *This is alpha code. The API is subject (and almost certain) to change. It shouldn't hurt your computer or data, but if it does, I am not obligated to do anything, even feel sorry for you.*
4
+
5
+ has_named_bootstraps is a simple way to load bootstrapped records into class-level constants, and generate a list in another constant of all such bootstraps. This is handy for generating <select>s and writing certain kinds of tests.
6
+
7
+ Getting the bootstrapped data in beforehand is up to you. I like seed_fu (http://github.com/mbleigh/seed-fu) for this.
8
+
9
+ = Example
10
+
11
+ class Department < ActiveRecord::Base
12
+ has_named_bootstraps(
13
+ :VALID_DEPARTMENTS, # name of the constant that'll hold the list of all bootstrap constants
14
+ :R_AND_D => 'R&D',
15
+ :MARKETING => 'Marketing',
16
+ :HANDSOME_DEVILS => 'Web Development'
17
+ )
18
+ end
19
+
20
+ >> Department.find(:all)
21
+ => [#<Department id: 1, name: "R&D">, #<Department id: 2, name: "Marketing">, #<Department id: 3, name: "Web Development">]
22
+ >> Department::R_AND_D
23
+ => #<Department id: 1, name: "R&D">
24
+ >> Department::MARKETING
25
+ => #<Department id: 2, name: "Marketing">
26
+ >> Department::HANDSOME_DEVILS
27
+ => #<Department id: 3, name: "Web Development">
28
+ >> Department::VALID_DEPARTMENTS
29
+ => [#<Department id: 1, name: "R&D">, #<Department id: 2, name: "Marketing">, #<Department id: 3, name: "Web Development">]
30
+
31
+ = TODO
32
+
33
+ -make master constant list optional
34
+ -allow caller to specify an attribute to use to find record (currently hardcoded to "name")
35
+ -write tests for shoulda macros, document same
36
+
37
+ = License and copyright
38
+
39
+ Copyright (c) 2010 Children's Hospital Boston, released under the GNU Lesser General Public License.
data/Rakefile ADDED
@@ -0,0 +1,53 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "has_named_bootstraps"
8
+ gem.summary = %Q{Load boostrapped database records into constants}
9
+ gem.description = %Q{has_named_bootstraps is a simple way to load bootstrapped records into class-level constants, and generate a list in another constant of all such bootstraps. This is handy for generating <select>s and writing certain kinds of tests.}
10
+ gem.email = "philip.darnowsky@childrens.harvard.edu"
11
+ gem.homepage = "http://github.com/chb/has_named_bootstraps"
12
+ gem.authors = ["Phil Darnowsky"]
13
+ gem.add_development_dependency "shoulda", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+
41
+ task :test => :check_dependencies
42
+
43
+ task :default => :test
44
+
45
+ require 'rake/rdoctask'
46
+ Rake::RDocTask.new do |rdoc|
47
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
48
+
49
+ rdoc.rdoc_dir = 'rdoc'
50
+ rdoc.title = "has_named_bootstraps #{version}"
51
+ rdoc.rdoc_files.include('README*')
52
+ rdoc.rdoc_files.include('lib/**/*.rb')
53
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.2
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require File.dirname(__FILE__) + "/rails/init"
@@ -0,0 +1,32 @@
1
+ module HasNamedBootstraps
2
+ def self.included(other)
3
+ other.extend(ClassMethods)
4
+ end
5
+
6
+ module ClassMethods
7
+ # For each key/value pair in constant_hash, look up the record with the value in the "name" attribute, and assign it to the constant denoted by the key. Assign a list of all such created constants to the constant named by valid_list_name.
8
+ #
9
+ # Example:
10
+ #
11
+ # class Department < ActiveRecord::Base
12
+ # has_named_bootstraps(
13
+ # :VALID_DEPARTMENTS,
14
+ # :R_AND_D => 'R&D',
15
+ # :MARKETING => 'Marketing',
16
+ # :HANDSOME_DEVILS => 'Web Development'
17
+ # )
18
+ # end
19
+ #
20
+ # Department::R_AND_D, Department::MARKETING and Department::HANDSOME_DEVILS are now all defined and point to an ActiveRecord object. Deparment::VALID_DEPARTMENTS is a list of those constants.
21
+
22
+ def has_named_bootstraps(valid_list_name, constant_hash)
23
+ valid_list = []
24
+
25
+ constant_hash.each do |constant_name, record_name|
26
+ valid_list << self.const_set(constant_name, self.find_by_name(record_name).freeze)
27
+ end
28
+
29
+ self.const_set(valid_list_name, valid_list.freeze)
30
+ end
31
+ end
32
+ end
data/rails/init.rb ADDED
@@ -0,0 +1 @@
1
+ ActiveRecord::Base.send(:include, HasNamedBootstraps)
@@ -0,0 +1,15 @@
1
+ class ActiveSupport::TestCase
2
+ def self.should_have_bootstraps(klass, expected_bootstraps)
3
+ context '' do
4
+ should "have the correct bootstrapped values" do
5
+
6
+ expected_bootstraps.each do |expected_bootstrap|
7
+ assert klass.const_get(expected_bootstrap), "Constant #{klass.to_s}::#{expected_bootstrap.to_s} not set"
8
+ end
9
+
10
+ assert_equal expected_bootstraps.length, klass.count, "Extraneous bootstrap values in #{klass.to_s}"
11
+ end
12
+ end
13
+ end
14
+ end
15
+
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class HasNamedBootstrapsTest < ActiveSupport::TestCase
4
+ CONSTANT_HASH = {
5
+ :R_AND_D => 'R&D',
6
+ :MARKETING => 'Marketing',
7
+ :HANDSOME_DEVILS => 'Web Development'
8
+ }
9
+
10
+ setup do
11
+ CONSTANT_HASH.values.each do |department_name|
12
+ Department.create!(:name => department_name)
13
+ end
14
+
15
+ Department.class_eval do
16
+ include HasNamedBootstraps
17
+
18
+ # Ignore "already initialized constant WHATEVER" warnings that we
19
+ # would otherwise get due to load_named_bootstraps running before every
20
+ # test, redefining constants as we go (which we want in this case)
21
+
22
+ old_verbose = $VERBOSE
23
+ $VERBOSE = nil
24
+ has_named_bootstraps(
25
+ :VALID_DEPARTMENTS,
26
+ CONSTANT_HASH
27
+ )
28
+ $VERBOSE = old_verbose
29
+ end
30
+ end
31
+
32
+ context "#has_named_bootstraps" do
33
+ should "create each expected constant" do
34
+ CONSTANT_HASH.each do |constant_name, department_name|
35
+ bootstrapped_constant = Department.const_get(constant_name)
36
+ assert_equal department_name, bootstrapped_constant.name
37
+ end
38
+ end
39
+
40
+ should "add each expected constant to the list of valid constants" do
41
+ expected_object_ids = CONSTANT_HASH.keys.map{|hash_symbol| Department.const_get(hash_symbol)}.map(&:id)
42
+ actual_object_ids = Department::VALID_DEPARTMENTS.map(&:id)
43
+
44
+ assert_same_elements expected_object_ids, actual_object_ids
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,23 @@
1
+ require 'rubygems'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
4
+ require 'active_record'
5
+ require 'active_record/fixtures'
6
+ require 'shoulda'
7
+ require 'ruby-debug'
8
+
9
+ require 'lib/has_named_bootstraps'
10
+
11
+ ActiveRecord::Base.establish_connection(
12
+ :adapter => 'sqlite3',
13
+ :database => 'has_named_bootstraps_test.db'
14
+ )
15
+
16
+ ActiveRecord::Schema.define do
17
+ create_table 'departments', :force => true do |t|
18
+ t.column 'name', :string
19
+ end
20
+ end
21
+
22
+ class Department < ActiveRecord::Base
23
+ end
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: has_named_bootstraps
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 2
10
+ version: 0.0.2
11
+ platform: ruby
12
+ authors:
13
+ - Phil Darnowsky
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-05-28 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: shoulda
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
32
+ version: "0"
33
+ type: :development
34
+ version_requirements: *id001
35
+ description: has_named_bootstraps is a simple way to load bootstrapped records into class-level constants, and generate a list in another constant of all such bootstraps. This is handy for generating <select>s and writing certain kinds of tests.
36
+ email: philip.darnowsky@childrens.harvard.edu
37
+ executables: []
38
+
39
+ extensions: []
40
+
41
+ extra_rdoc_files:
42
+ - README
43
+ files:
44
+ - .gitignore
45
+ - COPYRIGHT
46
+ - LGPL-V3-LICENSE
47
+ - README
48
+ - Rakefile
49
+ - VERSION
50
+ - init.rb
51
+ - lib/has_named_bootstraps.rb
52
+ - rails/init.rb
53
+ - shoulda_macros/has_named_boostrap_macros.rb
54
+ - test/test_has_named_bootstraps.rb
55
+ - test/test_helper.rb
56
+ has_rdoc: true
57
+ homepage: http://github.com/chb/has_named_bootstraps
58
+ licenses: []
59
+
60
+ post_install_message:
61
+ rdoc_options:
62
+ - --charset=UTF-8
63
+ require_paths:
64
+ - lib
65
+ required_ruby_version: !ruby/object:Gem::Requirement
66
+ none: false
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ hash: 3
71
+ segments:
72
+ - 0
73
+ version: "0"
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ hash: 3
80
+ segments:
81
+ - 0
82
+ version: "0"
83
+ requirements: []
84
+
85
+ rubyforge_project:
86
+ rubygems_version: 1.3.7
87
+ signing_key:
88
+ specification_version: 3
89
+ summary: Load boostrapped database records into constants
90
+ test_files:
91
+ - test/test_helper.rb
92
+ - test/test_has_named_bootstraps.rb