Class: FixedWidthColumns::FixedWidthAttribute
- Inherits:
-
Object
- Object
- FixedWidthColumns::FixedWidthAttribute
- Includes:
- Aduki::Initializer
- Defined in:
- lib/fixed_width_columns/fixed_width_attribute.rb
Instance Attribute Summary collapse
- #align ⇒ Object
-
#date_format ⇒ Object
Returns the value of attribute date_format.
-
#length ⇒ Object
Returns the value of attribute length.
-
#name ⇒ Object
Returns the value of attribute name.
- #padding ⇒ Object
-
#text ⇒ Object
Returns the value of attribute text.
Instance Method Summary collapse
Instance Attribute Details
#align ⇒ Object
9 |
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 9 def align ; (@align || :right).to_sym ; end |
#date_format ⇒ Object
Returns the value of attribute date_format.
7 8 9 |
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 7 def date_format @date_format end |
#length ⇒ Object
Returns the value of attribute length.
7 8 9 |
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 7 def length @length end |
#name ⇒ Object
Returns the value of attribute name.
7 8 9 |
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 7 def name @name end |
#padding ⇒ Object
10 |
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 10 def padding ; @padding || ' ' ; end |
#text ⇒ Object
Returns the value of attribute text.
7 8 9 |
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 7 def text @text end |
Instance Method Details
#format(value, override = { }) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 18 def format value, override={ } my_length = override[:length] || self.length || value.length my_align = override[:align] || self.align my_padding = override[:padding] || self.padding str = format_date value str = str.to_s[0...my_length] case my_align when :left str.ljust my_length, my_padding when :right str.rjust my_length, my_padding end end |
#format_date(value) ⇒ Object
12 13 14 15 16 |
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 12 def format_date value return value unless value.is_a? Date return value.to_s unless date_format value.strftime date_format end |
#string_for(thing) ⇒ Object
34 35 36 |
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 34 def string_for thing format(self.text || thing.send(name)) end |