Class: Y2R::AST::Ruby::TernaryOperator

Inherits:
Node
  • Object
show all
Defined in:
lib/y2r/ast/ruby.rb

Constant Summary

Constants inherited from Node

Node::INDENT_STEP

Instance Method Summary collapse

Methods inherited from Node

#ensure_separated, #has_comment?, #hates_to_stand_alone?, #single_line_width, #to_ruby

Instance Method Details

#ends_with_comment?Boolean

Returns:

  • (Boolean)


1086
1087
1088
# File 'lib/y2r/ast/ruby.rb', line 1086

def ends_with_comment?
  comment_after || self.else.ends_with_comment?
end

#pass_trailer?Boolean

Returns:

  • (Boolean)


1090
1091
1092
# File 'lib/y2r/ast/ruby.rb', line 1090

def pass_trailer?
  true
end

#priorityObject



1078
1079
1080
# File 'lib/y2r/ast/ruby.rb', line 1078

def priority
  Priority::TERNARY
end

#single_line_width_base(context) ⇒ Object



1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
# File 'lib/y2r/ast/ruby.rb', line 1064

def single_line_width_base(context)
  if !has_line_breaking_comment?
    inner_context = context.with_priority(priority)

    condition_width = condition.single_line_width(inner_context)
    then_width      = self.then.single_line_width(inner_context)
    else_width      = self.else.single_line_width(inner_context)

    condition_width + 3 + then_width + 3 + else_width
  else
    Float::INFINITY
  end
end

#starts_with_comment?Boolean

Returns:

  • (Boolean)


1082
1083
1084
# File 'lib/y2r/ast/ruby.rb', line 1082

def starts_with_comment?
  comment_before || condition.starts_with_comment?
end

#to_ruby_base(context) ⇒ Object



1055
1056
1057
1058
1059
1060
1061
1062
# File 'lib/y2r/ast/ruby.rb', line 1055

def to_ruby_base(context)
  if (fits_current_line?(context) || (self.then.hates_to_stand_alone? && self.else.hates_to_stand_alone?)) &&
      !has_line_breaking_comment?
    to_ruby_base_single_line(context)
  else
    to_ruby_base_multi_line(context)
  end
end