Class: SyntaxTree::ConditionalModFormatter

Inherits:
Object
  • Object
show all
Defined in:
lib/syntax_tree.rb

Overview

Formats an IfMod or UnlessMod node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(keyword, node) ⇒ ConditionalModFormatter

Returns a new instance of ConditionalModFormatter.



6689
6690
6691
6692
# File 'lib/syntax_tree.rb', line 6689

def initialize(keyword, node)
  @keyword = keyword
  @node = node
end

Instance Attribute Details

#keywordObject (readonly)

String

the keyword associated with this conditional



6684
6685
6686
# File 'lib/syntax_tree.rb', line 6684

def keyword
  @keyword
end

#nodeObject (readonly)

IfMod | UnlessMod

the node that is being formatted



6687
6688
6689
# File 'lib/syntax_tree.rb', line 6687

def node
  @node
end

Instance Method Details

#format(q) ⇒ Object



6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
# File 'lib/syntax_tree.rb', line 6694

def format(q)
  q.group do
    q.if_break do
      q.text("#{keyword} ")
      q.nest(keyword.length + 1) { q.format(node.predicate) }
      q.indent do
        q.breakable
        q.format(node.statement)
      end
      q.breakable
      q.text("end")
    end.if_flat do
      q.format(node.statement)
      q.text(" #{keyword} ")
      q.format(node.predicate)
    end
  end
end