trenni-formatters 2.12.1 → 2.12.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 518b46786862caa9ff2dced015ea7f14299c274f42bbe7556f9d85ef4fe5a21c
4
- data.tar.gz: af3c874086ff1d37d39f2294a2037d4cbc63fafc4e3a783f6d2481d579404c14
3
+ metadata.gz: 7fc81bd7424d62a51946525bfadb198b184223b85f39924b090c4917866e4e07
4
+ data.tar.gz: 2b68b00618ebff45b414dd7f4cd29eb770cdc4093ce0e80738a2b20dcba60377
5
5
  SHA512:
6
- metadata.gz: b044fd11db20ab856fe9608dea7039020c1ec42850260deb3b2ca27c6c56884e7a98aeb52ef53347dde510dc0070a6ab38ac2453e7b9d1fb4cc91f36e3d853f4
7
- data.tar.gz: 2e59bdcdfec2617347c0dd4ccbdc786057ea3cd2d1618f1f00c51c8cf9506a6f075489a0442163025bb2bb9c08468bee285c44e5a22e91a906a8b3aeb545c68d
6
+ metadata.gz: bb61c211734823ed5d5c198532d892bd881309c90799cdd939d025e0edc59582fcc11b92292c8cbfeaf748fc60ea81c883967aaf58c0f46bcd9067ba5c362bbd
7
+ data.tar.gz: 2fc540df7f46b5dd4209517143fbe50d603cfd9857000c86906bc50c2c0302d8ae467cdbc8758e00f6360ba3e6dd99ca79bb9b4a232af11e9ab8f9cacc18e6dd
@@ -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
@@ -29,37 +29,29 @@ module Trenni
29
29
  def self.call(formatter, builder, **options, &block)
30
30
  instance = self.new(formatter, builder, **options)
31
31
 
32
- instance.call(options, &block)
32
+ instance.call(&block)
33
33
  end
34
34
 
35
35
  def initialize(formatter, builder, **options)
36
36
  @formatter = formatter
37
- @object = formatter.object
38
-
39
37
  @builder = builder
40
-
41
38
  @options = options
42
- @field = options[:field]
43
39
  end
44
40
 
45
41
  def name_for(**options)
46
42
  @formatter.name_for(**options)
47
43
  end
48
44
 
49
- def title_for(**options)
50
- @formatter.title_for(**options)
51
- end
52
-
53
45
  def checkbox_attributes_for(**options)
54
46
  @formatter.checkbox_attributes_for(**options)
55
47
  end
56
48
 
57
- def call(**options, &block)
49
+ def call(&block)
58
50
  Builder.fragment(@builder) do |builder|
59
51
  builder.inline('span') do
60
- builder.inline :input, :type => :hidden, :name => name_for(**options), :value => 'false'
52
+ builder.inline :input, type: :hidden, name: name_for(**@options), value: 'false'
61
53
 
62
- builder.tag :input, checkbox_attributes_for(**options)
54
+ builder.tag :input, checkbox_attributes_for(**@options)
63
55
 
64
56
  builder.text " "
65
57
 
@@ -117,23 +117,6 @@ 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
122
  buffer = Trenni::Template.buffer(block.binding)
@@ -155,7 +138,9 @@ module Trenni
155
138
 
156
139
  def fieldset(**options, &block)
157
140
  super do |builder|
158
- builder.tag(:dl, &block)
141
+ builder.tag(:dl) do
142
+ yield(builder)
143
+ end
159
144
  end
160
145
  end
161
146
  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
 
@@ -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,15 @@ module Trenni
78
74
  :data => options[:data],
79
75
  }
80
76
  end
81
-
82
- def item(builder: nil, **options)
77
+
78
+ def item(builder = nil, **options)
83
79
  options[:field] ||= 'id'
84
80
 
85
81
  Builder.fragment(builder) do |builder|
86
82
  builder.inline(:option, option_attributes_for(**options)) {builder.text title_for(**options)}
87
83
  end
88
84
  end
89
-
85
+
90
86
  def optional_title_for(**options)
91
87
  if options[:optional] == true
92
88
  options[:blank] || ''
@@ -94,7 +90,7 @@ module Trenni
94
90
  options[:optional]
95
91
  end
96
92
  end
97
-
93
+
98
94
  def group_attributes_for(**options)
99
95
  return {
100
96
  :label => title_for(**options),
@@ -103,19 +99,19 @@ module Trenni
103
99
  :data => options[:data],
104
100
  }
105
101
  end
106
-
107
- def group(options = {}, &block)
102
+
103
+ def group(**options, &block)
108
104
  Builder.fragment(@builder) do |builder|
109
105
  builder.tag :optgroup, group_attributes_for(**options) do
110
106
  if options[:optional]
111
- item(:title => optional_title_for(**options), :value => nil, :builder => builder)
107
+ item(@builder, title: optional_title_for(**options), value: nil)
112
108
  end
113
109
 
114
110
  builder.capture(&block)
115
111
  end
116
112
  end
117
113
  end
118
-
114
+
119
115
  def select_attributes_for(**options)
120
116
  return {
121
117
  :name => name_for(**options),
@@ -126,12 +122,16 @@ module Trenni
126
122
  :required => options[:required],
127
123
  }
128
124
  end
129
-
130
- def call(**options, &block)
125
+
126
+ def optional?
127
+ @options[:optional]
128
+ end
129
+
130
+ def call(&block)
131
131
  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)
132
+ builder.tag :select, select_attributes_for(**@options) do
133
+ if self.optional?
134
+ item(builder, title: optional_title_for(**@options), value: nil)
135
135
  end
136
136
 
137
137
  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,8 +71,8 @@ module Trenni
73
71
  :data => options[:data],
74
72
  }
75
73
  end
76
-
77
- def item(builder: nil, **options, &block)
74
+
75
+ def item(builder = nil, **options, &block)
78
76
  Builder.fragment(builder) do |builder|
79
77
  builder.tag :tr do
80
78
  builder.inline(:td, :class => :handle) do
@@ -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
+ item(builder, 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.12.1"
25
+ VERSION = "2.12.2"
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.12.1
4
+ version: 2.12.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Samuel Williams
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-06-05 00:00:00.000000000 Z
11
+ date: 2020-06-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mapping