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

#get_attribute_value(obj, segment) ⇒ Object



34
35
36
37
38
39
40
41
# 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) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# 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

#string_for(thing) ⇒ Object



57
58
59
# File 'lib/fixed_width_columns/fixed_width_attribute.rb', line 57

def string_for thing
  format(self.text || lookup(thing, name))
end