class Lap::Constant

Public Class Methods

new(node, indent_level) click to toggle source
# File lib/lap/constant.rb, line 5
def initialize(node, indent_level)
  @node = node
  @indent_level = indent_level
end

Public Instance Methods

render() click to toggle source
# File lib/lap/constant.rb, line 10
def render
  "#{name} = #{value}\n".indent((Lap::Config[:indent] * @indent_level).to_i)
end

Private Instance Methods

name() click to toggle source
# File lib/lap/constant.rb, line 16
def name
  @node.name.name
end
record_contents(hsh) click to toggle source
# File lib/lap/constant.rb, line 38
def record_contents(hsh)
  inner_length = 0
  contents = hsh.map do |k, v|
    val = case v
          when RBS::Types::Literal
            v.literal
          else
            "nil"
          end
    pair = k.is_a?(Symbol) ? "#{k}: #{val}" : "#{k} => #{val}"
    inner_length += pair.length
    pair
  end

  if inner_length > Lap::Config[:preferred_line_length]
    "\n#{contents.join(",\n")}\n"
  else
    contents.join(", ")
  end
end
value() click to toggle source
# File lib/lap/constant.rb, line 20
def value
  case @node.type
  when RBS::Types::Record
    fields = @node.type.fields
    content = if fields.empty?
                nil
              else
                " #{record_contents(fields)} "
              end
    "{#{content}}"
  when RBS::Types::Literal
    @node.type.literal
  else
    warn "TODO: #{@node.type} not supported yet"
    ""
  end
end