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.
158 159 160 161 162 163 164 165 166 |
# File 'lib/term_utils/tab.rb', line 158 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`.
143 144 145 |
# File 'lib/term_utils/tab.rb', line 143 def align @align end |
#ellipsis ⇒ String
147 148 149 |
# File 'lib/term_utils/tab.rb', line 147 def ellipsis @ellipsis end |
#fixed ⇒ Boolean
145 146 147 |
# File 'lib/term_utils/tab.rb', line 145 def fixed @fixed end |
#format ⇒ Proc, ...
149 150 151 |
# File 'lib/term_utils/tab.rb', line 149 def format @format end |
#id ⇒ Symbol
137 138 139 |
# File 'lib/term_utils/tab.rb', line 137 def id @id end |
#index ⇒ Integer
139 140 141 |
# File 'lib/term_utils/tab.rb', line 139 def index @index end |
#width ⇒ Integer
141 142 143 |
# File 'lib/term_utils/tab.rb', line 141 def width @width end |
Instance Method Details
#align_cut(str) ⇒ String
Aligns and cuts a given string.
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/term_utils/tab.rb', line 182 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]
211 212 213 214 215 216 217 218 219 220 221 222 |
# File 'lib/term_utils/tab.rb', line 211 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]
203 204 205 206 207 |
# File 'lib/term_utils/tab.rb', line 203 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.
169 170 171 172 173 174 175 176 177 178 |
# File 'lib/term_utils/tab.rb', line 169 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 |