lib/thinreports/layout/format.rb in thinreports-0.8.2 vs lib/thinreports/layout/format.rb in thinreports-0.9.0

- old
+ new

@@ -1,80 +1,61 @@ # coding: utf-8 +require 'json' -require 'digest/sha1' - module Thinreports module Layout - class Format < Core::Shape::Manager::Format config_reader last_version: %w( version ) - config_reader report_title: %w( config title ) - config_reader page_paper_type: %w( config page paper-type ), - page_width: %w( config page width ), - page_height: %w( config page height ), - page_orientation: %w( config page orientation ) + 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 ) - config_checker 'user', user_paper_type: %w( config page paper-type ) - class << self + def build(filename) + schema = JSON.parse(read_file(filename)) - private + unless Layout::Version.compatible?(schema['version']) + raise Errors::IncompatibleLayoutFormat.new( + filename, schema['version'], Thinreports::Layout::Version.inspect_required_rules + ) + end - # @param [String] filename - # @param [Hash] options - # @option options [Boolean] :force (false) - def build_internal(filename, options = {}) - build_once(filename, options[:force]) do |content, id| - raw_format = parse_json(content) - - # Check the compatibility of specified layout file. - unless Thinreports::Layout::Version.compatible?(raw_format['version']) - info = [filename, raw_format['version'], - Thinreports::Layout::Version.inspect_required_rules] - raise Thinreports::Errors::IncompatibleLayoutFormat.new(*info) - end - - compact_format!(raw_format) - - # Build and initialize format. - new(raw_format, id) do |f| - build_layout(f) do |type, shape_format| - Core::Shape::Format(type).build(shape_format) - end - clean(f.layout) - end + if schema['version'] < '0.9.0' + 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 - # @param [Hash] raw_format A parsed json. - def compact_format!(raw_format) - %w( finger-print state version ).each {|attr| raw_format.delete(attr) } + def read_file(filename) + File.open(filename, 'r:UTF-8') {|f| f.read } end + end - # @param [String] filename - # @param [Boolean] force (false) - def build_once(filename, force = false, &block) - content = read_format_file(filename) + def initialize(*) + super + initialize_items(attributes['items']) + end - if force - block.call(content, nil) - else - id = Digest::SHA1.hexdigest(content).to_sym - built_format_registry[id] ||= block.call(content, id) - end - end + def user_paper_type? + page_paper_type == 'user' + end - # @param [String] filename - # @return [String] - def read_format_file(filename) - File.open(filename, 'r:UTF-8') {|f| f.read } - end + private - # @return [Hash] - def built_format_registry - @built_format_registry ||= {} + 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) + self.shapes[item.id.to_sym] = item end end end - end end