trenni-formatters 2.8.1 → 2.12.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3394bc79205491b32b1ccda3d66f2bf61dac01306735d375c33568386bbae6c1
4
- data.tar.gz: 3d150680e6494f6b7abaa10eb3b4622f772c69b740c886de05d8c6b371151ffb
3
+ metadata.gz: 7fc81bd7424d62a51946525bfadb198b184223b85f39924b090c4917866e4e07
4
+ data.tar.gz: 2b68b00618ebff45b414dd7f4cd29eb770cdc4093ce0e80738a2b20dcba60377
5
5
  SHA512:
6
- metadata.gz: 62458096a57b44c43fd13cb5b03d7e75907eea995b8776d7dc60ebac74326b6747d8386c9757aa2e6fbf8549c084b6ffa5245dbf353c6394935fc9b80e4f6f9e
7
- data.tar.gz: 34a072f941bf47d5ebd6e9959064f5b18ad5bb8aee6002977540ec692e3bc8a33db6b695549f113e2b958a28edc50b956de0f97627cb4a1f5c5f627f262cfbee
6
+ metadata.gz: bb61c211734823ed5d5c198532d892bd881309c90799cdd939d025e0edc59582fcc11b92292c8cbfeaf748fc60ea81c883967aaf58c0f46bcd9067ba5c362bbd
7
+ data.tar.gz: 2fc540df7f46b5dd4209517143fbe50d603cfd9857000c86906bc50c2c0302d8ae467cdbc8758e00f6360ba3e6dd99ca79bb9b4a232af11e9ab8f9cacc18e6dd
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright (c) 2012 Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -79,17 +81,17 @@ module Trenni
79
81
 
80
82
  attr :options
81
83
 
82
- def format_unspecified(object, options)
84
+ def format_unspecified(object, **options)
83
85
  object.to_s
84
86
  end
85
87
 
86
- def format(object, options = {})
88
+ def format(object, **options)
87
89
  method_name = self.method_for_mapping(object)
88
90
 
89
91
  if self.respond_to?(method_name)
90
- self.send(method_name, object, options)
92
+ self.send(method_name, object, **options)
91
93
  else
92
- format_unspecified(object, options)
94
+ format_unspecified(object, **options)
93
95
  end
94
96
  end
95
97
 
@@ -99,15 +101,15 @@ module Trenni
99
101
  @options[key]
100
102
  end
101
103
 
102
- map(String) do |object, options|
104
+ map(String) do |object, **options|
103
105
  object
104
106
  end
105
107
 
106
- map(NilClass) do |object, options|
108
+ map(NilClass) do |object, **options|
107
109
  options[:blank] || @options[:blank] || ""
108
110
  end
109
111
 
110
- map(TrueClass, FalseClass, *Mapping.lookup_descendants(Numeric)) do |object, options|
112
+ map(TrueClass, FalseClass, *Mapping.lookup_descendants(Numeric)) do |object, **options|
111
113
  object.to_s
112
114
  end
113
115
  end
@@ -1,4 +1,6 @@
1
- # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2020, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
4
6
  # of this software and associated documentation files (the "Software"), to deal
@@ -18,40 +20,46 @@
18
20
  # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
21
  # THE SOFTWARE.
20
22
 
21
- require 'trenni/formatters'
23
+ require 'trenni/builder'
22
24
 
23
- module Trenni::FormattersSpec
24
- class TestFormatter < Trenni::Formatters::Formatter
25
- def initialize(*)
26
- @count = 0
27
-
28
- super
29
- end
30
-
31
- attr :count
32
-
33
- map(String) do |value, options|
34
- @count += 1
35
-
36
- "String: #{value}"
37
- end
38
- end
39
-
40
- describe Trenni::Formatters do
41
- let(:test_formatter) {TestFormatter.new(foo: :bar)}
42
-
43
- it "should format string" do
44
- expect(test_formatter.format("foobar")).to be == "String: foobar"
45
- expect(test_formatter.count).to be == 1
46
- end
47
-
48
- it "should format numbers" do
49
- expect(test_formatter.format(10)).to be == "10"
50
- expect(test_formatter.count).to be == 0
51
- end
52
-
53
- it "has options" do
54
- expect(test_formatter[:foo]).to be == :bar
25
+ module Trenni
26
+ module Formatters
27
+ module HTML
28
+ class AcceptCheckbox
29
+ def self.call(formatter, builder, **options, &block)
30
+ instance = self.new(formatter, builder, **options)
31
+
32
+ instance.call(&block)
33
+ end
34
+
35
+ def initialize(formatter, builder, **options)
36
+ @formatter = formatter
37
+ @builder = builder
38
+ @options = options
39
+ end
40
+
41
+ def name_for(**options)
42
+ @formatter.name_for(**options)
43
+ end
44
+
45
+ def checkbox_attributes_for(**options)
46
+ @formatter.checkbox_attributes_for(**options)
47
+ end
48
+
49
+ def call(&block)
50
+ Builder.fragment(@builder) do |builder|
51
+ builder.inline('span') do
52
+ builder.inline :input, type: :hidden, name: name_for(**@options), value: 'false'
53
+
54
+ builder.tag :input, checkbox_attributes_for(**@options)
55
+
56
+ builder.text " "
57
+
58
+ builder.capture(self, &block)
59
+ end
60
+ end
61
+ end
62
+ end
55
63
  end
56
64
  end
57
65
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -30,109 +32,117 @@ module Trenni
30
32
  include FormFormatter
31
33
 
32
34
  # An input field (single line text).
33
- def input(options = {})
34
- options = @options.merge(options)
35
+ def input(**options)
36
+ options = @options.merge(**options)
35
37
 
36
38
  Builder.fragment do |builder|
37
39
  builder.inline(:dt) do
38
- builder.text title_for(options)
40
+ builder.text title_for(**options)
39
41
  end
40
42
 
41
43
  builder.inline(:dd) do
42
- builder.tag :input, input_attributes_for(options)
44
+ builder.tag :input, input_attributes_for(**options)
43
45
 
44
- if details = details_for(options)
45
- builder.inline(:small, class: 'details') { builder.text details }
46
+ if details = details_for(**options)
47
+ builder.inline(:small, class: 'details') {builder.text details}
46
48
  end
47
49
  end
48
50
  end
49
51
  end
50
52
 
51
53
  # An output field for the result of a computation.
52
- def output(options = {})
53
- options = @options.merge(options)
54
+ def output(**options)
55
+ options = @options.merge(**options)
54
56
 
55
57
  Builder.fragment do |builder|
56
- builder.inline(:dt) { builder.text title_for(options) }
58
+ builder.inline(:dt) {builder.text title_for(**options)}
57
59
 
58
60
  builder.inline(:dd) do
59
- builder.inline :output, output_attributes_for(options) do
60
- builder.text value_for(options)
61
+ builder.inline :output, output_attributes_for(**options) do
62
+ builder.text value_for(**options)
61
63
  end
62
64
  end
63
65
  end
64
66
  end
65
67
 
66
68
  # A textarea field (multi-line text).
67
- def textarea(options = {})
68
- options = @options.merge(options)
69
+ def textarea(**options)
70
+ options = @options.merge(**options)
69
71
 
70
72
  Builder.fragment do |builder|
71
73
  builder.tag(:dt) do
72
- builder.text title_for(options)
74
+ builder.text title_for(**options)
73
75
 
74
- if details = details_for(options)
75
- builder.inline(:small, class: 'details') { builder.text details }
76
+ if details = details_for(**options)
77
+ builder.inline(:small, class: 'details') {builder.text details}
76
78
  end
77
79
  end
78
80
 
79
81
  builder.inline(:dd) do
80
- builder.tag :textarea, textarea_attributes_for(options) do
81
- builder.text value_for(options)
82
+ builder.tag :textarea, textarea_attributes_for(**options) do
83
+ builder.text value_for(**options)
82
84
  end
83
85
  end
84
86
  end
85
87
  end
86
88
 
87
89
  # A checkbox field.
88
- def checkbox(options)
89
- options = @options.merge(options)
90
+ def checkbox(**options)
91
+ options = @options.merge(**options)
90
92
 
91
93
  Builder.fragment do |builder|
92
94
  builder.tag(:dd) do
93
- builder.tag :input, :type => :hidden, :name => name_for(options), :value => 'false'
95
+ builder.tag :input, :type => :hidden, :name => name_for(**options), :value => 'false'
94
96
 
95
97
  builder.inline(:label) do
96
- builder.tag :input, checkbox_attributes_for(options)
98
+ builder.tag :input, checkbox_attributes_for(**options)
97
99
  # We would like a little bit of whitespace between the checkbox and the title.
98
- builder.text " " + title_for(options)
100
+ builder.text " " + title_for(**options)
99
101
  end
100
102
 
101
- if details = details_for(options)
102
- builder.inline(:small, class: 'details') { builder.text details }
103
+ if details = details_for(**options)
104
+ builder.inline(:small, class: 'details') {builder.text details}
103
105
  end
104
106
  end
105
107
  end
106
108
  end
107
109
 
108
110
  # A submission button
109
- def submit(options = {})
110
- options = @options.merge(options)
111
- options[:title] ||= submit_title_for(options)
111
+ def submit(**options)
112
+ options = @options.merge(**options)
113
+ options[:title] ||= submit_title_for(**options)
112
114
 
113
115
  Builder.fragment do |builder|
114
- builder.tag :input, submit_attributes_for(options)
116
+ builder.tag :input, submit_attributes_for(**options)
115
117
  end
116
118
  end
117
-
118
- def element(klass, options = {}, &block)
119
- options = @options.merge(options)
119
+
120
+ def element(klass, **options, &block)
121
+ options = @options.merge(**options)
120
122
  buffer = Trenni::Template.buffer(block.binding)
121
123
 
122
124
  buffer << Builder.fragment do |builder|
123
125
  builder.inline(:dt) do
124
- builder.text title_for(options)
126
+ builder.text title_for(**options)
125
127
  end
126
128
 
127
129
  builder.tag(:dd) do
128
- klass.call(self, options, builder, &block)
130
+ klass.call(self, builder, **options, &block)
129
131
 
130
- if details = details_for(options)
131
- builder.inline(:small, class: 'details') { builder.text details }
132
+ if details = details_for(**options)
133
+ builder.inline(:small, class: 'details') {builder.text details}
132
134
  end
133
135
  end
134
136
  end
135
137
  end
138
+
139
+ def fieldset(**options, &block)
140
+ super do |builder|
141
+ builder.tag(:dl) do
142
+ yield(builder)
143
+ end
144
+ end
145
+ end
136
146
  end
137
147
  end
138
148
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  # Copyright, 2012, by Samuel G. D. Williams. <http://www.codeotaku.com>
2
4
  #
3
5
  # Permission is hereby granted, free of charge, to any person obtaining a copy
@@ -30,83 +32,83 @@ module Trenni
30
32
  end
31
33
 
32
34
  # Any additional details relating to a field (e.g. explanation text)
33
- def details_for(options)
35
+ def details_for(**options)
34
36
  options[:details]
35
37
  end
36
38
 
37
- def field_for(options)
39
+ def field_for(**options)
38
40
  options[:field]
39
41
  end
40
42
 
41
43
  # A title is a text string that will be displayed next to or on top of the control to describe it or its value:
42
- def title_for(options)
44
+ def title_for(**options)
43
45
  if title = options[:title]
44
46
  return title
45
47
  end
46
48
 
47
49
  # Generate a title from a field name:
48
- if field_name = field_for(options)
50
+ if field_name = field_for(**options)
49
51
  # Remove postfix "_id" or "_ids":
50
52
  return Strings::to_title(field_name.to_s.sub(/_ids?/, ''))
51
53
  end
52
54
  end
53
55
 
54
- def object_value_for(options)
55
- if object = options[:object] and field = field_for(options)
56
+ def object_value_for(**options)
57
+ if object = options[:object] and field = field_for(**options)
56
58
  object.send(field)
57
59
  end
58
60
  end
59
61
 
60
- def raw_value_for(options)
61
- value = options.fetch(:value) { object_value_for(options) }
62
+ def raw_value_for(**options)
63
+ value = options.fetch(:value) {object_value_for(**options)}
62
64
 
63
65
  # Allow to specify a default value if the value given, usually from an object, is nil.
64
66
  value || options[:default]
65
67
  end
66
68
 
67
69
  # The value of the field.
68
- def value_for(options)
69
- if value = raw_value_for(options)
70
- self.format(value, options)
70
+ def value_for(**options)
71
+ if value = raw_value_for(**options)
72
+ self.format(value, **options)
71
73
  end
72
74
  end
73
75
 
74
- def pattern_for(options)
76
+ def pattern_for(**options)
75
77
  options[:pattern]
76
78
  end
77
79
 
78
- def placeholder_for(options)
80
+ def placeholder_for(**options)
79
81
  options[:placeholder]
80
82
  end
81
83
 
82
- def input_attributes_for(options)
84
+ def input_attributes_for(**options)
83
85
  attributes = {
84
86
  :type => options[:type],
85
- :name => name_for(options),
87
+ :name => name_for(**options),
86
88
  :id => options[:id],
87
89
  :class => options[:class],
88
- :value => value_for(options),
90
+ :value => value_for(**options),
89
91
  :required => options[:required],
90
92
  :disabled => options[:disabled],
91
93
  :readonly => options[:readonly],
92
- :pattern => pattern_for(options),
93
- :placeholder => placeholder_for(options),
94
+ :pattern => pattern_for(**options),
95
+ :placeholder => placeholder_for(**options),
94
96
  # for <input type="range|number">
95
- :min => options[:min],
96
- :max => options[:max],
97
+ :min => options[:minimum] || options[:min],
98
+ :max => options[:maximum] || options[:max],
97
99
  :step => options[:step],
98
100
  # for <input type="text">
99
- :minlength => options[:minlength],
100
- :maxlength => options[:maxlength],
101
+ :minlength => options[:minimum] || options[:minlength],
102
+ :maxlength => options[:maximum] || options[:maxlength],
101
103
  :data => options[:data],
102
104
  }
103
105
 
104
106
  return attributes
105
107
  end
106
108
 
107
- def output_attributes_for(options)
109
+ def output_attributes_for(**options)
108
110
  attributes = {
109
- :name => name_for(options),
111
+ :name => name_for(**options),
110
112
  :id => options[:id],
111
113
  :class => options[:class],
112
114
  :for => options[:for],
@@ -117,30 +119,30 @@ module Trenni
117
119
  return attributes
118
120
  end
119
121
 
120
- def textarea_attributes_for(options)
122
+ def textarea_attributes_for(**options)
121
123
  return {
122
- :name => name_for(options),
124
+ :name => name_for(**options),
123
125
  :id => options[:id],
124
126
  :class => options[:class],
125
127
  :required => options[:required],
126
128
  :disabled => options[:disabled],
127
129
  :readonly => options[:readonly],
128
- :pattern => pattern_for(options),
129
- :placeholder => placeholder_for(options),
130
+ :pattern => pattern_for(**options),
131
+ :placeholder => placeholder_for(**options),
130
132
  :minlength => options[:minlength],
131
133
  :maxlength => options[:maxlength],
132
134
  :data => options[:data],
133
135
  }
134
136
  end
135
137
 
136
- def checkbox_attributes_for(options)
138
+ def checkbox_attributes_for(**options)
137
139
  return {
138
140
  :type => options[:type] || 'checkbox',
139
141
  :id => options[:id],
140
142
  :class => options[:class],
141
- :name => name_for(options),
143
+ :name => name_for(**options),
142
144
  :value => 'true',
143
- :checked => raw_value_for(options),
145
+ :checked => raw_value_for(**options),
144
146
  :required => options[:required],
145
147
  :disabled => options[:disabled],
146
148
  :readonly => options[:readonly],
@@ -148,71 +150,86 @@ module Trenni
148
150
  }
149
151
  end
150
152
 
151
- def submit_attributes_for(options)
153
+ def submit_attributes_for(**options)
152
154
  return {
153
155
  :type => options[:type] || 'submit',
154
- :name => name_for(options),
156
+ :name => name_for(**options),
155
157
  :id => options[:id],
156
158
  :class => options[:class],
157
159
  :disabled => options[:disabled],
158
- :value => title_for(options),
160
+ :value => title_for(**options),
159
161
  :data => options[:data],
160
162
  }
161
163
  end
162
164
 
163
- def submit_title_for(options)
164
- title_for(options) || (new_record? ? 'Create' : 'Update')
165
+ def submit_title_for(**options)
166
+ title_for(**options) || (new_record? ? 'Create' : 'Update')
165
167
  end
166
168
 
167
- def hidden_attributes_for(options)
169
+ def hidden_attributes_for(**options)
168
170
  return {
169
171
  :type => options[:type] || 'hidden',
170
172
  :id => options[:id],
171
173
  :class => options[:class],
172
- :name => name_for(options),
173
- :value => value_for(options),
174
+ :name => name_for(**options),
175
+ :value => value_for(**options),
174
176
  :data => options[:data],
175
177
  }
176
178
  end
177
179
 
178
180
  # A hidden field.
179
- def hidden(options = {})
180
- options = @options.merge(options)
181
+ def hidden(**options)
182
+ options = @options.merge(**options)
181
183
 
182
184
  Builder.fragment do |builder|
183
- builder.tag :input, hidden_attributes_for(options)
185
+ builder.tag :input, hidden_attributes_for(**options)
184
186
  end
185
187
  end
186
188
 
187
- def button_attributes_for(options)
189
+ def button_attributes_for(**options)
188
190
  return {
189
191
  :type => options[:type] || 'submit',
190
- :name => name_for(options),
192
+ :name => name_for(**options),
191
193
  :id => options[:id],
192
194
  :class => options[:class],
193
195
  :disabled => options[:disabled],
194
- :value => value_for(options),
196
+ :value => value_for(**options),
195
197
  :data => options[:data],
196
198
  }
197
199
  end
198
200
 
199
- def button_title_for(options)
201
+ def button_title_for(**options)
200
202
  type = options.fetch(:type, 'submit').to_sym
201
203
 
202
204
  if type == :submit
203
- submit_title_for(options)
205
+ submit_title_for(**options)
204
206
  else
205
- title_for(options) || Strings::to_title(type.to_s)
207
+ title_for(**options) || Strings::to_title(type.to_s)
206
208
  end
207
209
  end
208
210
 
209
211
  # A hidden field.
210
- def button(options = {})
211
- options = @options.merge(options)
212
+ def button(**options)
213
+ options = @options.merge(**options)
212
214
 
213
215
  Builder.fragment do |builder|
214
- builder.inline :button, button_attributes_for(options) do
215
- builder.text button_title_for(options)
216
+ builder.inline :button, button_attributes_for(**options) do
217
+ builder.text button_title_for(**options)
218
+ end
219
+ end
220
+ end
221
+
222
+ def fieldset(**options, &block)
223
+ options = @options.merge(**options)
224
+ buffer = Trenni::Template.buffer(block.binding)
225
+
226
+ Builder.fragment(buffer) do |builder|
227
+ builder.tag('fieldset') do
228
+ builder.inline('legend') do
229
+ builder.text title_for(**options)
230
+ end
231
+
232
+ yield(builder)
216
233
  end
217
234
  end
218
235
  end