Class: Swak::Table::SmartRow

Inherits:
Object
  • Object
show all
Defined in:
lib/swak/table.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(header, fields) ⇒ SmartRow

Returns a new instance of SmartRow.



6
7
8
9
10
11
12
13
14
15
16
17
18
# File 'lib/swak/table.rb', line 6

def initialize(header, fields)
  if header.size != fields.size
    raise "Header and field size mismatch in SmartRow.new #{header.inspect} vs #{fields.inspect}"
  end

  @header = header
  @fields = fields
  @colmap = {}
  header.each_with_index {|fieldname, i| @colmap[fieldname] = i}
  if @colmap.size != @header.size
    raise "Duplicate field names not allowed in SmartRow header"
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



36
37
38
# File 'lib/swak/table.rb', line 36

def method_missing(sym, *args, &block)
  @fields.send(sym, *args, &block)
end

Instance Attribute Details

#colmapObject (readonly)

Returns the value of attribute colmap.



4
5
6
# File 'lib/swak/table.rb', line 4

def colmap
  @colmap
end

#fieldsObject (readonly)

Returns the value of attribute fields.



4
5
6
# File 'lib/swak/table.rb', line 4

def fields
  @fields
end

#headerObject (readonly)

Returns the value of attribute header.



4
5
6
# File 'lib/swak/table.rb', line 4

def header
  @header
end

Instance Method Details

#[](*args) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/swak/table.rb', line 20

def [](*args)
  if args.size == 1 && args[0].is_a?(String)
    @fields[@colmap[args[0]]]
  else
    @fields[*args]
  end
end

#[]=(*args) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/swak/table.rb', line 28

def []=(*args)
  if args.size == 2 && args[0].is_a?(String)
    @fields[@colmap[args[0]]] = args[-1]
  else
    @fields.[]=(*args)
  end
end