sinatra-helpers 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,21 @@
1
+ ## MAC OS
2
+ .DS_Store
3
+
4
+ ## TEXTMATE
5
+ *.tmproj
6
+ tmtags
7
+
8
+ ## EMACS
9
+ *~
10
+ \#*
11
+ .\#*
12
+
13
+ ## VIM
14
+ *.swp
15
+
16
+ ## PROJECT::GENERAL
17
+ coverage
18
+ rdoc
19
+ pkg
20
+
21
+ ## PROJECT::SPECIFIC
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Cyril David
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,17 @@
1
+ = sinatra-helpers
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Cyril David. See LICENSE for details.
@@ -0,0 +1,52 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "sinatra-helpers"
8
+ gem.summary = %Q{Some generic helpers for the view layer}
9
+ gem.description = %Q{Includes month_choices, year_choices, country_choices}
10
+ gem.email = "cyx.ucron@gmail.com"
11
+ gem.homepage = "http://github.com/sinefunc/sinatra-helpers"
12
+ gem.authors = ["Cyril David"]
13
+ gem.add_development_dependency "contest", ">= 0"
14
+ end
15
+ Jeweler::GemcutterTasks.new
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/test_*.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/test_*.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
47
+
48
+ rdoc.rdoc_dir = 'rdoc'
49
+ rdoc.title = "sinatra-helpers #{version}"
50
+ rdoc.rdoc_files.include('README*')
51
+ rdoc.rdoc_files.include('lib/**/*.rb')
52
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.0
@@ -0,0 +1,59 @@
1
+ require 'date'
2
+
3
+ module Sinatra
4
+ module Helpers
5
+ autoload :HamlErrorPresenter, "sinatra/helpers/haml_error_presenter"
6
+ autoload :Country, "sinatra/helpers/country"
7
+
8
+ def country_choices
9
+ Country.to_select
10
+ end
11
+
12
+ def month_choices
13
+ Date::MONTHNAMES.map.
14
+ with_index { |month, i| ["%d - %s" % [i, month], i] }.
15
+ tap { |arr| arr.shift }
16
+ end
17
+
18
+ def year_choices(loffset = -20, uoffset = +20)
19
+ years = ((Date.today.year + loffset)..(Date.today.year + uoffset)).to_a
20
+ years.zip(years)
21
+ end
22
+
23
+ def select_options(pairs, current)
24
+ pairs.map { |label, value|
25
+ tag(:option, label, :value => value, :selected => (current == value))
26
+ }.join("\n")
27
+ end
28
+
29
+ def errors_on(object, options = { :class => 'errors' }, &block)
30
+ return if object.errors.empty?
31
+
32
+ lines = if object.errors.respond_to?(:full_messages)
33
+ object.errors.full_messages
34
+ else
35
+ HamlErrorPresenter.new(object.errors).present(self, &block)
36
+ end
37
+
38
+ haml_tag(:div, options) do
39
+ haml_tag(:ul) do
40
+ lines.each do |error|
41
+ haml_tag(:li, error)
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ protected
48
+ def tag(tag, content, atts = {})
49
+ tag_atts = atts.inject("") { |a, (k, v)|
50
+ a << ('%s="%s"' % [k, v]) if v
51
+ a
52
+ }
53
+
54
+ %(<#{ tag } #{ tag_atts }>#{ content }</#{ tag }>)
55
+ end
56
+ end
57
+
58
+ register Helpers
59
+ end
@@ -0,0 +1,289 @@
1
+ # encoding: UTF-8
2
+
3
+ module Sinatra
4
+ module Helpers
5
+ class Country
6
+ LOOKUP = {
7
+ :AF => "Afghanistan",
8
+ :AX => "Aland Islands",
9
+ :AL => "Albania",
10
+ :DZ => "Algeria",
11
+ :AS => "American Samoa",
12
+ :AD => "Andorra",
13
+ :AO => "Angola",
14
+ :AI => "Anguilla",
15
+ :AQ => "Antarctica",
16
+ :AG => "Antigua and Barbuda",
17
+ :AR => "Argentina",
18
+ :AM => "Armenia",
19
+ :AW => "Aruba",
20
+ :AU => "Australia",
21
+ :AT => "Austria",
22
+ :AZ => "Azerbaijan",
23
+ :BS => "Bahamas",
24
+ :BH => "Bahrain",
25
+ :BD => "Bangladesh",
26
+ :BB => "Barbados",
27
+ :BY => "Belarus",
28
+ :BE => "Belgium",
29
+ :BZ => "Belize",
30
+ :BJ => "Benin",
31
+ :BM => "Bermuda",
32
+ :BT => "Bhutan",
33
+ :BO => "Bolivia",
34
+ :BA => "Bosnia and Herzegovina",
35
+ :BW => "Botswana",
36
+ :BV => "Bouvet Island",
37
+ :BR => "Brazil",
38
+ :BQ => "British Antarctic Territory",
39
+ :IO => "British Indian Ocean Territory",
40
+ :VG => "British Virgin Islands",
41
+ :BN => "Brunei",
42
+ :BG => "Bulgaria",
43
+ :BF => "Burkina Faso",
44
+ :BI => "Burundi",
45
+ :KH => "Cambodia",
46
+ :CM => "Cameroon",
47
+ :CA => "Canada",
48
+ :CT => "Canton and Enderbury Islands",
49
+ :CV => "Cape Verde",
50
+ :KY => "Cayman Islands",
51
+ :CF => "Central African Republic",
52
+ :TD => "Chad",
53
+ :CL => "Chile",
54
+ :CN => "China",
55
+ :CX => "Christmas Island",
56
+ :CC => "Cocos Islands",
57
+ :CO => "Colombia",
58
+ :KM => "Comoros",
59
+ :CG => "Congo - Brazzaville",
60
+ :CD => "Congo - Kinshasa",
61
+ :CK => "Cook Islands",
62
+ :CR => "Costa Rica",
63
+ :HR => "Croatia",
64
+ :CU => "Cuba",
65
+ :CY => "Cyprus",
66
+ :CZ => "Czech Republic",
67
+ :DK => "Denmark",
68
+ :DJ => "Djibouti",
69
+ :DM => "Dominica",
70
+ :DO => "Dominican Republic",
71
+ :NQ => "Dronning Maud Land",
72
+ :DD => "East Germany",
73
+ :TL => "East Timor",
74
+ :EC => "Ecuador",
75
+ :EG => "Egypt",
76
+ :SV => "El Salvador",
77
+ :GQ => "Equatorial Guinea",
78
+ :ER => "Eritrea",
79
+ :EE => "Estonia",
80
+ :ET => "Ethiopia",
81
+ :QU => "European Union",
82
+ :FK => "Falkland Islands",
83
+ :FO => "Faroe Islands",
84
+ :FJ => "Fiji",
85
+ :FI => "Finland",
86
+ :FR => "France",
87
+ :GF => "French Guiana",
88
+ :PF => "French Polynesia",
89
+ :TF => "French Southern Territories",
90
+ :FQ => "French Southern and Antarctic Territories",
91
+ :GA => "Gabon",
92
+ :GM => "Gambia",
93
+ :GE => "Georgia",
94
+ :DE => "Germany",
95
+ :GH => "Ghana",
96
+ :GI => "Gibraltar",
97
+ :GR => "Greece",
98
+ :GL => "Greenland",
99
+ :GD => "Grenada",
100
+ :GP => "Guadeloupe",
101
+ :GU => "Guam",
102
+ :GT => "Guatemala",
103
+ :GG => "Guernsey",
104
+ :GN => "Guinea",
105
+ :GW => "Guinea-Bissau",
106
+ :GY => "Guyana",
107
+ :HT => "Haiti",
108
+ :HM => "Heard Island and McDonald Islands",
109
+ :HN => "Honduras",
110
+ :HK => "Hong Kong",
111
+ :HU => "Hungary",
112
+ :IS => "Iceland",
113
+ :IN => "India",
114
+ :ID => "Indonesia",
115
+ :IR => "Iran",
116
+ :IQ => "Iraq",
117
+ :IE => "Ireland",
118
+ :IM => "Isle of Man",
119
+ :IL => "Israel",
120
+ :IT => "Italy",
121
+ :CI => "Ivory Coast",
122
+ :JM => "Jamaica",
123
+ :JP => "Japan",
124
+ :JE => "Jersey",
125
+ :JT => "Johnston Island",
126
+ :JO => "Jordan",
127
+ :KZ => "Kazakhstan",
128
+ :KE => "Kenya",
129
+ :KI => "Kiribati",
130
+ :KW => "Kuwait",
131
+ :KG => "Kyrgyzstan",
132
+ :LA => "Laos",
133
+ :LV => "Latvia",
134
+ :LB => "Lebanon",
135
+ :LS => "Lesotho",
136
+ :LR => "Liberia",
137
+ :LY => "Libya",
138
+ :LI => "Liechtenstein",
139
+ :LT => "Lithuania",
140
+ :LU => "Luxembourg",
141
+ :MO => "Macau",
142
+ :MK => "Macedonia",
143
+ :MG => "Madagascar",
144
+ :MW => "Malawi",
145
+ :MY => "Malaysia",
146
+ :MV => "Maldives",
147
+ :ML => "Mali",
148
+ :MT => "Malta",
149
+ :MH => "Marshall Islands",
150
+ :MQ => "Martinique",
151
+ :MR => "Mauritania",
152
+ :MU => "Mauritius",
153
+ :YT => "Mayotte",
154
+ :FX => "Metropolitan France",
155
+ :MX => "Mexico",
156
+ :FM => "Micronesia",
157
+ :MI => "Midway Islands",
158
+ :MD => "Moldova",
159
+ :MC => "Monaco",
160
+ :MN => "Mongolia",
161
+ :ME => "Montenegro",
162
+ :MS => "Montserrat",
163
+ :MA => "Morocco",
164
+ :MZ => "Mozambique",
165
+ :MM => "Myanmar",
166
+ :NA => "Namibia",
167
+ :NR => "Nauru",
168
+ :NP => "Nepal",
169
+ :NL => "Netherlands",
170
+ :AN => "Netherlands Antilles",
171
+ :NT => "Neutral Zone",
172
+ :NC => "New Caledonia",
173
+ :NZ => "New Zealand",
174
+ :NI => "Nicaragua",
175
+ :NE => "Niger",
176
+ :NG => "Nigeria",
177
+ :NU => "Niue",
178
+ :NF => "Norfolk Island",
179
+ :KP => "North Korea",
180
+ :VD => "North Vietnam",
181
+ :MP => "Northern Mariana Islands",
182
+ :NO => "Norway",
183
+ :OM => "Oman",
184
+ :QO => "Outlying Oceania",
185
+ :PC => "Pacific Islands Trust Territory",
186
+ :PK => "Pakistan",
187
+ :PW => "Palau",
188
+ :PS => "Palestinian Territory",
189
+ :PA => "Panama",
190
+ :PZ => "Panama Canal Zone",
191
+ :PG => "Papua New Guinea",
192
+ :PY => "Paraguay",
193
+ :YD => "People's Democratic Republic of Yemen",
194
+ :PE => "Peru",
195
+ :PH => "Philippines",
196
+ :PN => "Pitcairn",
197
+ :PL => "Poland",
198
+ :PT => "Portugal",
199
+ :PR => "Puerto Rico",
200
+ :QA => "Qatar",
201
+ :RE => "Reunion",
202
+ :RO => "Romania",
203
+ :RU => "Russia",
204
+ :RW => "Rwanda",
205
+ :BL => "Saint Barthélemy",
206
+ :SH => "Saint Helena",
207
+ :KN => "Saint Kitts and Nevis",
208
+ :LC => "Saint Lucia",
209
+ :MF => "Saint Martin",
210
+ :PM => "Saint Pierre and Miquelon",
211
+ :VC => "Saint Vincent and the Grenadines",
212
+ :WS => "Samoa",
213
+ :SM => "San Marino",
214
+ :ST => "Sao Tome and Principe",
215
+ :SA => "Saudi Arabia",
216
+ :SN => "Senegal",
217
+ :RS => "Serbia",
218
+ :CS => "Serbia and Montenegro",
219
+ :SC => "Seychelles",
220
+ :SL => "Sierra Leone",
221
+ :SG => "Singapore",
222
+ :SK => "Slovakia",
223
+ :SI => "Slovenia",
224
+ :SB => "Solomon Islands",
225
+ :SO => "Somalia",
226
+ :ZA => "South Africa",
227
+ :GS => "South Georgia and the South Sandwich Islands",
228
+ :KR => "South Korea",
229
+ :ES => "Spain",
230
+ :LK => "Sri Lanka",
231
+ :SD => "Sudan",
232
+ :SR => "Suriname",
233
+ :SJ => "Svalbard and Jan Mayen",
234
+ :SZ => "Swaziland",
235
+ :SE => "Sweden",
236
+ :CH => "Switzerland",
237
+ :SY => "Syria",
238
+ :TW => "Taiwan",
239
+ :TJ => "Tajikistan",
240
+ :TZ => "Tanzania",
241
+ :TH => "Thailand",
242
+ :TG => "Togo",
243
+ :TK => "Tokelau",
244
+ :TO => "Tonga",
245
+ :TT => "Trinidad and Tobago",
246
+ :TN => "Tunisia",
247
+ :TR => "Turkey",
248
+ :TM => "Turkmenistan",
249
+ :TC => "Turks and Caicos Islands",
250
+ :TV => "Tuvalu",
251
+ :PU => "U.S. Miscellaneous Pacific Islands",
252
+ :VI => "U.S. Virgin Islands",
253
+ :UG => "Uganda",
254
+ :UA => "Ukraine",
255
+ :SU => "Union of Soviet Socialist Republics",
256
+ :AE => "United Arab Emirates",
257
+ :GB => "United Kingdom",
258
+ :US => "United States",
259
+ :UM => "United States Minor Outlying Islands",
260
+ :ZZ => "Unknown or Invalid Region",
261
+ :UY => "Uruguay",
262
+ :UZ => "Uzbekistan",
263
+ :VU => "Vanuatu",
264
+ :VA => "Vatican",
265
+ :VE => "Venezuela",
266
+ :VN => "Vietnam",
267
+ :WK => "Wake Island",
268
+ :WF => "Wallis and Futuna",
269
+ :EH => "Western Sahara",
270
+ :YE => "Yemen",
271
+ :ZM => "Zambia",
272
+ :ZW => "Zimbabwe"
273
+ }
274
+
275
+ def self.random_code
276
+ @to_a ||= LOOKUP.to_a
277
+ @to_a[rand(@to_a.size)].first
278
+ end
279
+
280
+ def self.to_select
281
+ LOOKUP.map { |code, name| [name, code] }
282
+ end
283
+
284
+ def self.[](code)
285
+ LOOKUP[code.to_sym] if not code.to_s.empty?
286
+ end
287
+ end
288
+ end
289
+ end
@@ -0,0 +1,18 @@
1
+ module Sinatra
2
+ module Helpers
3
+ # Taken from http://github.com/citrusbyte/reddit-clone
4
+ # Credit goes to citrusbyte.
5
+ class HamlErrorPresenter < Ohm::Validations::Presenter
6
+ def on(error, message = (block_given? ? @context.capture_haml { yield } : raise(ArgumentError)))
7
+ handle(error) do
8
+ @output << message
9
+ end
10
+ end
11
+
12
+ def present(context)
13
+ @context = context
14
+ super()
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'sinatra-helpers'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'helper'
2
+
3
+ class TestSinatraHelpers < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
metadata ADDED
@@ -0,0 +1,85 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-helpers
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Cyril David
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-05-06 00:00:00 +08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: contest
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :development
31
+ version_requirements: *id001
32
+ description: Includes month_choices, year_choices, country_choices
33
+ email: cyx.ucron@gmail.com
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.rdoc
41
+ files:
42
+ - .document
43
+ - .gitignore
44
+ - LICENSE
45
+ - README.rdoc
46
+ - Rakefile
47
+ - VERSION
48
+ - lib/sinatra/helpers.rb
49
+ - lib/sinatra/helpers/country.rb
50
+ - lib/sinatra/helpers/haml_error_presenter.rb
51
+ - test/helper.rb
52
+ - test/test_sinatra-helpers.rb
53
+ has_rdoc: true
54
+ homepage: http://github.com/sinefunc/sinatra-helpers
55
+ licenses: []
56
+
57
+ post_install_message:
58
+ rdoc_options:
59
+ - --charset=UTF-8
60
+ require_paths:
61
+ - lib
62
+ required_ruby_version: !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
69
+ required_rubygems_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ segments:
74
+ - 0
75
+ version: "0"
76
+ requirements: []
77
+
78
+ rubyforge_project:
79
+ rubygems_version: 1.3.6
80
+ signing_key:
81
+ specification_version: 3
82
+ summary: Some generic helpers for the view layer
83
+ test_files:
84
+ - test/helper.rb
85
+ - test/test_sinatra-helpers.rb