Sha256: a983a8478848f50d0d0afdd5cd13ff3417e10539f433a6cbcc6b2f4b409a2129

Contents?: true

Size: 1.57 KB

Versions: 7

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require 'json'

require_relative 'report'
require_relative 'section'

module Thinreports
  module SectionReport
    module Schema
      class Parser
        def parse(schema_json_data)
          schema_data = JSON.parse(schema_json_data)

          section_schema_datas = schema_data['sections'].group_by { |section| section['type'] }

          Schema::Report.new(
            schema_data,
            headers: parse_sections(:header, section_schema_datas['header']),
            details: parse_sections(:detail, section_schema_datas['detail']),
            footers: parse_sections(:footer, section_schema_datas['footer'])
          )
        end

        private

        attr_reader :schema_data

        def parse_sections(section_type, section_schema_datas = nil)
          return {} if section_schema_datas.nil?

          section_schema_datas.each_with_object({}) do |section_schema_data, section_schemas|
            id = section_schema_data['id']
            section_schemas[id.to_sym] = parse_section(section_type, section_schema_data)
          end
        end

        def parse_section(type, section_schema_data)
          items = section_schema_data['items'].map do |item_schema_data|
            item_type = item_schema_data['type']
            Core::Shape::Format(item_type).new(item_schema_data)
          end
          section_schema_class_for(type).new(section_schema_data, items: items)
        end

        def section_schema_class_for(section_type)
          Schema::Section.const_get(section_type.capitalize)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
thinreports-0.14.2 lib/thinreports/section_report/schema/parser.rb
thinreports-0.14.1 lib/thinreports/section_report/schema/parser.rb
thinreports-0.14.0 lib/thinreports/section_report/schema/parser.rb
thinreports-0.13.1 lib/thinreports/section_report/schema/parser.rb
thinreports-0.13.0 lib/thinreports/section_report/schema/parser.rb
thinreports-0.12.1 lib/thinreports/section_report/schema/parser.rb
thinreports-0.12.0 lib/thinreports/section_report/schema/parser.rb