Class: TermUtils::Tab::Column
- Inherits:
-
Object
- Object
- TermUtils::Tab::Column
- Defined in:
- lib/term_utils/tab.rb
Overview
Represents a table column.
Instance Attribute Summary collapse
-
#align ⇒ Symbol
`:left`, `:right`.
- #ellipsis ⇒ String
- #fixed ⇒ Boolean
- #format ⇒ Proc, ...
- #id ⇒ Symbol
- #index ⇒ Integer
- #width ⇒ Integer
Instance Method Summary collapse
-
#align_cut(str) ⇒ String
Aligns and cuts a given string.
-
#initialize(opts = {}) ⇒ Column
constructor
A new instance of Column.
-
#render_data(v) ⇒ Object
Renders a given value.
-
#render_header(v) ⇒ Object
Renders a given header.
-
#validate ⇒ nil
Validates the column represented by this one.
Constructor Details
#initialize(opts = {}) ⇒ Column
Returns a new instance of Column
173 174 175 176 177 178 179 180 181 |
# File 'lib/term_utils/tab.rb', line 173 def initialize(opts = {}) @id = opts.fetch(:id) @index = opts.fetch(:index) @width = opts.fetch(:width, 8) @align = opts.fetch(:align, :left) @fixed = opts.fetch(:fixed, false) @ellipsis = opts.fetch(:ellipsis, "?") @format = opts.fetch(:format, nil) end |
Instance Attribute Details
#align ⇒ Symbol
Returns `:left`, `:right`.
158 159 160 |
# File 'lib/term_utils/tab.rb', line 158 def align @align end |
#ellipsis ⇒ String
162 163 164 |
# File 'lib/term_utils/tab.rb', line 162 def ellipsis @ellipsis end |
#fixed ⇒ Boolean
160 161 162 |
# File 'lib/term_utils/tab.rb', line 160 def fixed @fixed end |
#format ⇒ Proc, ...
164 165 166 |
# File 'lib/term_utils/tab.rb', line 164 def format @format end |
#id ⇒ Symbol
152 153 154 |
# File 'lib/term_utils/tab.rb', line 152 def id @id end |
#index ⇒ Integer
154 155 156 |
# File 'lib/term_utils/tab.rb', line 154 def index @index end |
#width ⇒ Integer
156 157 158 |
# File 'lib/term_utils/tab.rb', line 156 def width @width end |
Instance Method Details
#align_cut(str) ⇒ String
Aligns and cuts a given string.
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
# File 'lib/term_utils/tab.rb', line 197 def align_cut(str) if @align == :left # Align left if @fixed and (str.length > @width) str = "#{str[0..(@width - (@ellipsis.length + 1))]}#{@ellipsis}" else str = "%-*s" % [@width, str] end else # Align right if @fixed and (str.length > @width) str = "#{@ellipsis}#{str[(str.length - @width + @ellipsis.length)..(str.length - 1)]}" else str = "%*s" % [@width, str] end end str end |
#render_data(v) ⇒ Object
Renders a given value. return [String]
226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/term_utils/tab.rb', line 226 def render_data(v) str = v if v if @format.is_a? Proc str = @format.call(v) elsif @format.is_a? String str = @format % v end end str = str.to_s unless str.is_a? String align_cut str end |
#render_header(v) ⇒ Object
Renders a given header. return [String]
218 219 220 221 222 |
# File 'lib/term_utils/tab.rb', line 218 def render_header(v) str = v str = str.to_s unless str.is_a? String align_cut str end |
#validate ⇒ nil
Validates the column represented by this one.
184 185 186 187 188 189 190 191 192 193 |
# File 'lib/term_utils/tab.rb', line 184 def validate raise "missing column id (nil)" if @id.nil? raise "missing column index (nil)" if @index.nil? raise "wrong column index (not integer)" unless @index.is_a? Integer raise "wrong column index (not >= 0)" if @index < 0 raise "missing column width (nil)" if @width.nil? raise "wrong column width (not integer)" unless @width.is_a? Integer raise "wrong column width (not > 0)" if @width <= 0 raise "wrong column align (not :left or :right)" unless %i{left right}.index @align end |