constantrecord 0.2.0 → 0.3.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/README.rdoc +6 -0
- data/VERSION +1 -1
- data/constantrecord.gemspec +5 -2
- data/lib/constantrecord.rb +38 -12
- data/samples/canadian_province.rb +16 -0
- data/samples/german_province.rb +24 -0
- data/samples/us_state.rb +56 -0
- data/test/test_constantrecord.rb +16 -1
- metadata +8 -5
data/README.rdoc
CHANGED
@@ -2,6 +2,8 @@
|
|
2
2
|
|
3
3
|
A tiny ActiveRecord substitute for small, never changing database tables.
|
4
4
|
|
5
|
+
*** Works with Rails 2 and Rails 3! ***
|
6
|
+
|
5
7
|
== Usage
|
6
8
|
|
7
9
|
class Currency < ConstantRecord::Base
|
@@ -103,6 +105,10 @@ In this case, you must also change your validation:
|
|
103
105
|
validates_inclusion_of :currency_id, :in => Currency.names
|
104
106
|
end
|
105
107
|
|
108
|
+
== Sample Data
|
109
|
+
|
110
|
+
This Gem features sample data for U.S. states, Canadian provinces and
|
111
|
+
territories and German Bundesländer. Check out the sample directory!
|
106
112
|
|
107
113
|
== Copyright
|
108
114
|
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.3.1
|
data/constantrecord.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{constantrecord}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.3.1"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Christoph Petschnig"]
|
12
|
-
s.date = %q{2010-
|
12
|
+
s.date = %q{2010-10-02}
|
13
13
|
s.description = %q{A tiny ActiveRecord substitute for small, never changing database tables.}
|
14
14
|
s.email = %q{info@purevirtual.de}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -23,6 +23,9 @@ Gem::Specification.new do |s|
|
|
23
23
|
"VERSION",
|
24
24
|
"constantrecord.gemspec",
|
25
25
|
"lib/constantrecord.rb",
|
26
|
+
"samples/canadian_province.rb",
|
27
|
+
"samples/german_province.rb",
|
28
|
+
"samples/us_state.rb",
|
26
29
|
"test/helper.rb",
|
27
30
|
"test/test_constantrecord.rb"
|
28
31
|
]
|
data/lib/constantrecord.rb
CHANGED
@@ -2,7 +2,7 @@ module ConstantRecord #:nodoc:
|
|
2
2
|
|
3
3
|
class ConstantNotFound < StandardError; end #:nodoc:
|
4
4
|
class LoggerError < StandardError; end #:nodoc:
|
5
|
-
|
5
|
+
|
6
6
|
# ConstantRecord::Base is a tiny ActiveRecord substitute for small, never
|
7
7
|
# changing database tables.
|
8
8
|
#
|
@@ -80,7 +80,11 @@ module ConstantRecord #:nodoc:
|
|
80
80
|
|
81
81
|
raise TypeError.new("#{self}.find failed!\nArguments: #{args.inspect}") unless selector.kind_of?(Symbol) || selector.kind_of?(Fixnum)
|
82
82
|
|
83
|
-
|
83
|
+
case selector
|
84
|
+
when :all
|
85
|
+
# ignore conditions on :all
|
86
|
+
return find_all if selector == :all
|
87
|
+
when :first
|
84
88
|
# no conditions given, return the first record
|
85
89
|
return self.new(1, *@data[0]) if args.size == 1
|
86
90
|
|
@@ -103,22 +107,35 @@ module ConstantRecord #:nodoc:
|
|
103
107
|
end
|
104
108
|
|
105
109
|
return nil
|
110
|
+
when :last
|
111
|
+
return self.new(@data.size, *@data[-1])
|
112
|
+
else
|
113
|
+
# ignore conditions if id is given as the first argument
|
114
|
+
return find_by_id(selector) if selector.kind_of?(Fixnum)
|
106
115
|
end
|
107
116
|
|
108
|
-
|
117
|
+
raise ArgumentError.new("#{self}.find failed!\nArguments: #{args.inspect}")
|
118
|
+
end
|
109
119
|
|
110
|
-
|
111
|
-
|
120
|
+
# shortcut to #find(:all)
|
121
|
+
def self.all(*args)
|
122
|
+
find_all
|
123
|
+
end
|
112
124
|
|
113
|
-
|
114
|
-
|
125
|
+
# shortcut to #find(:first)
|
126
|
+
def self.first(*args)
|
127
|
+
find(:first, *args)
|
128
|
+
end
|
115
129
|
|
116
|
-
|
130
|
+
# shortcut to #find(:last)
|
131
|
+
def self.last(*args)
|
132
|
+
find(:last, *args)
|
117
133
|
end
|
118
134
|
|
119
|
-
# shortcut to
|
120
|
-
def self.
|
121
|
-
|
135
|
+
# shortcut to retrieve value for name - eases dynamic lookup
|
136
|
+
def self.[](name)
|
137
|
+
ret = @data.detect{|datum| datum.first == name}
|
138
|
+
ret ? ret.last : raise(ConstantNotFound, "No such #{self.name} constant: #{name}")
|
122
139
|
end
|
123
140
|
|
124
141
|
# Implement +count+. Warning: <tt>:conditions</tt> are not supported!
|
@@ -129,7 +146,11 @@ module ConstantRecord #:nodoc:
|
|
129
146
|
# ignore conditions on :all
|
130
147
|
return @data.size if selector == :all
|
131
148
|
|
132
|
-
raise "#{self}.count failed!\nArguments: #{args.inspect}"
|
149
|
+
raise ArgumentError.new("#{self}.count failed!\nArguments: #{args.inspect}")
|
150
|
+
end
|
151
|
+
|
152
|
+
def [](attr)
|
153
|
+
instance_variable_get("@#{attr}")
|
133
154
|
end
|
134
155
|
|
135
156
|
# A ConstantRecord will never be a new record
|
@@ -142,6 +163,11 @@ module ConstantRecord #:nodoc:
|
|
142
163
|
false
|
143
164
|
end
|
144
165
|
|
166
|
+
# Rails 3.0: this method is checked in ActiveRecord::Base.compute_type
|
167
|
+
def self.match(*args)
|
168
|
+
true
|
169
|
+
end
|
170
|
+
|
145
171
|
# Show output in the form of `SELECT * FROM tablename;`
|
146
172
|
def self.table
|
147
173
|
# get columns in the form of {0 => :id, 1 => :name, ...}
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# All provinces and territories of Canada with their name, two-character
|
2
|
+
# abbreviation, their capital and population of the year 2010.
|
3
|
+
# Data taken from http://en.wikipedia.org/wiki/Provinces_and_territories_of_Canada
|
4
|
+
class CanadianProvince < ConstantRecord::Base #:nodoc:
|
5
|
+
columns :name, :abbr, :capital, :population
|
6
|
+
data [ 'Alberta', 'AB', 'Alberta', 3724832 ],
|
7
|
+
[ 'British Columbia', 'BC', 'British Columbia', 4510858 ],
|
8
|
+
[ 'Manitoba', 'MB', 'Manitoba', 1232654 ],
|
9
|
+
[ 'New Brunswick', 'NB', 'New Brunswick', 751273 ],
|
10
|
+
[ 'Newfoundland and Labrador', 'NL', 'Newfoundland and Labrador', 510901 ],
|
11
|
+
[ 'Nova Scotia', 'NS', 'Nova Scotia', 940482 ],
|
12
|
+
[ 'Ontario', 'ON', 'Ontario', 13167894 ],
|
13
|
+
[ 'Prince Edward Island', 'PE', 'Charlottetown', 141551 ],
|
14
|
+
[ 'Quebec', 'QC', 'Quebec', 7886108 ],
|
15
|
+
[ 'Saskatchewan', 'SK', 'Saskatchewan', 1041729 ]
|
16
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
|
3
|
+
# All provinces (Bundesländer) of Germany with their name, two-character
|
4
|
+
# abbreviation, their area in km² and population of the year 2007.
|
5
|
+
# Data taken from http://de.wikipedia.org/wiki/Land_(Deutschland)
|
6
|
+
class GermanProvince < ConstantRecord::Base #:nodoc:
|
7
|
+
columns :name, :abbr, :area, :population
|
8
|
+
data [ 'Baden-Württemberg', 'BW', 35751, 10750000 ],
|
9
|
+
[ 'Bayern', 'BY', 70552, 12520000 ],
|
10
|
+
[ 'Berlin', 'BE', 891, 3416000 ],
|
11
|
+
[ 'Brandenburg', 'BB', 29480, 2536000 ],
|
12
|
+
[ 'Bremen', 'HB', 419, 663000 ],
|
13
|
+
[ 'Hamburg', 'HH', 755, 1771000 ],
|
14
|
+
[ 'Hessen', 'HE', 21115, 6073000 ],
|
15
|
+
[ 'Mecklenburg-Vorpommern', 'MV', 23185, 1680000 ],
|
16
|
+
[ 'Niedersachsen', 'NI', 47625, 7972000 ],
|
17
|
+
[ 'Nordrhein-Westfalen', 'NW', 34086, 17997000 ],
|
18
|
+
[ 'Rheinland-Pfalz', 'RP', 19853, 4046000 ],
|
19
|
+
[ 'Saarland', 'SL', 2569, 1037000 ],
|
20
|
+
[ 'Sachsen', 'SN', 18418, 4220000 ],
|
21
|
+
[ 'Sachsen-Anhalt', 'ST', 20447, 2412000 ],
|
22
|
+
[ 'Schleswig-Holstein', 'SH', 15799, 2837000 ],
|
23
|
+
[ 'Thüringen', 'TH', 16172, 2289000 ]
|
24
|
+
end
|
data/samples/us_state.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# All states of the U.S.A. with their name, two-character abbreviation, area in
|
2
|
+
# square miles, population of the year 2009, the capital and the biggest city.
|
3
|
+
# Data taken from http://en.wikipedia.org/wiki/U.S._state
|
4
|
+
class USState < ConstantRecord::Base #:nodoc:
|
5
|
+
columns :name, :usps, :area, :population, :capital, :biggest_city
|
6
|
+
data ['Alabama', 'AL', 52419, 4708708, 'Montgomery', 'Birmingham' ],
|
7
|
+
['Alaska', 'AK', 663267, 698473, 'Juneau', 'Anchorage' ],
|
8
|
+
['Arizona', 'AZ', 113998, 6595778, 'Phoenix', 'Phoenix' ],
|
9
|
+
['Arkansas', 'AR', 52897, 2889450, 'Little Rock', 'Little Rock' ],
|
10
|
+
['California', 'CA', 163700, 36961664, 'Sacramento', 'Los Angeles' ],
|
11
|
+
['Colorado', 'CO', 104185, 5024748, 'Denver', 'Denver' ],
|
12
|
+
['Connecticut', 'CT', 5543, 3518288, 'Hartford', 'Bridgeport' ],
|
13
|
+
['Delaware', 'DE', 2491, 885122, 'Dover', 'Wilmington' ],
|
14
|
+
['Florida', 'FL', 65755, 18537969, 'Tallahassee', 'Jacksonville' ],
|
15
|
+
['Georgia', 'GA', 59425, 9829211, 'Atlanta', 'Atlanta' ],
|
16
|
+
['Hawaii', 'HI', 10931, 1295178, 'Honolulu', 'Honolulu' ],
|
17
|
+
['Idaho', 'ID', 83642, 1545801, 'Boise', 'Boise' ],
|
18
|
+
['Illinois', 'IL', 54440, 12910409, 'Springfield', 'Chicago' ],
|
19
|
+
['Indiana', 'IN', 36418, 6423113, 'Indianapolis', 'Indianapolis' ],
|
20
|
+
['Iowa', 'IA', 56272, 3007856, 'Des Moines', 'Des Moines' ],
|
21
|
+
['Kansas', 'KS', 82277, 2818747, 'Topeka', 'Wichita' ],
|
22
|
+
['Kentucky', 'KY', 40409, 4314113, 'Frankfort', 'Louisville' ],
|
23
|
+
['Louisiana', 'LA', 52271, 4492076, 'Baton Rouge', 'New Orleans' ],
|
24
|
+
['Maine', 'ME', 35385, 1318301, 'Augusta', 'Portland' ],
|
25
|
+
['Maryland', 'MD', 12407, 5699478, 'Annapolis', 'Baltimore' ],
|
26
|
+
['Massachusetts', 'MA', 10554, 6593587, 'Boston', 'Boston' ],
|
27
|
+
['Michigan', 'MI', 97990, 9969727, 'Lansing', 'Detroit' ],
|
28
|
+
['Minnesota', 'MN', 86943, 5266214, 'Saint Paul', 'Minneapolis' ],
|
29
|
+
['Mississippi', 'MS', 48434, 2951996, 'Jackson', 'Jackson' ],
|
30
|
+
['Missouri', 'MO', 69704, 5987580, 'Jefferson City', 'Kansas City' ],
|
31
|
+
['Montana', 'MT', 147165, 974989, 'Helena', 'Billings' ],
|
32
|
+
['Nebraska', 'NE', 77420, 1796619, 'Lincoln', 'Omaha' ],
|
33
|
+
['Nevada', 'NV', 110567, 2643085, 'Carson City', 'Las Vegas' ],
|
34
|
+
['New Hampshire', 'NH', 9350, 1324575, 'Concord', 'Manchester' ],
|
35
|
+
['New Jersey', 'NJ', 8729, 8707739, 'Trenton', 'Newark' ],
|
36
|
+
['New Mexico', 'NM', 121697, 2009671, 'Santa Fe', 'Albuquerque' ],
|
37
|
+
['New York', 'NY', 54556, 19541453, 'Albany', 'New York City' ],
|
38
|
+
['North Carolina', 'NC', 53865, 9380884, 'Raleigh', 'Charlotte' ],
|
39
|
+
['North Dakota', 'ND', 70762, 646844, 'Bismarck', 'Fargo' ],
|
40
|
+
['Ohio', 'OH', 44825, 11542645, 'Columbus', 'Columbus' ],
|
41
|
+
['Oklahoma', 'OK', 69960, 3687050, 'Oklahoma City', 'Oklahoma City' ],
|
42
|
+
['Oregon', 'OR', 98466, 3825657, 'Salem', 'Portland' ],
|
43
|
+
['Pennsylvania', 'PA', 46055, 12604767, 'Harrisburg', 'Philadelphia' ],
|
44
|
+
['Rhode Island', 'RI', 1210, 1053209, 'Providence', 'Providence' ],
|
45
|
+
['South Carolina', 'SC', 32020, 4561242, 'Columbia', 'Columbia' ],
|
46
|
+
['South Dakota', 'SD', 77184, 812383, 'Pierre', 'Sioux Falls' ],
|
47
|
+
['Tennessee', 'TN', 42181, 6296254, 'Nashville', 'Memphis' ],
|
48
|
+
['Texas', 'TX', 268820, 24782302, 'Austin', 'Houston' ],
|
49
|
+
['Utah', 'UT', 84899, 2784572, 'Salt Lake City', 'Salt Lake City'],
|
50
|
+
['Vermont', 'VT', 9623, 621760, 'Montpelier', 'Burlington' ],
|
51
|
+
['Virginia', 'VA', 42774, 7882590, 'Richmond', 'Virginia Beach'],
|
52
|
+
['Washington', 'WA', 71362, 6664195, 'Olympia', 'Seattle' ],
|
53
|
+
['West Virginia', 'WV', 24230, 1819777, 'Charleston', 'Charleston' ],
|
54
|
+
['Wisconsin', 'WI', 65498, 5654774, 'Madison', 'Milwaukee' ],
|
55
|
+
['Wyoming', 'WY', 97818, 544270, 'Cheyenne', 'Cheyenne' ]
|
56
|
+
end
|
data/test/test_constantrecord.rb
CHANGED
@@ -51,9 +51,15 @@ class TestConstantRecord < Test::Unit::TestCase
|
|
51
51
|
assert_raise (RuntimeError) { SimpleClass.find_by_foo('bar') }
|
52
52
|
assert_equal [ 'Lithuania', 'Latvia', 'Estonia' ], SimpleClass.find(:all).collect{|o| o.name}
|
53
53
|
assert_equal [ 1, 2, 3 ], SimpleClass.find(:all).collect{|o| o.id}
|
54
|
+
assert_equal [ 1, 2, 3 ], SimpleClass.find(:all, :ignored => 'blah').collect{|o| o.id}
|
55
|
+
assert_equal [ 1, 2, 3 ], SimpleClass.all.collect{|o| o.id}
|
56
|
+
assert_equal [ 1, 2, 3 ], SimpleClass.all(:ignored => 'blah').collect{|o| o.id}
|
54
57
|
assert_equal 3, SimpleClass.count
|
55
58
|
assert_equal 'Lithuania', SimpleClass.find(:first).name
|
59
|
+
assert_equal 'Lithuania', SimpleClass.first.name
|
60
|
+
assert_equal 'Estonia', SimpleClass.first(:conditions => {:name => "Estonia"}).name
|
56
61
|
assert_equal 'Estonia', SimpleClass.find(:last).name
|
62
|
+
assert_equal 'Estonia', SimpleClass.last.name
|
57
63
|
end
|
58
64
|
|
59
65
|
def test_simple_finder_with_custom_column_name
|
@@ -72,7 +78,7 @@ class TestConstantRecord < Test::Unit::TestCase
|
|
72
78
|
end
|
73
79
|
|
74
80
|
def test_multi_column_finder
|
75
|
-
all = MultiColumnClass.find(:all)
|
81
|
+
all = MultiColumnClass.find(:all, :conditions => {})
|
76
82
|
chf = all[4]
|
77
83
|
assert 5 == chf.id && chf.short && 'Swiss franc' == chf.description
|
78
84
|
|
@@ -86,6 +92,10 @@ class TestConstantRecord < Test::Unit::TestCase
|
|
86
92
|
assert_equal [ 'EUR', 'USD', 'CAD', 'GBP', 'CHF' ], MultiColumnClass.find(:all).collect{|o| o.short}
|
87
93
|
assert_equal [ 1, 2, 3, 4, 5 ], MultiColumnClass.find(:all).collect{|o| o.id}
|
88
94
|
assert_equal 5, MultiColumnClass.count
|
95
|
+
|
96
|
+
assert_equal 'Euro', MultiColumnClass['EUR']
|
97
|
+
assert_equal 'US Dollar', MultiColumnClass['USD']
|
98
|
+
assert_raise (ConstantRecord::ConstantNotFound) { MultiColumnClass['BadValue'] }
|
89
99
|
end
|
90
100
|
|
91
101
|
def test_multi_column_not_string_finder
|
@@ -137,4 +147,9 @@ class TestConstantRecord < Test::Unit::TestCase
|
|
137
147
|
BadColumnNames.columns :instance_method, :class_variable # must log 2 warnings
|
138
148
|
assert_equal ConstantRecord::Base.logger.warn_count - warnings_count, 2
|
139
149
|
end
|
150
|
+
|
151
|
+
def test_attribute_accessors
|
152
|
+
# TODO!
|
153
|
+
end
|
154
|
+
|
140
155
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: constantrecord
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 17
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 3
|
9
|
+
- 1
|
10
|
+
version: 0.3.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Christoph Petschnig
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-
|
18
|
+
date: 2010-10-02 00:00:00 +02:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -35,6 +35,9 @@ files:
|
|
35
35
|
- VERSION
|
36
36
|
- constantrecord.gemspec
|
37
37
|
- lib/constantrecord.rb
|
38
|
+
- samples/canadian_province.rb
|
39
|
+
- samples/german_province.rb
|
40
|
+
- samples/us_state.rb
|
38
41
|
- test/helper.rb
|
39
42
|
- test/test_constantrecord.rb
|
40
43
|
has_rdoc: true
|