Class: Headache::Record::Base

Inherits:
Fixy::Record
  • Object
show all
Defined in:
lib/headache/record/base.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.fieldsObject



14
15
16
# File 'lib/headache/record/base.rb', line 14

def self.fields
  @record_fields.to_a.map { |i| i.last }.inject({}) { |m,i| m[i[:name]] = i.except(:name); m }
end

.normalize_field_value(field, value) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/headache/record/base.rb', line 18

def self.normalize_field_value(field, value)
  field_type = fields[field].try(:[],:type)
  return value if field_type.nil?

  case field_type
  when :numeric_value
    value.gsub(/^0+/,'').to_i
  when :alphanumeric
    value.strip
  when :nothing
    nil
  else
    value
  end
end

.parse_fields(record_string) ⇒ Object



8
9
10
11
12
# File 'lib/headache/record/base.rb', line 8

def self.parse_fields(record_string)
  parse(record_string)[:fields].inject({}) do |m,i|
    m[i[:name]] = i[:value]; m
  end
end

.parse_fields_normalize(record_string) ⇒ Object



4
5
6
# File 'lib/headache/record/base.rb', line 4

def self.parse_fields_normalize(record_string)
  parse_fields(record_string).inject({}) { |m,i| m[i.first] = normalize_field_value(i.first, i.last); m }
end

Instance Method Details

#normalize_field_value(f, v) ⇒ Object



34
35
36
# File 'lib/headache/record/base.rb', line 34

def normalize_field_value(f, v)
  self.class.normalize_field_value(f, v)
end

#parse(record_string) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/headache/record/base.rb', line 47

def parse(record_string)
  parse_fields(record_string).each_pair do |field, value|
    if respond_to?("#{field}=")
      send "#{field}=", normalize_field_value(field, value)
    end
  end
  self
end

#parse_fields(record_string) ⇒ Object



43
44
45
# File 'lib/headache/record/base.rb', line 43

def parse_fields(record_string)
  self.class.parse_fields record_string
end

#to_hObject



38
39
40
41
# File 'lib/headache/record/base.rb', line 38

def to_h
  str = self.generate.gsub Headache::DocumentParser::LINE_SEPARATOR, ''
  self.class.parse_fields(str)
end