Sha256: 83a66d566971390b5c29db857f5d396f1deac4fc3c04cb4e0a21066e7f49d0b1

Contents?: true

Size: 1.88 KB

Versions: 2

Compression:

Stored size: 1.88 KB

Contents

# frozen_string_literal: true

require 'json'

module Thinreports
  module Layout
    class Format < Core::Shape::Manager::Format
      config_reader last_version: %w[version]
      config_reader report_title: %w[title]
      config_reader page_paper_type: %w[report paper-type],
                    page_width: %w[report width],
                    page_height: %w[report height],
                    page_orientation: %w[report orientation]

      class << self
        # rubocop:disable Metrics/AbcSize
        def build(filename)
          schema = JSON.parse(read_file(filename))
          schema_version = Layout::Version.new(schema['version'])

          unless schema_version.compatible?
            raise Errors::IncompatibleLayoutFormat.new(
              filename, schema['version'],
              Layout::Version.compatible_rules.join(' and ')
            )
          end

          if schema_version.legacy?
            warn '[DEPRECATION] Support for the layout file with old format' \
                 ' that generated with Editor 0.8 or lower will be dropped in Thinreports 1.1.' \
                 ' Please convert to new layout format using Thinreports Editor 0.9 or 1.0.'
            schema = Layout::LegacySchema.new(schema).upgrade
          end

          new schema
        end

        def read_file(filename)
          File.read(filename, encoding: 'UTF-8')
        end
      end

      def initialize(*)
        super
        initialize_items(attributes['items'])
      end

      def user_paper_type?
        page_paper_type == 'user'
      end

      private

      def initialize_items(item_schemas)
        item_schemas.each do |item_schema|
          id, type = item_schema.values_at 'id', 'type'

          next if id.empty? && type != 'page-number'

          item = Core::Shape::Format(type).new(item_schema)
          shapes[item.id.to_sym] = item
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thinreports-0.10.1 lib/thinreports/layout/format.rb
thinreports-0.10.0 lib/thinreports/layout/format.rb