input 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/input.rb +157 -0
- data/lib/input/version.rb +7 -0
- metadata +47 -0
data/lib/input.rb
ADDED
@@ -0,0 +1,157 @@
|
|
1
|
+
require 'input/version'
|
2
|
+
|
3
|
+
module Input
|
4
|
+
|
5
|
+
def self.form(obj=nil, attributes={}, &block)
|
6
|
+
Form.begin(obj, attributes, &block)
|
7
|
+
end
|
8
|
+
|
9
|
+
class Form
|
10
|
+
|
11
|
+
# The object related to the form, if any.
|
12
|
+
attr_reader :obj
|
13
|
+
|
14
|
+
# the final output of the form being built
|
15
|
+
attr_accessor :output
|
16
|
+
|
17
|
+
##
|
18
|
+
#
|
19
|
+
# Create a new instance tied to an object and initialize output.
|
20
|
+
#
|
21
|
+
#
|
22
|
+
# @param [Object] obj
|
23
|
+
# Sets the object for the form (if any)
|
24
|
+
#
|
25
|
+
def initialize(obj)
|
26
|
+
@obj = obj
|
27
|
+
@output = ''
|
28
|
+
end
|
29
|
+
|
30
|
+
##
|
31
|
+
#
|
32
|
+
# Create a form object and yeild to block
|
33
|
+
#
|
34
|
+
# @param [Object] obj
|
35
|
+
# Sets the object for the form (if any)
|
36
|
+
#
|
37
|
+
def self.begin(obj, attributes={}, &block)
|
38
|
+
# build form object
|
39
|
+
form = new(obj)
|
40
|
+
|
41
|
+
# construct form
|
42
|
+
form.build(Tag.new(self, :form, attributes), &block)
|
43
|
+
|
44
|
+
# return final output
|
45
|
+
form.output
|
46
|
+
end
|
47
|
+
|
48
|
+
##
|
49
|
+
#
|
50
|
+
# Create a tag, build it, and yield to block if needed
|
51
|
+
#
|
52
|
+
def tag(type, attributes={}, &block)
|
53
|
+
tag = Tag.new(self, type, attributes)
|
54
|
+
build(tag, &block)
|
55
|
+
end
|
56
|
+
|
57
|
+
##
|
58
|
+
#
|
59
|
+
# Create input tag, build it, and yield to block if needed
|
60
|
+
#
|
61
|
+
def input(type, attributes={}, &block)
|
62
|
+
tag = Input.new(self, type, attributes)
|
63
|
+
build(tag, &block)
|
64
|
+
end
|
65
|
+
|
66
|
+
##
|
67
|
+
#
|
68
|
+
# Create input tag, build it, and yield to block if needed
|
69
|
+
#
|
70
|
+
def build(tag, &block)
|
71
|
+
@output << tag.open
|
72
|
+
yield self if block_given?
|
73
|
+
@output << tag.close
|
74
|
+
end
|
75
|
+
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
class Tag
|
80
|
+
|
81
|
+
attr_accessor :form
|
82
|
+
attr_accessor :type
|
83
|
+
attr_accessor :attributes
|
84
|
+
|
85
|
+
# Which tags are self closing (such tags ignore children).
|
86
|
+
SELF_CLOSING = [:img, :input]
|
87
|
+
|
88
|
+
# Borrowed from Rack::Utils, map of single character strings to html escaped versions.
|
89
|
+
ESCAPE_HTML = {"&" => "&", "<" => "<", ">" => ">", "'" => "'", '"' => """}
|
90
|
+
|
91
|
+
# A regexp that matches all html characters requiring escaping.
|
92
|
+
ESCAPE_HTML_PATTERN = Regexp.union(*ESCAPE_HTML.keys)
|
93
|
+
|
94
|
+
##
|
95
|
+
#
|
96
|
+
# Create a new tag.
|
97
|
+
#
|
98
|
+
def initialize(form, type, attributes={})
|
99
|
+
@form = form
|
100
|
+
@type = type
|
101
|
+
@attributes = attributes
|
102
|
+
end
|
103
|
+
|
104
|
+
##
|
105
|
+
#
|
106
|
+
# Return opening half of tag, or entire tag if self closing
|
107
|
+
#
|
108
|
+
def open
|
109
|
+
if SELF_CLOSING.include?(@type)
|
110
|
+
"<#{@type}#{attr_html(attributes)}/>"
|
111
|
+
else
|
112
|
+
"<#{@type}#{attr_html(attributes)}>"
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
##
|
117
|
+
#
|
118
|
+
# Return opening half of tag, or nothing tag if self closing
|
119
|
+
#
|
120
|
+
def close
|
121
|
+
if SELF_CLOSING.include?(type)
|
122
|
+
""
|
123
|
+
else
|
124
|
+
"</#{type}>"
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
private
|
129
|
+
|
130
|
+
##
|
131
|
+
#
|
132
|
+
# Escape ampersands, brackets and quotes to their HTML/XML entities.
|
133
|
+
#
|
134
|
+
def h(string)
|
135
|
+
string.to_s.gsub(ESCAPE_HTML_PATTERN){|c| ESCAPE_HTML[c] }
|
136
|
+
end
|
137
|
+
|
138
|
+
##
|
139
|
+
#
|
140
|
+
# Transforms attributes hash into html, quoting and escaping values.
|
141
|
+
#
|
142
|
+
def attr_html(attr)
|
143
|
+
attr = attr.to_a.reject{|k,v| v.nil?}
|
144
|
+
" #{attr.map{|k, v| "#{k}=\"#{h(v)}\""}.sort.join(' ')}" unless attr.empty?
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
class Input < Tag
|
150
|
+
|
151
|
+
def initialize(form, type, attributes={})
|
152
|
+
super(form, :input, attributes.merge({:type=>type}))
|
153
|
+
end
|
154
|
+
|
155
|
+
end
|
156
|
+
|
157
|
+
end
|
metadata
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: input
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Tyler Kellen
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-17 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: Input is a framework-agnostic HTML5 form library with no external dependencies.
|
15
|
+
email: tyler@sleekcode.net
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- lib/input/version.rb
|
21
|
+
- lib/input.rb
|
22
|
+
homepage: https://github.com/tkellen/input
|
23
|
+
licenses: []
|
24
|
+
post_install_message:
|
25
|
+
rdoc_options: []
|
26
|
+
require_paths:
|
27
|
+
- lib
|
28
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ! '>='
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
35
|
+
none: false
|
36
|
+
requirements:
|
37
|
+
- - ! '>='
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
requirements: []
|
41
|
+
rubyforge_project: input
|
42
|
+
rubygems_version: 1.8.10
|
43
|
+
signing_key:
|
44
|
+
specification_version: 3
|
45
|
+
summary: A lightweight HTML5 form library.
|
46
|
+
test_files: []
|
47
|
+
has_rdoc:
|