trenni-formatters 0.1.0
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 +17 -0
- data/.travis.yml +9 -0
- data/Gemfile +4 -0
- data/README.md +71 -0
- data/Rakefile +9 -0
- data/lib/trenni/formatters.rb +21 -0
- data/lib/trenni/formatters/formatter.rb +60 -0
- data/lib/trenni/formatters/html/definition_list_form.rb +179 -0
- data/lib/trenni/formatters/html/option_select.rb +107 -0
- data/lib/trenni/formatters/html/table_select.rb +103 -0
- data/lib/trenni/formatters/version.rb +25 -0
- data/test/test_formatters.rb +46 -0
- data/test/test_forms_formatter.rb +49 -0
- data/trenni-formatters.gemspec +29 -0
- metadata +97 -0
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,71 @@
|
|
1
|
+
# Trenni::Formatters
|
2
|
+
|
3
|
+
Trenni is a templating system, and these formatters assist with the development
|
4
|
+
of typical view and form based web interface. A formatter is a high-level
|
5
|
+
adapter that turns model data into presentation text.
|
6
|
+
|
7
|
+
Formatters are designed to be customised, typically per-project, for specific
|
8
|
+
formatting needs.
|
9
|
+
|
10
|
+
[](http://travis-ci.org/ioquatix/trenni-formatters)
|
11
|
+
|
12
|
+
## Installation
|
13
|
+
|
14
|
+
Add this line to your application's Gemfile:
|
15
|
+
|
16
|
+
gem 'trenni-formatters'
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install trenni-formatters
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
The most basic usage involves converting model data into presentation text by
|
29
|
+
a mapping corresponding to the objects type:
|
30
|
+
|
31
|
+
formatter = Trenni::Formatters::Formatter.new
|
32
|
+
|
33
|
+
formatter.for(String) do |value, options|
|
34
|
+
count += 1
|
35
|
+
"String: #{value}"
|
36
|
+
end
|
37
|
+
|
38
|
+
assert_equal "String: foobar", formatter.format("foobar")
|
39
|
+
|
40
|
+
## Contributing
|
41
|
+
|
42
|
+
1. Fork it
|
43
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
44
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
45
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
46
|
+
5. Create new Pull Request
|
47
|
+
|
48
|
+
## License
|
49
|
+
|
50
|
+
Released under the MIT license.
|
51
|
+
|
52
|
+
Copyright (c) 2012 [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams/).
|
53
|
+
|
54
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
55
|
+
a copy of this software and associated documentation files (the
|
56
|
+
"Software"), to deal in the Software without restriction, including
|
57
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
58
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
59
|
+
permit persons to whom the Software is furnished to do so, subject to
|
60
|
+
the following conditions:
|
61
|
+
|
62
|
+
The above copyright notice and this permission notice shall be
|
63
|
+
included in all copies or substantial portions of the Software.
|
64
|
+
|
65
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
66
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
67
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
68
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
69
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
70
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
71
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# Copyright (c) 2012 Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'trenni/formatters/formatter'
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# Copyright (c) 2012 Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'trenni/strings'
|
22
|
+
|
23
|
+
module Trenni
|
24
|
+
module Formatters
|
25
|
+
class Formatter
|
26
|
+
def initialize(options = {})
|
27
|
+
@options = options
|
28
|
+
@formatters = {}
|
29
|
+
end
|
30
|
+
|
31
|
+
def format(object, options = {})
|
32
|
+
if formatter = @formatters[object.class]
|
33
|
+
@formatters[object.class].call(object, options)
|
34
|
+
else
|
35
|
+
if object
|
36
|
+
Strings::to_html(object.to_s)
|
37
|
+
else
|
38
|
+
options[:blank] || ""
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
alias text format
|
44
|
+
|
45
|
+
def [] key
|
46
|
+
@options[key]
|
47
|
+
end
|
48
|
+
|
49
|
+
def for(klass, &block)
|
50
|
+
@formatters[klass] = block
|
51
|
+
end
|
52
|
+
|
53
|
+
protected
|
54
|
+
|
55
|
+
def merged_options(options = {})
|
56
|
+
@options.merge(options)
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,179 @@
|
|
1
|
+
# Copyright (c) 2012 Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'trenni/builder'
|
22
|
+
|
23
|
+
module Trenni
|
24
|
+
module Formatters
|
25
|
+
module HTML
|
26
|
+
module DefinitionListForm
|
27
|
+
# The target object of the form.
|
28
|
+
def object
|
29
|
+
@options[:object]
|
30
|
+
end
|
31
|
+
|
32
|
+
# The name of the field.
|
33
|
+
def name_for(options)
|
34
|
+
options[:field] || title_for(options).downcase.gsub(/\s+/, '_').to_sym
|
35
|
+
end
|
36
|
+
|
37
|
+
# The human presentable title for the field.
|
38
|
+
def title_for(options)
|
39
|
+
title = options[:title] || Strings::to_title(options[:field].to_s)
|
40
|
+
|
41
|
+
Strings::to_html(title)
|
42
|
+
end
|
43
|
+
|
44
|
+
# The value of the field.
|
45
|
+
def value_for(options)
|
46
|
+
value = options[:value]
|
47
|
+
|
48
|
+
if options[:object]
|
49
|
+
value ||= options[:object].send(name_for(options))
|
50
|
+
end
|
51
|
+
|
52
|
+
self.format(value, options)
|
53
|
+
end
|
54
|
+
|
55
|
+
def pattern_for(options)
|
56
|
+
nil
|
57
|
+
end
|
58
|
+
|
59
|
+
def input_attributes_for(options)
|
60
|
+
return {
|
61
|
+
:type => options[:type],
|
62
|
+
:name => name_for(options),
|
63
|
+
:value => value_for(options),
|
64
|
+
:required => options[:required] ? true : false,
|
65
|
+
:pattern => pattern_for(options)
|
66
|
+
}
|
67
|
+
end
|
68
|
+
|
69
|
+
# An input field (single line text).
|
70
|
+
def input(options = {})
|
71
|
+
options = @options.merge(options)
|
72
|
+
|
73
|
+
Builder.fragment do |builder|
|
74
|
+
builder.inline(:dt) { builder.text title_for(options) }
|
75
|
+
|
76
|
+
builder.inline(:dd) do
|
77
|
+
builder.tag :input, input_attributes_for(options)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
def textarea_attributes_for(options)
|
83
|
+
return {
|
84
|
+
:name => name_for(options),
|
85
|
+
:required => options[:required] ? true : false,
|
86
|
+
}
|
87
|
+
end
|
88
|
+
|
89
|
+
# A textarea field (multi-line text).
|
90
|
+
def textarea(options = {})
|
91
|
+
options = @options.merge(options)
|
92
|
+
|
93
|
+
Builder.fragment do |builder|
|
94
|
+
builder.inline(:dt) { builder.text title_for(options) }
|
95
|
+
|
96
|
+
builder.inline(:dd) do
|
97
|
+
builder.tag :textarea, textarea_attributes_for(options) do
|
98
|
+
builder.text value_for(options)
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def checkbox_attributes_for(options)
|
105
|
+
return {
|
106
|
+
:type => options[:type] || 'checkbox',
|
107
|
+
:checked => options[:object].send(name),
|
108
|
+
:name => name_for(options),
|
109
|
+
:required => options[:required] ? true : false,
|
110
|
+
:value => 'true',
|
111
|
+
}
|
112
|
+
end
|
113
|
+
|
114
|
+
# A checkbox field.
|
115
|
+
def checkbox(options)
|
116
|
+
options = @options.merge(options)
|
117
|
+
|
118
|
+
Builder.fragment do |builder|
|
119
|
+
builder.tag(:dd) do
|
120
|
+
builder.tag :input, :type => :hidden, :name => name_for(options), :value => 'false'
|
121
|
+
builder.tag :input, checkbox_attributes_for(options)
|
122
|
+
builder.text " " + title_for(options)
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
|
127
|
+
def submit_attributes_for(options)
|
128
|
+
return {
|
129
|
+
:type => options[:type] || 'submit',
|
130
|
+
:name => name_for(options),
|
131
|
+
:value => title_for(options),
|
132
|
+
}
|
133
|
+
end
|
134
|
+
|
135
|
+
# A submission button
|
136
|
+
def submit(options = {})
|
137
|
+
options = @options.merge(options)
|
138
|
+
|
139
|
+
unless options[:field]
|
140
|
+
options[:title] ||= self.object.saved? ? 'Update' : 'Create'
|
141
|
+
end
|
142
|
+
|
143
|
+
Builder.fragment do |builder|
|
144
|
+
builder.tag :input, submit_attributes_for(options)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
def hidden_attributes_for(options)
|
149
|
+
return {
|
150
|
+
:type => options[:type] || 'hidden',
|
151
|
+
:name => name_for(options),
|
152
|
+
:value => value_for(options),
|
153
|
+
}
|
154
|
+
end
|
155
|
+
|
156
|
+
# A hidden field.
|
157
|
+
def hidden(options = {})
|
158
|
+
options = @options.merge(options)
|
159
|
+
|
160
|
+
Builder.fragment do |builder|
|
161
|
+
builder.tag :input, hidden_attributes_for(options)
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
def element(klass, options = {}, &block)
|
166
|
+
options = @options.merge(options)
|
167
|
+
buffer = Trenni::Template.buffer(block.binding)
|
168
|
+
|
169
|
+
buffer << Builder.fragment do |builder|
|
170
|
+
builder.inline(:dt) { builder.text title_for(options) }
|
171
|
+
builder.tag(:dd) do
|
172
|
+
klass.call(self, options, builder, &block)
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
end
|
@@ -0,0 +1,107 @@
|
|
1
|
+
# Copyright (c) 2012 Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'trenni/builder'
|
22
|
+
|
23
|
+
module Trenni
|
24
|
+
module Formatters
|
25
|
+
module HTML
|
26
|
+
# Standard drop-down select box:
|
27
|
+
class OptionSelect
|
28
|
+
def self.call(formatter, options, builder, &block)
|
29
|
+
instance = self.new(formatter, options, builder)
|
30
|
+
|
31
|
+
instance.call(options, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize(formatter, options, builder)
|
35
|
+
@formatter = formatter
|
36
|
+
@object = formatter.object
|
37
|
+
@field = options[:field]
|
38
|
+
|
39
|
+
@builder = builder
|
40
|
+
end
|
41
|
+
|
42
|
+
def name_for(options)
|
43
|
+
@formatter.name_for(options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def value_for(options)
|
47
|
+
@formatter.value_for(options)
|
48
|
+
end
|
49
|
+
|
50
|
+
def title_for(options)
|
51
|
+
@formatter.title_for(options)
|
52
|
+
end
|
53
|
+
|
54
|
+
def item_attributes_for(options)
|
55
|
+
return {
|
56
|
+
:value => value_for(options),
|
57
|
+
:selected => options[:selected],
|
58
|
+
}
|
59
|
+
end
|
60
|
+
|
61
|
+
def item(options = {})
|
62
|
+
options[:field] ||= 'id'
|
63
|
+
|
64
|
+
Builder.fragment(options[:builder]) do |builder|
|
65
|
+
builder.inline(:option, option_attributes_for(options)) { builder.text title_for(options) }
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def group_attributes_for(options)
|
70
|
+
return {
|
71
|
+
:label => title_for(options)
|
72
|
+
}
|
73
|
+
end
|
74
|
+
|
75
|
+
def group(options = {}, &block)
|
76
|
+
Builder.fragment do |builder|
|
77
|
+
builder.tag :optgroup, group_attributes_for(options) do
|
78
|
+
if options[:optional]
|
79
|
+
item(:title => '', :value => '', :builder => builder)
|
80
|
+
end
|
81
|
+
|
82
|
+
builder.append Trenni::Template.capture(&block)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
def select_attributes_for(options)
|
88
|
+
return {
|
89
|
+
:name => name_for(options)
|
90
|
+
}
|
91
|
+
end
|
92
|
+
|
93
|
+
def call(options = {}, &block)
|
94
|
+
Builder.fragment(@builder) do |builder|
|
95
|
+
builder.tag :select, select_attributes_for(options) do
|
96
|
+
if options[:optional]
|
97
|
+
item(:title => '', :value => '', :builder => builder)
|
98
|
+
end
|
99
|
+
|
100
|
+
builder.append Trenni::Template.capture(self, &block)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# Copyright (c) 2012 Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
require 'trenni/builder'
|
22
|
+
|
23
|
+
module Trenni
|
24
|
+
module Formatters
|
25
|
+
module HTML
|
26
|
+
# Table based select boxes using per-row checkboxes.
|
27
|
+
class TableSelect
|
28
|
+
def self.call(formatter, options, builder, &block)
|
29
|
+
instance = self.new(formatter, options, builder)
|
30
|
+
|
31
|
+
instance.call(options, &block)
|
32
|
+
end
|
33
|
+
|
34
|
+
def initialize(formatter, options, builder)
|
35
|
+
@formatter = formatter
|
36
|
+
@object = formatter.object
|
37
|
+
@field = options[:field]
|
38
|
+
|
39
|
+
@builder = builder
|
40
|
+
end
|
41
|
+
|
42
|
+
def name_for(options)
|
43
|
+
@formatter.name_for(options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def value_for(options)
|
47
|
+
@formatter.value_for(options)
|
48
|
+
end
|
49
|
+
|
50
|
+
def title_for(options)
|
51
|
+
@formatter.title_for(options)
|
52
|
+
end
|
53
|
+
|
54
|
+
def radio_attributes_for(options)
|
55
|
+
return {
|
56
|
+
:type => :radio,
|
57
|
+
:name => @field,
|
58
|
+
:value => value_for(options),
|
59
|
+
:selected => options[:selected],
|
60
|
+
}
|
61
|
+
end
|
62
|
+
|
63
|
+
def item(options = {}, &block)
|
64
|
+
fragment = Builder.fragment do |builder|
|
65
|
+
builder.tag :tr do
|
66
|
+
builder.inline(:td, :class => :handle) do
|
67
|
+
builder.tag :input, radio_attributes_for(options)
|
68
|
+
end
|
69
|
+
|
70
|
+
builder.inline(:td, :class => :item) do
|
71
|
+
if block_given?
|
72
|
+
builder.append Trenni::Template.capture(self, &block)
|
73
|
+
else
|
74
|
+
builder.text title_for(options)
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
if block_given?
|
81
|
+
buffer = Trenni::buffer(block.binding)
|
82
|
+
buffer << fragment
|
83
|
+
|
84
|
+
return nil
|
85
|
+
else
|
86
|
+
return fragment
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def call(options = {}, &block)
|
91
|
+
Builder.fragment(@builder) do |builder|
|
92
|
+
builder.tag :table do
|
93
|
+
builder.tag :tbody do
|
94
|
+
builder.append Trenni::Template.capture(self, &block)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# Copyright (c) 2012 Samuel G. D. Williams. <http://www.codeotaku.com>
|
2
|
+
#
|
3
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
4
|
+
# of this software and associated documentation files (the "Software"), to deal
|
5
|
+
# in the Software without restriction, including without limitation the rights
|
6
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
7
|
+
# copies of the Software, and to permit persons to whom the Software is
|
8
|
+
# furnished to do so, subject to the following conditions:
|
9
|
+
#
|
10
|
+
# The above copyright notice and this permission notice shall be included in
|
11
|
+
# all copies or substantial portions of the Software.
|
12
|
+
#
|
13
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
14
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
15
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
16
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
+
# THE SOFTWARE.
|
20
|
+
|
21
|
+
module Trenni
|
22
|
+
module Formatters
|
23
|
+
VERSION = "0.1.0"
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Copyright (c) 2012 Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require 'pathname'
|
24
|
+
require 'test/unit'
|
25
|
+
require 'stringio'
|
26
|
+
|
27
|
+
require 'trenni'
|
28
|
+
require 'trenni/formatters'
|
29
|
+
|
30
|
+
class TestFormatters < Test::Unit::TestCase
|
31
|
+
def test_basic_formatter
|
32
|
+
count = 0
|
33
|
+
formatter = Trenni::Formatters::Formatter.new
|
34
|
+
|
35
|
+
formatter.for(String) do |value, options|
|
36
|
+
count += 1
|
37
|
+
"String: #{value}"
|
38
|
+
end
|
39
|
+
|
40
|
+
assert_equal "String: foobar", formatter.format("foobar")
|
41
|
+
assert_equal 1, count
|
42
|
+
|
43
|
+
assert_equal "10", formatter.format(10)
|
44
|
+
assert_equal 1, count
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
# Copyright (c) 2012 Samuel G. D. Williams. <http://www.codeotaku.com>
|
4
|
+
#
|
5
|
+
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
# of this software and associated documentation files (the "Software"), to deal
|
7
|
+
# in the Software without restriction, including without limitation the rights
|
8
|
+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
# copies of the Software, and to permit persons to whom the Software is
|
10
|
+
# furnished to do so, subject to the following conditions:
|
11
|
+
#
|
12
|
+
# The above copyright notice and this permission notice shall be included in
|
13
|
+
# all copies or substantial portions of the Software.
|
14
|
+
#
|
15
|
+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
# THE SOFTWARE.
|
22
|
+
|
23
|
+
require 'pathname'
|
24
|
+
require 'test/unit'
|
25
|
+
require 'stringio'
|
26
|
+
|
27
|
+
require 'trenni'
|
28
|
+
require 'trenni/formatters/html/definition_list_form'
|
29
|
+
require 'trenni/formatters/html/option_select'
|
30
|
+
require 'trenni/formatters/html/table_select'
|
31
|
+
|
32
|
+
class TestFormsFormatter < Test::Unit::TestCase
|
33
|
+
class BasicFormatter < Trenni::Formatters::Formatter
|
34
|
+
include Trenni::Formatters::HTML::DefinitionListForm
|
35
|
+
end
|
36
|
+
|
37
|
+
class Foo
|
38
|
+
attr :bar, true
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_basic_formatter
|
42
|
+
object = Foo.new
|
43
|
+
object.bar = 10
|
44
|
+
|
45
|
+
formatter = BasicFormatter.new(:object => object)
|
46
|
+
|
47
|
+
assert_equal %Q{<dt>Bar</dt>\n<dd><input name="bar" value="10"/></dd>}, formatter.input(:field => :bar)
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'trenni/formatters/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |gem|
|
7
|
+
gem.name = "trenni-formatters"
|
8
|
+
gem.version = Trenni::Formatters::VERSION
|
9
|
+
gem.authors = ["Samuel Williams"]
|
10
|
+
gem.email = ["samuel.williams@oriontransfer.co.nz"]
|
11
|
+
gem.description = <<-EOF
|
12
|
+
Trenni is a templating system, and these formatters assist with the development
|
13
|
+
of typical view and form based web interface. A formatter is a high-level
|
14
|
+
adapter that turns model data into presentation text.
|
15
|
+
|
16
|
+
Formatters are designed to be customised, typically per-project, for specific
|
17
|
+
formatting needs.
|
18
|
+
EOF
|
19
|
+
gem.summary = %q{Formatters for Trenni, to assist with typical views and form based interfaces.}
|
20
|
+
gem.homepage = "https://github.com/ioquatix/trenni-formatters"
|
21
|
+
|
22
|
+
gem.files = `git ls-files`.split($/)
|
23
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
24
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
25
|
+
gem.require_paths = ["lib"]
|
26
|
+
|
27
|
+
gem.add_dependency "trenni", "~> 1.0.2"
|
28
|
+
gem.add_dependency "redcarpet", "~> 2.1.1"
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,97 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: trenni-formatters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Samuel Williams
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-10-25 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: trenni
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 1.0.2
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ~>
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 1.0.2
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: redcarpet
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
33
|
+
none: false
|
34
|
+
requirements:
|
35
|
+
- - ~>
|
36
|
+
- !ruby/object:Gem::Version
|
37
|
+
version: 2.1.1
|
38
|
+
type: :runtime
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ~>
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 2.1.1
|
46
|
+
description: ! "\tTrenni is a templating system, and these formatters assist with
|
47
|
+
the development\n\tof typical view and form based web interface. A formatter is
|
48
|
+
a high-level\n\tadapter that turns model data into presentation text.\n\n\tFormatters
|
49
|
+
are designed to be customised, typically per-project, for specific\n\tformatting
|
50
|
+
needs.\n"
|
51
|
+
email:
|
52
|
+
- samuel.williams@oriontransfer.co.nz
|
53
|
+
executables: []
|
54
|
+
extensions: []
|
55
|
+
extra_rdoc_files: []
|
56
|
+
files:
|
57
|
+
- .gitignore
|
58
|
+
- .travis.yml
|
59
|
+
- Gemfile
|
60
|
+
- README.md
|
61
|
+
- Rakefile
|
62
|
+
- lib/trenni/formatters.rb
|
63
|
+
- lib/trenni/formatters/formatter.rb
|
64
|
+
- lib/trenni/formatters/html/definition_list_form.rb
|
65
|
+
- lib/trenni/formatters/html/option_select.rb
|
66
|
+
- lib/trenni/formatters/html/table_select.rb
|
67
|
+
- lib/trenni/formatters/version.rb
|
68
|
+
- test/test_formatters.rb
|
69
|
+
- test/test_forms_formatter.rb
|
70
|
+
- trenni-formatters.gemspec
|
71
|
+
homepage: https://github.com/ioquatix/trenni-formatters
|
72
|
+
licenses: []
|
73
|
+
post_install_message:
|
74
|
+
rdoc_options: []
|
75
|
+
require_paths:
|
76
|
+
- lib
|
77
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
78
|
+
none: false
|
79
|
+
requirements:
|
80
|
+
- - ! '>='
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
84
|
+
none: false
|
85
|
+
requirements:
|
86
|
+
- - ! '>='
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
requirements: []
|
90
|
+
rubyforge_project:
|
91
|
+
rubygems_version: 1.8.24
|
92
|
+
signing_key:
|
93
|
+
specification_version: 3
|
94
|
+
summary: Formatters for Trenni, to assist with typical views and form based interfaces.
|
95
|
+
test_files:
|
96
|
+
- test/test_formatters.rb
|
97
|
+
- test/test_forms_formatter.rb
|