sinatra-tag-helpers 0.0.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.
checksums.yaml ADDED
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ MGVlNzVlZWYzMTRmMWIxZTlmNDIwNmRjMTM0Yjc1YWQ3NzZjMDhlMg==
5
+ data.tar.gz: !binary |-
6
+ ODFhMmRmOThjZWM0MjZjOWExYTEyZjc3Mzc5ZDg5NjI0ZTFkMGJiYw==
7
+ !binary "U0hBNTEy":
8
+ metadata.gz: !binary |-
9
+ MDllZTg1NjQ3OGQwZjk5YmQxZGRkNTBhOWE3M2NhNTMxMjdlODg4ZmQ4Nzcz
10
+ YWUxMDJkMDRlNjBhMTA3NzJkZDFhYzY4YjFiZDI2ZTk3OTkwOWY4MWNjMDYx
11
+ YzIzOGRiNThiNWJkZTYxYWQ3MmU1ZjY5ZWViMDUwNjNjYTk0MmY=
12
+ data.tar.gz: !binary |-
13
+ YTNmMjMxNDE4ZWE0N2Q4MTE1YmJkOTViODNlNzZhMGVmMDE4MjE2YWM3YWQ3
14
+ NTVkMTk5MTA0NTI1NzA1ZDBjNzZhZGU2YzMyZmQ0NzgzMWE5YTQ2NjNlNjE5
15
+ ODMxZjBmNzE2MDE2MWU1Y2MwNzE0ZTEzOWI2ZjRiZGM0MWZhNTc=
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in sinatra-tag-helpers.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Evan Lecklider
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,31 @@
1
+ # Sinatra::TagHelpers
2
+
3
+ Some real simple tag helpers to make things easier for myself.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'sinatra-tag-helpers', :require => 'sinatra/tag-helpers'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install sinatra-tag-helpers
18
+
19
+ ## Usage
20
+
21
+ Classic style apps should be good to go and modular apps can do the old:
22
+
23
+ helpers Sinatra::TagHelpers
24
+
25
+ ## Contributing
26
+
27
+ 1. Fork it
28
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
29
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
30
+ 4. Push to the branch (`git push origin my-new-feature`)
31
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,5 @@
1
+ module Sinatra
2
+ module TagHelpers
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,199 @@
1
+ require 'sinatra/base'
2
+ require 'sinatra/tag-helpers/version'
3
+
4
+ module Sinatra
5
+ module TagHelpers
6
+ # don't even try it
7
+ def html_escape(text = '')
8
+ EscapeUtils.escape_html(text || '')
9
+ end
10
+ alias_method :h, :html_escape
11
+
12
+ # converts a hash into HTML style attributes
13
+ def to_attributes(hash)
14
+ hash.collect do |key, value|
15
+ # for things like data: { stuff: 'hey' }
16
+ if value.is_a? Hash
17
+ value.collect do |k, v|
18
+ "#{key}-#{k}='#{v}'"
19
+ end
20
+ else
21
+ value.is_a?(TrueClass) ? key.to_s : "#{key}='#{value}'"
22
+ end
23
+ end.join(' ').chomp
24
+ end
25
+
26
+ # mostly so we can override this if we need to look anywhere besides the params
27
+ # * cough * session * cough *
28
+ def param_value(param_name)
29
+ html_escape(params[param_name.to_sym] || '')
30
+ end
31
+
32
+ # link helper
33
+ # examples:
34
+ #
35
+ # link_to 'Overview', '/account/overview' # => <a href='/account/overview'>Overview</a>
36
+ #
37
+ # when on /account/overview
38
+ #
39
+ # <a href='/account/overview' class='current'>Overview</a>
40
+ #
41
+ def link_to(text, link, attributes = {})
42
+ if link == request.path_info
43
+ attributes[:class] = "#{ attributes[:class] } current"
44
+ end
45
+
46
+ attributes.merge!({ :href => to(link) })
47
+
48
+ "<a #{ to_attributes(attributes) }>#{ text }</a>"
49
+ end
50
+
51
+ # input_for creates an <input> tag with a number of configurable options
52
+ # if `param` is set in the `params` hash, the values from the `params` hash will be populated in the tag.
53
+ #
54
+ # <%= input_for 'something_hidden', type: 'hidden', value: 'Shhhhhh' %>
55
+ #
56
+ # Yields:
57
+ #
58
+ # <input type='hidden' name='something_hidden' id='something_hidden' value='Shhhhhh'>
59
+ #
60
+ def input_for(param, attributes = {})
61
+ attributes = {
62
+ :type => 'text',
63
+ :value => param_value(param),
64
+ :name => param,
65
+ :id => param
66
+ }.merge(attributes)
67
+
68
+ "<input #{ to_attributes(attributes) }>"
69
+ end
70
+
71
+ # radio_for creates an input tag of type radio and marks it `checked` if the param argument is set to the same value in the `params` hash
72
+ def radio_for(param, attributes = {})
73
+ attributes = {
74
+ :type => 'radio'
75
+ }.merge(attributes)
76
+
77
+ if param_value(param) == attributes[:value].to_s
78
+ attributes.merge!({ :checked => true })
79
+ end
80
+
81
+ input_for param, attributes
82
+ end
83
+
84
+ # checkbox_for creates an input of type checkbox with a `checked_if` argument to determine if it should be checked
85
+ #
86
+ # <%= checkbox_for 'is_cool', User.is_cool? %>
87
+ #
88
+ # Yields:
89
+ #
90
+ # <input type='checkbox' name='is_cool' id='is_cool' value='true'>
91
+ #
92
+ # Which will be marked with `checked` if `User.is_cool?` evaluates to true
93
+ #
94
+ def checkbox_for(param, checked_if, attributes = {})
95
+ attributes = {
96
+ :type => 'checkbox',
97
+ :value => 'true'
98
+ }.merge(attributes)
99
+
100
+ if checked_if || param_value(param) == attributes[:value].to_s
101
+ attributes.merge!({ checked: true })
102
+ end
103
+
104
+ input_for param, attributes
105
+ end
106
+
107
+ # creates a simple <textarea> tag
108
+ def textarea_for(param, attributes = {})
109
+ attributes = {
110
+ :name => param,
111
+ :id => param
112
+ }.merge(attributes)
113
+
114
+ "<textarea #{ attributes.to_attr }>#{ param_value(param) }</textarea>"
115
+ end
116
+
117
+ # option_for creates an <option> element with the specified attributes
118
+ # if the param specified is set to the value of this option tag then it is marked as 'selected'
119
+ # designed to be used within a <select> element
120
+ #
121
+ # <%= option_for 'turtles', key: 'I love them', value: 'love' %>
122
+ #
123
+ # Yields:
124
+ #
125
+ # <option value='love'>I love them</option>
126
+ #
127
+ # If params[:turtle] is set to 'love' this yields:
128
+ #
129
+ # <option value='love' selected>I love them</option>
130
+ #
131
+ def option_for(param, attributes = {})
132
+ default = attributes.delete(:default).to_s
133
+
134
+ if !params[param.to_sym].nil? && !params[param.to_sym].empty?
135
+ default = param_value(param)
136
+ end
137
+
138
+ attributes.merge!({ :selected => true }) if default == attributes[:value].to_s
139
+ key = attributes.delete(:key)
140
+
141
+ "<option #{ to_attributes(attributes) }>#{ key }</option>"
142
+ end
143
+
144
+ # select_for creates a <select> element with the specified attributes
145
+ # options are the available <option> tags within the <select> box
146
+ #
147
+ # <%= select_for 'days', { monday: 'Monday', myday: 'MY DAY!' } %>
148
+ #
149
+ # Yields:
150
+ #
151
+ # <select name='days' id='days' size='1'>
152
+ # <option value='monday'>Monday</option>
153
+ # <option value='myday'>MY DAY!</option>
154
+ # </select>
155
+ #
156
+ def select_for(param, options, attributes = {})
157
+ select = ["<select #{ to_attributes(attributes) } name='#{ param }' id='#{ param }' size='1'>"]
158
+
159
+ options.collect do |key, val|
160
+ select.push option_for(param, :key => key, :value => val, :default => attributes[:default])
161
+ end
162
+
163
+ select.push('</select>').join(' ').chomp
164
+ end
165
+
166
+ # shortcut to generate a month list
167
+ def months_for(param, attributes = {})
168
+ select_for(param, { 'Month' => '', '1 - January' => '01', '2 - February' => '02', '3 - March' => '03', '4 - April' => '04', '5 - May' => '05', '6 - June' => '06', '7 - July' => '07', '8 - August' => '08', '9 - September' => '09', '10 - October' => '10', '11 - November' => '11', '12 - December' => '12' }, attributes)
169
+ end
170
+
171
+ def years_for(param, range = 1940..Date.today.year, attributes = {})
172
+ options = { 'Year' => '' }
173
+
174
+ if range.last > range.first
175
+ # top down
176
+ range.last.downto(range.first) do |r|
177
+ options[r] = r
178
+ end
179
+ else
180
+ # bottom up
181
+ range.last.upto(range.first) do |r|
182
+ options[r] = r
183
+ end
184
+ end
185
+
186
+ select_for(param, options, attributes)
187
+ end
188
+
189
+ def days_for(param, attributes = {})
190
+ options = { 'Day' => '' }
191
+
192
+ (1..31).each do |day|
193
+ options[day] = day
194
+ end
195
+
196
+ select_for(param, options, attributes)
197
+ end
198
+ end
199
+ end
@@ -0,0 +1,24 @@
1
+ $:.unshift File.expand_path("../lib", __FILE__)
2
+ require 'sinatra/tag-helpers/version'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'sinatra-tag-helpers'
6
+ spec.version = Sinatra::TagHelpers::VERSION
7
+ spec.authors = ['Evan Lecklider']
8
+ spec.email = ['evan.lecklider@gmail.com']
9
+ spec.description = %q{Tag helpers (links and input tags) for Sinatra}
10
+ spec.summary = spec.description
11
+ spec.homepage = 'https://github.com/l3ck/sinatra-tag-helpers'
12
+
13
+ spec.files = `git ls-files`.split($/)
14
+ spec.executables = spec.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
15
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
16
+ spec.require_paths = ['lib']
17
+ spec.license = 'MIT'
18
+
19
+ spec.add_dependency 'sinatra', '~> 1.4.0'
20
+ spec.add_dependency 'escape_utils', '>= 0.3.0'
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.3'
23
+ spec.add_development_dependency 'rake'
24
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sinatra-tag-helpers
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Evan Lecklider
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-06 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sinatra
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 1.4.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 1.4.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: escape_utils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: 0.3.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ! '>='
39
+ - !ruby/object:Gem::Version
40
+ version: 0.3.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.3'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.3'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ description: Tag helpers (links and input tags) for Sinatra
70
+ email:
71
+ - evan.lecklider@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - .gitignore
77
+ - Gemfile
78
+ - LICENSE.txt
79
+ - README.md
80
+ - Rakefile
81
+ - lib/sinatra/tag-helpers.rb
82
+ - lib/sinatra/tag-helpers/version.rb
83
+ - sinatra-tag-helpers.gemspec
84
+ homepage: https://github.com/l3ck/sinatra-tag-helpers
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ! '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ! '>='
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.0.3
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Tag helpers (links and input tags) for Sinatra
108
+ test_files: []