Class: Weskit::WML::Formatter

Inherits:
Object
  • Object
show all
Defined in:
lib/weskit/wml/formatter.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Formatter

Returns a new instance of Formatter.



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/weskit/wml/formatter.rb', line 26

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

Instance Attribute Details

#separatorObject (readonly)

Returns the value of attribute separator.



5
6
7
# File 'lib/weskit/wml/formatter.rb', line 5

def separator
  @separator
end

Class Method Details

.colorObject



48
49
50
# File 'lib/weskit/wml/formatter.rb', line 48

def color
  @color ||= new options_color
end

.defaultObject



52
53
54
# File 'lib/weskit/wml/formatter.rb', line 52

def default
  @default or plain
end

.default=(item) ⇒ Object



56
57
58
59
# File 'lib/weskit/wml/formatter.rb', line 56

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

.plainObject



61
62
63
# File 'lib/weskit/wml/formatter.rb', line 61

def plain
  @plain ||= new options_plain
end

Instance Method Details

#format(item, indentation = 0) ⇒ Object



7
8
9
10
11
12
# File 'lib/weskit/wml/formatter.rb', line 7

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

#format_detached(item, formatter = nil) ⇒ Object



14
15
16
17
18
19
20
# File 'lib/weskit/wml/formatter.rb', line 14

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

  duplicate.formatter = formatter
  format item
end

#indent(content, width = 0) ⇒ Object



22
23
24
# File 'lib/weskit/wml/formatter.rb', line 22

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