class FixedWidthColumns::FixedWidthAttribute

Attributes

align[W]
date_format[RW]
length[RW]
name[RW]
padding[W]
text[RW]

Public Instance Methods

align() click to toggle source
# File lib/fixed_width_columns/fixed_width_attribute.rb, line 9
def align   ; (@align || :right).to_sym ; end
format(value, override={ }) click to toggle source
# 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) click to toggle source
# 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
get_attribute_value(obj, segment) click to toggle source
# File lib/fixed_width_columns/fixed_width_attribute.rb, line 34
def get_attribute_value obj, segment
  segment = segment.gsub(/-/, '_').to_sym
  if obj.is_a? Hash
    obj[segment]
  else
    obj.send segment
  end
end
lookup(obj, str) click to toggle source
# File lib/fixed_width_columns/fixed_width_attribute.rb, line 43
def lookup obj, str
  segments = (str.to_s || "").split "."
  segment = segments.shift
  while !((segment == nil) || (segment.strip == '') ||(obj == nil))
    begin
      obj = get_attribute_value obj, segment
    rescue Exception => e
      raise "looking up #{str.inspect} ;\n    can't lookup #{segment}\n    on #{obj.inspect}, \n    got #{e.message.inspect}"
    end
    segment = segments.shift
  end
  obj
end
padding() click to toggle source
# File lib/fixed_width_columns/fixed_width_attribute.rb, line 10
def padding ; @padding || ' '           ; end
string_for(thing) click to toggle source
# File lib/fixed_width_columns/fixed_width_attribute.rb, line 57
def string_for thing
  format(self.text || lookup(thing, name))
end