Class: Cpr::Row

Inherits:
Object
  • Object
show all
Includes:
Kunderefnr
Defined in:
lib/cpr/row.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Kunderefnr

#birthday, #birthday_century, #pnr, #pnr_assigned_digits

Constructor Details

#initialize(data = nil) ⇒ Row

Returns a new instance of Row.



7
8
9
10
11
12
13
# File 'lib/cpr/row.rb', line 7

def initialize(data = nil)
  if data
    @data = data
  else
    @data = self.class.fill
  end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/cpr/row.rb', line 6

def data
  @data
end

Class Method Details

.create(fields_with_vals = {}) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/cpr/row.rb', line 36

def create(fields_with_vals = {})
  instance = new
  fields_with_vals.each do |key,val|
    instance[key] = val
  end
  instance
end

.field(fieldname, type, range, opts = {}) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/cpr/row.rb', line 44

def field(fieldname, type, range, opts = {})
  @fields ||= []
  @fields_map ||= {}
  check_contiguous!(fieldname, range)
  @fields << Cpr::Field.create_type(type, fieldname, range, opts)
  @fields_map[fieldname] = @fields.last
end

.field_by_name(fieldname) ⇒ Object



56
57
58
# File 'lib/cpr/row.rb', line 56

def field_by_name(fieldname)
  @fields_map[fieldname] or raise "No field by name '#{fieldname}"
end

.fieldsObject



52
53
54
# File 'lib/cpr/row.rb', line 52

def fields
  @fields
end

.fillObject



64
65
66
# File 'lib/cpr/row.rb', line 64

def fill
  @fields.map(&:fill).join
end

.inherited(subclass) ⇒ Object



79
80
81
82
# File 'lib/cpr/row.rb', line 79

def inherited(subclass)
  @subclasses ||= []
  @subclasses << subclass
end

.lengthObject



60
61
62
# File 'lib/cpr/row.rb', line 60

def length
  @fields.last.range.last + 1 unless @fields.empty?
end

.parse(str) ⇒ Object



73
74
75
76
77
# File 'lib/cpr/row.rb', line 73

def parse(str)
  load_subclasses if @subclasses.nil?
  parser = @subclasses.find {|subclass| subclass.parses? str }
  parser.new(str) if parser
end

.parses?(str) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
# File 'lib/cpr/row.rb', line 68

def parses?(str)
  f = @fields_map['Recordtype']
  f && str.start_with?(f.fill) && str.length == length
end

.title(title) ⇒ Object



32
33
34
# File 'lib/cpr/row.rb', line 32

def title(title)
  @title = title
end

Instance Method Details

#[](fieldname) ⇒ Object



15
16
17
# File 'lib/cpr/row.rb', line 15

def [](fieldname)
  self.class.field_by_name(fieldname).read(@data)
end

#[]=(fieldname, value) ⇒ Object



19
20
21
# File 'lib/cpr/row.rb', line 19

def []=(fieldname, value)
  self.class.field_by_name(fieldname).write(@data, value)
end

#contentObject



23
24
25
26
27
28
29
# File 'lib/cpr/row.rb', line 23

def content
  h = {}
  self.class.fields.each do |field|
    h[field.name] = self[field.name]
  end
  h
end