Sha256: dd4c7fc8bb051a5e3ae3c1c507619bbeea1c8a0bd200b4a9b35fafc4a468427c

Contents?: true

Size: 1.42 KB

Versions: 2

Compression:

Stored size: 1.42 KB

Contents

# coding: utf-8

require 'json'

module Thinreports::Core
  module Format

    module Builder
      def build(*args)
        build_internal(*args)
      rescue Thinreports::Errors::Basic => e
        raise
      rescue => e
        raise Thinreports::Errors::InvalidLayoutFormat
      end

      # @abstract
      def build_internal(*args)
        raise NotImplementedError
      end

      # @param [Thinreports::Core::Format::Base] format
      # @param [Hash] options
      def build_layout(format, options = {}, &block)
        level = '-' * ((options[:level] || 1 ) - 1)
        pattern = /<!--#{level}SHAPE(.*?)SHAPE#{level}-->/

        format.layout.scan(pattern) do |m|
          shape_format = block.call(*parsed_format_and_shape_type(m.first))
          format.shapes[shape_format.id.to_sym] = shape_format
        end
        format.layout.gsub!(pattern, '')
      end

      # @param [String] svg
      def clean(svg)
        svg.gsub!(/<!--.*?-->/, '')
      end

      # @param [String] svg
      def clean_with_attributes(svg)
        clean(svg)
        svg.gsub!(/ x\-[a-z\d\-]+?=".*?"/, '')
        svg.gsub!(/ class=".*?"/, '')
      end

      def shape_tag(format)
        %{<%= r(:"#{format.id}")%>}
      end

      def parsed_format_and_shape_type(json_string)
        f = parse_json(json_string)
        [ f['type'], f ]
      end

      def parse_json(json_string)
        JSON.parse(json_string)
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
thinreports-0.8.1 lib/thinreports/core/format/builder.rb
thinreports-0.8.0 lib/thinreports/core/format/builder.rb