Class: FixedWidthColumns::FixedWidthAttribute

Inherits:
Object
  • Object
show all
Includes:
Aduki::Initializer
Defined in:
lib/fixed_width_columns/fixed_width_attribute.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#alignObject



9
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 9

def align   ; (@align || :right).to_sym ; end

#date_formatObject

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

#lengthObject

Returns the value of attribute length.



7
8
9
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 7

def length
  @length
end

#nameObject

Returns the value of attribute name.



7
8
9
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 7

def name
  @name
end

#paddingObject



10
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 10

def padding ; @padding || ' '           ; end

#textObject

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