Sha256: 16407a26c8f14d4b5c6521f92aa14ed7c9bcaa9a18a5fd6fb64b4a3a3e4d2bf3

Contents?: true

Size: 1.61 KB

Versions: 10

Compression:

Stored size: 1.61 KB

Contents

module Weskit::WML
  class Formatter
    private_class_method :new

    attr_reader :separator

    def format item, indentation = 0
      case item
        when Attribute then @attr_formatter.format item, indentation
        when Element   then @elem_formatter.format item, indentation
      end
    end

    def format_detached item, formatter = nil
      formatter ||= item.formatter
      duplicate   = item.dup.detach

      duplicate.formatter = formatter
      format item
    end

    def indent content, width = 0
      @indent * width + "#{content}"
    end

    def initialize options = {}
      options = options_default.merge options

      @attr_formatter = options[:attr_formatter].new self
      @elem_formatter = options[:elem_formatter].new self

      @indent    = options[:indent]
      @separator = options[:separator]

      self
    end

    private

    def options_default
      {
        :indent    => '  ',
        :separator => $/
      }
    end

    class << self
      def color
        @color ||= new options_color
      end

      def default
        @default or plain
      end

      def default= item
        Mixins::Validator.raise_unless Formatter, item
        @default = item
      end

      def plain
        @plain ||= new options_plain
      end

      private

      def options_color
        {
          :attr_formatter => Formatters::AnsiColorAttribute,
          :elem_formatter => Formatters::AnsiColorElement
        }
      end

      def options_plain
        {
          :attr_formatter => Formatters::Attribute,
          :elem_formatter => Formatters::Element
        }
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
weskit-0.3.6 lib/weskit/wml/formatter.rb
weskit-0.3.5 lib/weskit/wml/formatter.rb
weskit-0.3.4 lib/weskit/wml/formatter.rb
weskit-0.3.3 lib/weskit/wml/formatter.rb
weskit-0.3.2 lib/weskit/wml/formatter.rb
weskit-0.3.1 lib/weskit/wml/formatter.rb
weskit-0.3.0 lib/weskit/wml/formatter.rb
weskit-0.2.1 lib/weskit/wml/formatter.rb
weskit-0.2.0 lib/weskit/wml/formatter.rb
weskit-0.1.0 lib/weskit/wml/formatter.rb