trenni-formatters 2.11.0 → 2.13.1

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: 6d6feaaa592cf7b7ee7470871eb4a169485b1141978ca45fc0bae655e76494fc
4
- data.tar.gz: 78e3dec788ea4b9439d64a281f13b1dd8a4b4845d65340d595e4e8489437d93c
3
+ metadata.gz: c0ffa3d064d785fe2c64b78b697ca516c180aa6294d460b171d1e447a021bb67
4
+ data.tar.gz: b9c6d99fa176f30c276786858a17a7aade76ba97b0ab985716b2cfc7deee179c
5
5
  SHA512:
6
- metadata.gz: 1ed0633355b499980dadbcb900a651020f9718d43bae54b8dbbe2f8e0c491d06dec7bb9cf63e4eab58f6aa5bdba84742a31f1824711c124e95c2a24f8930fd0f
7
- data.tar.gz: 151c6d08577bfc6481591c3b2c0733083b5d847c02f633525c5225629dc717f45b532b6c957fb27450cda2787e88d939e63d1bc4e6c15fe15bc4e570f2b78c4b
6
+ metadata.gz: c25a21883a18453e50f6ad2a34e2539e20271433f6df3a71b742858f7bfb57b10e915dffebfc87880f087c200104e947311250515afc181bc399ef3b609c001c
7
+ data.tar.gz: 07c4b5ef4dfb1527df0969fdeffa6cee6b637ef80b0bc88c43e45110617b3ea951ec2c7922dcc9d95b18b72eb5086e21d97815c9a421af6cfb3169a85728777a
Binary file
Binary file
@@ -81,17 +81,17 @@ module Trenni
81
81
 
82
82
  attr :options
83
83
 
84
- def format_unspecified(object, options)
84
+ def format_unspecified(object, **options)
85
85
  object.to_s
86
86
  end
87
87
 
88
- def format(object, options = {})
88
+ def format(object, **options)
89
89
  method_name = self.method_for_mapping(object)
90
90
 
91
91
  if self.respond_to?(method_name)
92
- self.send(method_name, object, options)
92
+ self.send(method_name, object, **options)
93
93
  else
94
- format_unspecified(object, options)
94
+ format_unspecified(object, **options)
95
95
  end
96
96
  end
97
97
 
@@ -101,15 +101,15 @@ module Trenni
101
101
  @options[key]
102
102
  end
103
103
 
104
- map(String) do |object, options|
104
+ map(String) do |object, **options|
105
105
  object
106
106
  end
107
107
 
108
- map(NilClass) do |object, options|
108
+ map(NilClass) do |object, **options|
109
109
  options[:blank] || @options[:blank] || ""
110
110
  end
111
111
 
112
- map(TrueClass, FalseClass, *Mapping.lookup_descendants(Numeric)) do |object, options|
112
+ map(TrueClass, FalseClass, *Mapping.lookup_descendants(Numeric)) do |object, **options|
113
113
  object.to_s
114
114
  end
115
115
  end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Copyright, 2020, by 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 'trenni/builder'
24
+
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
63
+ end
64
+ end
65
+ end
@@ -117,28 +117,10 @@ module Trenni
117
117
  end
118
118
  end
119
119
 
120
- def fieldset(**options, &block)
121
- options = @options.merge(**options)
122
- buffer = Trenni::Template.buffer(block.binding)
123
-
124
- Builder.fragment(buffer) do |builder|
125
- builder.tag('fieldset') do
126
- builder.inline('legend') do
127
- builder.text title_for(**options)
128
- end
129
-
130
- builder.tag('dl') do
131
- yield(builder)
132
- end
133
- end
134
- end
135
- end
136
-
137
120
  def element(klass, **options, &block)
138
121
  options = @options.merge(**options)
139
- buffer = Trenni::Template.buffer(block.binding)
140
122
 
141
- buffer << Builder.fragment do |builder|
123
+ Builder.fragment(block&.binding) do |builder|
142
124
  builder.inline(:dt) do
143
125
  builder.text title_for(**options)
144
126
  end
@@ -155,7 +137,9 @@ module Trenni
155
137
 
156
138
  def fieldset(**options, &block)
157
139
  super do |builder|
158
- builder.tag(:dl, &block)
140
+ builder.tag(:dl) do
141
+ yield(builder)
142
+ end
159
143
  end
160
144
  end
161
145
  end
@@ -69,7 +69,7 @@ module Trenni
69
69
  # The value of the field.
70
70
  def value_for(**options)
71
71
  if value = raw_value_for(**options)
72
- self.format(value, options)
72
+ self.format(value, **options)
73
73
  end
74
74
  end
75
75
 
@@ -99,8 +99,6 @@ module Trenni
99
99
  builder.inline :input, :type => :hidden, :name => name_for(**options), :value => 'false'
100
100
 
101
101
  builder.inline(:span) do
102
- builder.text title_for(**options)
103
-
104
102
  if details = details_for(**options)
105
103
  builder.inline(:small) {builder.text details}
106
104
  end
@@ -108,7 +106,7 @@ module Trenni
108
106
 
109
107
  builder.tag :input, checkbox_attributes_for(**options)
110
108
 
111
- # We would like a little bit of whitespace between the checkbox and the title.
109
+ # We would like a little bit of whitespace between the checkbox and the title:
112
110
  builder.text " " + title_for(**options)
113
111
  end
114
112
  end
@@ -128,7 +126,7 @@ module Trenni
128
126
  options = @options.merge(**options)
129
127
  buffer = Trenni::Template.buffer(block.binding)
130
128
 
131
- buffer << Builder.fragment do |builder|
129
+ Builder.fragment(buffer) do |builder|
132
130
  builder.inline(:label) do
133
131
  builder.inline(:span) do
134
132
  builder.text title_for(**options)
@@ -30,19 +30,15 @@ module Trenni
30
30
  def self.call(formatter, builder, **options, &block)
31
31
  instance = self.new(formatter, builder, **options)
32
32
 
33
- instance.call(options, &block)
33
+ instance.call(&block)
34
34
  end
35
35
 
36
36
  def initialize(formatter, builder, **options)
37
37
  @formatter = formatter
38
- @object = formatter.object
39
-
40
38
  @builder = builder
41
-
42
39
  @options = options
43
- @field = options[:field]
44
40
  end
45
-
41
+
46
42
  def name_for(**options)
47
43
  if name = @formatter.name_for(**options)
48
44
  if options[:multiple]
@@ -52,23 +48,23 @@ module Trenni
52
48
  return name
53
49
  end
54
50
  end
55
-
51
+
56
52
  def raw_value_for(**options)
57
53
  @formatter.raw_value_for(**options)
58
54
  end
59
-
55
+
60
56
  def raw_value
61
57
  @raw_value ||= raw_value_for(**@options)
62
58
  end
63
-
59
+
64
60
  def value_for(**options)
65
61
  @formatter.value_for(**options)
66
62
  end
67
-
63
+
68
64
  def title_for(**options)
69
65
  @formatter.title_for(**options)
70
66
  end
71
-
67
+
72
68
  def option_attributes_for(**options)
73
69
  return {
74
70
  :value => value_for(**options),
@@ -78,15 +74,21 @@ module Trenni
78
74
  :data => options[:data],
79
75
  }
80
76
  end
81
-
82
- def item(builder: nil, **options)
77
+
78
+ def item(**options, &block)
83
79
  options[:field] ||= 'id'
84
80
 
85
- Builder.fragment(builder) do |builder|
86
- builder.inline(:option, option_attributes_for(**options)) {builder.text title_for(**options)}
81
+ Builder.fragment(block&.binding || @builder) do |builder|
82
+ builder.inline(:option, option_attributes_for(**options)) do
83
+ if block_given?
84
+ builder.capture(self, &block)
85
+ else
86
+ builder.text title_for(**options)
87
+ end
88
+ end
87
89
  end
88
90
  end
89
-
91
+
90
92
  def optional_title_for(**options)
91
93
  if options[:optional] == true
92
94
  options[:blank] || ''
@@ -94,7 +96,7 @@ module Trenni
94
96
  options[:optional]
95
97
  end
96
98
  end
97
-
99
+
98
100
  def group_attributes_for(**options)
99
101
  return {
100
102
  :label => title_for(**options),
@@ -103,19 +105,17 @@ module Trenni
103
105
  :data => options[:data],
104
106
  }
105
107
  end
106
-
107
- def group(options = {}, &block)
108
- Builder.fragment(@builder) do |builder|
109
- builder.tag :optgroup, group_attributes_for(**options) do
110
- if options[:optional]
111
- item(:title => optional_title_for(**options), :value => nil, :builder => builder)
112
- end
113
-
114
- builder.capture(&block)
108
+
109
+ def group(**options, &block)
110
+ @builder.tag :optgroup, group_attributes_for(**options) do
111
+ if options[:optional]
112
+ item(title: optional_title_for(**options), value: nil)
115
113
  end
114
+
115
+ @builder.capture(&block)
116
116
  end
117
117
  end
118
-
118
+
119
119
  def select_attributes_for(**options)
120
120
  return {
121
121
  :name => name_for(**options),
@@ -126,12 +126,16 @@ module Trenni
126
126
  :required => options[:required],
127
127
  }
128
128
  end
129
-
130
- def call(options = {}, &block)
129
+
130
+ def optional?
131
+ @options[:optional]
132
+ end
133
+
134
+ def call(&block)
131
135
  Builder.fragment(@builder) do |builder|
132
- builder.tag :select, select_attributes_for(**options) do
133
- if options[:optional]
134
- item(:title => optional_title_for(**options), :value => nil, :builder => builder)
136
+ builder.tag :select, select_attributes_for(**@options) do
137
+ if self.optional?
138
+ builder << item(title: optional_title_for(**@options), value: nil)
135
139
  end
136
140
 
137
141
  builder.capture(self, &block)
@@ -30,19 +30,17 @@ module Trenni
30
30
  def self.call(formatter, builder, **options, &block)
31
31
  instance = self.new(formatter, builder, **options)
32
32
 
33
- instance.call(options, &block)
33
+ instance.call(&block)
34
34
  end
35
35
 
36
36
  def initialize(formatter, builder, **options)
37
37
  @formatter = formatter
38
- @object = formatter.object
39
-
40
38
  @builder = builder
41
-
42
39
  @options = options
40
+
43
41
  @field = options[:field]
44
42
  end
45
-
43
+
46
44
  def name_for(**options)
47
45
  @formatter.name_for(**options)
48
46
  end
@@ -50,19 +48,19 @@ module Trenni
50
48
  def raw_value_for(**options)
51
49
  @formatter.raw_value_for(**options)
52
50
  end
53
-
51
+
54
52
  def raw_value
55
53
  @raw_value ||= raw_value_for(**@options)
56
54
  end
57
-
55
+
58
56
  def value_for(**options)
59
57
  @formatter.value_for(**options)
60
58
  end
61
-
59
+
62
60
  def title_for(**options)
63
61
  @formatter.title_for(**options)
64
62
  end
65
-
63
+
66
64
  def radio_attributes_for(**options)
67
65
  return {
68
66
  :type => :radio,
@@ -73,9 +71,9 @@ module Trenni
73
71
  :data => options[:data],
74
72
  }
75
73
  end
76
-
77
- def item(builder: nil, **options, &block)
78
- Builder.fragment(builder) do |builder|
74
+
75
+ def item(**options, &block)
76
+ Builder.fragment(block&.binding || @builder) do |builder|
79
77
  builder.tag :tr do
80
78
  builder.inline(:td, :class => :handle) do
81
79
  builder.tag :input, radio_attributes_for(**options)
@@ -89,7 +87,7 @@ module Trenni
89
87
  end
90
88
  end
91
89
  end
92
- end >> block
90
+ end
93
91
  end
94
92
 
95
93
  def optional_title_for(**options)
@@ -99,13 +97,17 @@ module Trenni
99
97
  options[:optional]
100
98
  end
101
99
  end
102
-
103
- def call(options = {}, &block)
100
+
101
+ def optional?
102
+ @options[:optional]
103
+ end
104
+
105
+ def call(&block)
104
106
  Builder.fragment(@builder) do |builder|
105
107
  builder.tag :table do
106
108
  builder.tag :tbody do
107
- if options[:optional]
108
- item(:title => optional_title_for(**options), :value => nil, :builder => builder)
109
+ if self.optional?
110
+ builder << item(title: optional_title_for(**@options), value: nil)
109
111
  end
110
112
 
111
113
  builder.capture(self, &block)
@@ -27,7 +27,7 @@ module Trenni
27
27
  module Formatters
28
28
  module RelativeTime
29
29
  def self.included(base)
30
- base.map(Time) do |object, options|
30
+ base.map(Time) do |object, **options|
31
31
  current_time = options.fetch(:current_time) {Time.now}
32
32
 
33
33
  # Ensure we display the time in localtime, and show the year if it is different:
@@ -26,28 +26,30 @@ require 'mapping/model'
26
26
  module Trenni
27
27
  module Formatters
28
28
  module TruncatedText
29
- def truncated_text(content, options = {})
29
+ def truncated_text(content, length: 30, **options)
30
30
  if content
31
- length = options.fetch(:length, 30)
32
-
33
- content = TruncatedText.truncate_text(content, length, options)
31
+ content = TruncatedText.truncate_text(content, length, **options)
34
32
 
35
33
  return self.format(content)
36
34
  end
37
35
  end
38
36
 
39
- def self.truncate_text(text, truncate_at, options = {})
37
+ def self.truncate_text(text, truncate_at, omission: nil, separator: nil, **options)
40
38
  return text.dup unless text.length > truncate_at
41
39
 
42
- options[:omission] ||= '...'
43
- length_with_room_for_omission = truncate_at - options[:omission].length
44
- stop = if options[:separator]
45
- text.rindex(options[:separator], length_with_room_for_omission) || length_with_room_for_omission
46
- else
47
- length_with_room_for_omission
40
+ omission ||= '...'
41
+
42
+ length_with_room_for_omission = truncate_at - omission.length
43
+
44
+ stop = nil
45
+
46
+ if separator
47
+ stop = text.rindex(separator, length_with_room_for_omission)
48
48
  end
49
49
 
50
- "#{text[0...stop]}#{options[:omission]}"
50
+ stop ||= length_with_room_for_omission
51
+
52
+ "#{text[0...stop]}#{omission}"
51
53
  end
52
54
  end
53
55
  end
@@ -22,6 +22,6 @@
22
22
 
23
23
  module Trenni
24
24
  module Formatters
25
- VERSION = "2.11.0"
25
+ VERSION = "2.13.1"
26
26
  end
27
27
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trenni-formatters
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.11.0
4
+ version: 2.13.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-03 00:00:00.000000000 Z
11
+ date: 2020-07-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mapping
@@ -30,14 +30,14 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '3.4'
33
+ version: '3.12'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '3.4'
40
+ version: '3.12'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: bake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,34 +52,6 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
- - !ruby/object:Gem::Dependency
56
- name: bake-bundler
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
- - !ruby/object:Gem::Dependency
70
- name: bake-modernize
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
55
  - !ruby/object:Gem::Dependency
84
56
  name: bundler
85
57
  requirement: !ruby/object:Gem::Requirement
@@ -122,14 +94,18 @@ dependencies:
122
94
  - - ">="
123
95
  - !ruby/object:Gem::Version
124
96
  version: '0'
125
- description:
126
- email:
97
+ description:
98
+ email:
127
99
  executables: []
128
100
  extensions: []
129
101
  extra_rdoc_files: []
130
102
  files:
103
+ - lib/.DS_Store
104
+ - lib/trenni/.DS_Store
131
105
  - lib/trenni/formatters.rb
106
+ - lib/trenni/formatters/.DS_Store
132
107
  - lib/trenni/formatters/formatter.rb
108
+ - lib/trenni/formatters/html/accept_checkbox.rb
133
109
  - lib/trenni/formatters/html/definition_list_form.rb
134
110
  - lib/trenni/formatters/html/form_formatter.rb
135
111
  - lib/trenni/formatters/html/label_form.rb
@@ -144,7 +120,7 @@ licenses:
144
120
  - MIT
145
121
  metadata:
146
122
  funding_uri: https://github.com/sponsors/ioquatix/
147
- post_install_message:
123
+ post_install_message:
148
124
  rdoc_options: []
149
125
  require_paths:
150
126
  - lib
@@ -160,7 +136,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
160
136
  version: '0'
161
137
  requirements: []
162
138
  rubygems_version: 3.1.2
163
- signing_key:
139
+ signing_key:
164
140
  specification_version: 4
165
141
  summary: Formatters for Trenni, to assist with typical views and form based interfaces.
166
142
  test_files: []