Class: Cpr::FieldSpec

Inherits:
OpenStruct
  • Object
show all
Defined in:
lib/cpr/class_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extract_default(text) ⇒ Object



21
22
23
24
25
# File 'lib/cpr/class_generator.rb', line 21

def self.extract_default(text)
  if text =~ /^(\d+)$/
    $1.to_i
  end
end

.parse_from_tr(tr) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/cpr/class_generator.rb', line 7

def self.parse_from_tr(tr)
  #      0     1      2        3             4             5
  # Feltnavn|Format|Længde|Record pos|Indhold i feltet|Krævede felter
  #
  # Record pos begins at 1
  tds = tr.xpath("TD").map {|td| td.content.strip }
  unless tds[0] == '' or tds[0] == "Recordlængde"
    FieldSpec.new(:name => tds[0],
      :type => tds[1],
      :range => (tds[3].to_i-1)..(tds[3].to_i-2 + tds[2].to_i),
      :default => extract_default(tds[4]))
  end
end

Instance Method Details

#field_defObject



27
28
29
30
31
# File 'lib/cpr/class_generator.rb', line 27

def field_def
  str = %{field '#{name}', '#{type}', #{range}}
  str += %{, :default => '#{default}'} if default
  str
end

#to_sObject



34
35
36
# File 'lib/cpr/class_generator.rb', line 34

def to_s
  "#{name} (#{type}) #{range}: #{default}"
end