|
|
@ -1,31 +1,30 @@ |
|
|
|
class Form |
|
|
|
class Form |
|
|
|
|
|
|
|
attr_reader :form_definition |
|
|
|
attr_reader :form_definition, :all_sections, :all_subsections, :all_pages |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def initialize(start_year, end_year) |
|
|
|
def initialize(start_year, end_year) |
|
|
|
form_json = "config/forms/#{start_year}_#{end_year}.json" |
|
|
|
form_json = "config/forms/#{start_year}_#{end_year}.json" |
|
|
|
raise "No form definition file exists for given year".freeze unless File.exist?(form_json) |
|
|
|
raise "No form definition file exists for given year".freeze unless File.exist?(form_json) |
|
|
|
|
|
|
|
|
|
|
|
@form_definition = JSON.load(File.new(form_json)) |
|
|
|
@form_definition = JSON.parse(File.open(form_json).read) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# Returns a hash with sections as keys |
|
|
|
# Returns a hash with sections as keys |
|
|
|
def all_sections |
|
|
|
def all_sections |
|
|
|
@sections ||= @form_definition["sections"] |
|
|
|
@all_sections ||= @form_definition["sections"] |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# Returns a hash with subsections as keys |
|
|
|
# Returns a hash with subsections as keys |
|
|
|
def all_subsections |
|
|
|
def all_subsections |
|
|
|
@subsections ||= all_sections.map do |section_key, section_value| |
|
|
|
@all_subsections ||= all_sections.map { |_section_key, section_value| |
|
|
|
section_value["subsections"] |
|
|
|
section_value["subsections"] |
|
|
|
end.reduce(:merge) |
|
|
|
}.reduce(:merge) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# Returns a hash with pages as keys |
|
|
|
# Returns a hash with pages as keys |
|
|
|
def all_pages |
|
|
|
def all_pages |
|
|
|
@all_pages ||= all_subsections.map do |subsection_key, subsection_value| |
|
|
|
@all_pages ||= all_subsections.map { |_subsection_key, subsection_value| |
|
|
|
subsection_value["pages"] |
|
|
|
subsection_value["pages"] |
|
|
|
end.reduce(:merge) |
|
|
|
}.reduce(:merge) |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
# Returns a hash with the pages as keys |
|
|
|
# Returns a hash with the pages as keys |
|
|
@ -38,9 +37,9 @@ class Form |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def subsection_for_page(page) |
|
|
|
def subsection_for_page(page) |
|
|
|
all_subsections.find do |subsection_key, subsection_value| |
|
|
|
all_subsections.find { |_subsection_key, subsection_value| |
|
|
|
subsection_value["pages"].keys.include?(page) |
|
|
|
subsection_value["pages"].key?(page) |
|
|
|
end.first |
|
|
|
}.first |
|
|
|
end |
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def next_page(previous_page) |
|
|
|
def next_page(previous_page) |
|
|
@ -52,7 +51,7 @@ class Form |
|
|
|
def previous_page(current_page) |
|
|
|
def previous_page(current_page) |
|
|
|
subsection = subsection_for_page(current_page) |
|
|
|
subsection = subsection_for_page(current_page) |
|
|
|
current_page_idx = pages_for_subsection(subsection).keys.index(current_page) |
|
|
|
current_page_idx = pages_for_subsection(subsection).keys.index(current_page) |
|
|
|
return unless current_page_idx > 0 |
|
|
|
return unless current_page_idx.positive? |
|
|
|
|
|
|
|
|
|
|
|
pages_for_subsection(subsection).keys[current_page_idx - 1] |
|
|
|
pages_for_subsection(subsection).keys[current_page_idx - 1] |
|
|
|
end |
|
|
|
end |
|
|
|