html_format 0.0.3 → 0.0.4

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
  SHA1:
3
- metadata.gz: b46a4cdb45df13a48e56927254c498f13d0ba0c6
4
- data.tar.gz: 72c9babeb2d5701317f6727dd759ad373def2fbf
3
+ metadata.gz: 15a5d06beaa01fe23e72b23952879b4f7bf3ec3e
4
+ data.tar.gz: a9e44f86a7b748196a541462ff2a15e82ae80725
5
5
  SHA512:
6
- metadata.gz: a69f00217d5a952eb7e5c06a0d94f8acb54229ee2ae87755596f56f718c72443179dc7e39e0b6fbd0e5d9f1b1b3e2ffe0a2f8855049f0ff1caaebf92283a4483
7
- data.tar.gz: a79e25e14e0c07810b1df301fdedae787d239caab030ac402d830c1105c59950eab6c034efe707aa03863c1f521910f0bf6c3e91302de52abd9153ea9eb0f740
6
+ metadata.gz: 8d127c1a85828f90491cc1bebbc7343837bc15ce30ac08f6442cf2bfccec535782d60d352c5b484067b38393e3c1f25615d77b4b7abd7574e1954c1a6273224e
7
+ data.tar.gz: 4de2bef2f5ecd4f9f8c49325985c19e8e17b64ea9736da8ee18adc1b3095a631644bffc1ac1592cf8a0b9559c7c06e37e2dd91b45d4a9d1dfc65ea1a040c9e9f
@@ -5,7 +5,7 @@ require 'action_view'
5
5
  module HtmlFormat
6
6
  mattr_accessor :default_options
7
7
  self.default_options = {
8
- table_class: 'table table-striped table-bordered table-hover',
8
+ table_class: ['table', 'table-striped', 'table-bordered', 'table-hover'],
9
9
  nesting: false,
10
10
  title_tag: :h2,
11
11
  header_patch: false, # If there is no header, add it
@@ -54,58 +54,63 @@ module HtmlFormat
54
54
 
55
55
  info = function_table.find { |e| e[:_case].call(obj) }
56
56
  body = ''.html_safe
57
- if true
58
- if @options[:caption].present?
59
- body << content_tag(:caption, @options[:caption])
60
- end
57
+
58
+ if v = @options[:caption].presence
59
+ body << v
61
60
  end
62
61
  if @options[:header_patch]
63
- if info[:header_patch]
64
- if v = info[:header_patch].call(obj)
62
+ if info[:header_process]
63
+ if v = info[:header_process].call(obj)
65
64
  body << v
66
65
  end
67
66
  end
68
67
  end
69
68
  body << info[:process].call(obj)
70
- body = content_tag(:table, body, :class => table_class(info))
69
+ body = tag.table(body, :class => table_class(info))
71
70
  if @options[:depth].zero?
72
71
  if @options[:responsive]
73
- body = content_tag(:div, body, :class => 'table-responsive')
72
+ body = tag.div(body, :class => 'table-responsive')
74
73
  end
75
74
  if @options[:title].present?
76
- body = content_tag(@options[:title_tag], @options[:title], :class => 'title') + body
75
+ body = tag.(@options[:title_tag], @options[:title], :class => 'title') + body
77
76
  end
78
77
  end
79
- content_tag(:div, body, :class => "html_format html_format_depth_#{@options[:depth]}")
78
+ tag.div(body, :class => ['html_format', "html_format_depth_#{@options[:depth]}"])
80
79
  end
81
80
 
82
81
  private
83
82
 
84
83
  def function_table
85
84
  [
85
+ # Hash
86
+ #
86
87
  # {a: 1, b: 2}
88
+ #
87
89
  # [a][1]
88
90
  # [b][2]
89
91
  {
90
92
  _case: -> e { e.kind_of?(Hash) },
91
93
  css_class: 'html_format_type_hash',
92
- header_patch: -> e {
93
- content_tag(:thead) do
94
- tr do
95
- th(@options[:key_label]) + th(@options[:value_label])
94
+ header_process: -> e {
95
+ tag.thead do
96
+ tag.tr do
97
+ tag.th(@options[:key_label]) + tag.th(@options[:value_label])
96
98
  end
97
99
  end
98
100
  },
99
101
  process: -> e {
100
102
  e.collect {|key, val|
101
- tr do
102
- th(key) + td(val)
103
+ tag.tr do
104
+ tag.th(key) + td(val)
103
105
  end
104
106
  }.join.html_safe
105
107
  },
106
108
  },
107
109
 
110
+ # Array of Hash
111
+ #
108
112
  # [{a: 1, b: 2}, {a: 3, b: 4}]
113
+ #
109
114
  # [a][b]
110
115
  # [1][2]
111
116
  # [3][4]
@@ -115,14 +120,14 @@ module HtmlFormat
115
120
  process: -> e {
116
121
  keys = e.inject([]) { |a, e| a | e.keys }
117
122
  body = ''.html_safe
118
- body += content_tag(:thead) do
119
- tr do
120
- keys.collect {|e| th(e) }.join.html_safe
123
+ body += tag.thead do
124
+ tag.tr do
125
+ keys.collect {|e| tag.th(e) }.join.html_safe
121
126
  end
122
127
  end
123
- body + content_tag(:tbody) do
128
+ body + tag.tbody do
124
129
  e.collect { |hash|
125
- tr do
130
+ tag.tr do
126
131
  keys.collect { |key| td(hash[key]) }.join.html_safe
127
132
  end
128
133
  }.join.html_safe
@@ -130,24 +135,28 @@ module HtmlFormat
130
135
  },
131
136
  },
132
137
 
138
+ # Array of Array
139
+ #
133
140
  # [[:a, :b], [ 1, 2], [ 3, 4]]
141
+ #
134
142
  # [a][b]
135
143
  # [1][2]
136
144
  # [3][4]
137
145
  {
138
146
  _case: -> e { e.kind_of?(Array) && e.all?{|e|e.kind_of?(Array)} },
139
147
  css_class: 'html_format_type_array_of_array',
140
- header_patch: -> e {
148
+ header_process: -> e {
141
149
  if e.first.kind_of?(Array)
142
- content_tag(:thead) do
143
- e.first.collect { td('') }.join.html_safe # カラムの意味はわからないので空ラベルとする
150
+ tag.thead do
151
+ # I do not know the meaning of the column so make it empty label
152
+ e.first.collect { td('') }.join.html_safe
144
153
  end
145
154
  end
146
155
  },
147
156
  process: -> e {
148
- content_tag(:tbody) do
157
+ tag.tbody do
149
158
  e.collect { |elems|
150
- tr do
159
+ tag.tr do
151
160
  elems.collect { |e| td(e) }.join.html_safe
152
161
  end
153
162
  }.join.html_safe
@@ -155,44 +164,42 @@ module HtmlFormat
155
164
  },
156
165
  },
157
166
 
167
+ # Array
168
+ #
158
169
  # [:a, :b]
170
+ #
159
171
  # [a][b]
160
172
  {
161
173
  _case: -> e { e.kind_of?(Array) },
162
174
  css_class: 'html_format_type_array',
163
175
  process: -> e {
164
- content_tag(:tbody) do
165
- tr do
176
+ tag.tbody do
177
+ tag.tr do
166
178
  e.collect { |e| td(e) }.join.html_safe
167
179
  end
168
180
  end
169
181
  },
170
182
  },
171
183
 
184
+ # Ohter
185
+ #
172
186
  # :a
187
+ #
173
188
  # [a]
174
189
  {
175
190
  _case: -> e { true },
176
191
  css_class: 'html_format_type_object',
177
192
  process: -> e {
178
- content_tag(:tbody) do
179
- tr { td(e) }
193
+ tag.tbody do
194
+ tag.tr { td(e) }
180
195
  end
181
196
  },
182
197
  },
183
198
  ]
184
199
  end
185
200
 
186
- def tr(&block)
187
- content_tag(:tr, &block)
188
- end
189
-
190
- def th(val)
191
- content_tag(:th, val)
192
- end
193
-
194
201
  def td(val)
195
- content_tag(:td, value_as_string(val))
202
+ tag.td(value_as_string(val))
196
203
  end
197
204
 
198
205
  def value_as_string(val)
@@ -209,11 +216,10 @@ module HtmlFormat
209
216
 
210
217
  def table_class(info)
211
218
  if @options[:depth] == 0
212
- # return 'table table-condensed table-bordered table-striped'
213
- "table #{info[:css_class]} #{@options[:table_class]}".squish.scan(/\S+/).uniq.join(' ')
219
+ ['table', info[:css_class], @options[:table_class]].join(' ').squish.scan(/\S+/).uniq.join(' ')
214
220
  else
215
- # 入れ子になったテーブルは小さめにして装飾を避ける
216
- 'table table-condensed'
221
+ # Keep nested tables small and avoid decorations
222
+ ['table', 'table-condensed']
217
223
  end
218
224
  end
219
225
  end
@@ -1,3 +1,3 @@
1
1
  module HtmlFormat
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
Binary file