Class: SheetFiller::Form

Inherits:
Struct
  • Object
show all
Defined in:
lib/sheet_filler/form.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#parserObject

Returns the value of attribute parser

Returns:

  • (Object)

    the current value of parser



5
6
7
# File 'lib/sheet_filler/form.rb', line 5

def parser
  @parser
end

#sheet_dataObject

Returns the value of attribute sheet_data

Returns:

  • (Object)

    the current value of sheet_data



5
6
7
# File 'lib/sheet_filler/form.rb', line 5

def sheet_data
  @sheet_data
end

Instance Method Details

#fill_form(raw_data) ⇒ Object



18
19
20
21
# File 'lib/sheet_filler/form.rb', line 18

def fill_form(raw_data)
  form_data = process(raw_data)
  parser.fill(form_data)
end

#populate_data(form_data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sheet_filler/form.rb', line 23

def populate_data(form_data)
  @data = form_data
  fields = parser.parse_fields(sheet_data.field_names)
  @blocks = fields.map do |name|
    if field = sheet_data.by_name(name)
      field_code = field["code"]
      @data[field_code] = form_data[field_code]
      [field_code, field["parser_block"]]
    end
  end
  process_blocks
end

#process(raw_data) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/sheet_filler/form.rb', line 6

def process(raw_data)
  populate_data(raw_data)
  form_data = @data.map do |field_code, value|
    if field = sheet_data.by_code(field_code)
      [field["name"], value]
    end
  end
  form_data = Hash[form_data.compact]
  raw_data.each { |key, value| form_data["-#{key}-"] = value }
  form_data
end