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.



7057
7058
7059
7060
# File 'lib/syntax_tree.rb', line 7057

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

Instance Attribute Details

#keywordObject (readonly)

String

the keyword associated with this conditional



7052
7053
7054
# File 'lib/syntax_tree.rb', line 7052

def keyword
  @keyword
end

#nodeObject (readonly)

IfMod | UnlessMod

the node that is being formatted



7055
7056
7057
# File 'lib/syntax_tree.rb', line 7055

def node
  @node
end

Instance Method Details

#format(q) ⇒ Object



7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
# File 'lib/syntax_tree.rb', line 7062

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
      Parentheses.flat(q) do
        q.format(node.statement)
        q.text(" #{keyword} ")
        q.format(node.predicate)
      end
    end
  end
end