class TmuxERBParser::Converter

Public Class Methods

convert(structured) click to toggle source
# File lib/tmux-erb-parser/converter.rb, line 6
def convert(structured)
  structured = [structured] if structured.is_a?(Hash)

  structured.inject([]) do |result, hash|
    result << '' unless result.empty?

    comment = hash.is_a?(Hash) && hash.delete('comment')
    result << "# #{comment}" if comment

    result.concat([*convert_structured(hash)])
  end
end

Private Class Methods

convert_hash(hash, prefix = []) click to toggle source
# File lib/tmux-erb-parser/converter.rb, line 21
def convert_hash(hash, prefix = [])
  converted = hash.map do |key, value|
    case key
    when '"'
      convert_structured(value, [*prefix, %('"')])
    when 'if', 'if-shell', 'run', 'run-shell'
      convert_structured_shell(value, [*prefix, key])
    when /style/
      convert_structured_style(value, [*prefix, key])
    else
      convert_structured(value, [*prefix, key])
    end
  end
  converted.flatten
end
convert_string(string, prefix = []) click to toggle source
# File lib/tmux-erb-parser/converter.rb, line 37
def convert_string(string, prefix = [])
  [*prefix, string].join(' ')
end
convert_structured(item, prefix = []) click to toggle source
# File lib/tmux-erb-parser/converter.rb, line 41
def convert_structured(item, prefix = [])
  case item
  when Array
    item.map { |i| convert_structured(i, prefix) }
  when Hash
    convert_hash(item, prefix)
  else
    convert_string(item, prefix)
  end
end
convert_structured_shell(item, prefix = []) click to toggle source
# File lib/tmux-erb-parser/converter.rb, line 52
def convert_structured_shell(item, prefix = [])
  case item
  when Array
    args = item.map { |i| convert_structured(i, []) }
    args = args.flatten.map do |arg|
      arg.include?(' ') ? %('#{arg}') : arg
    end

    [*prefix, *args].join(' ')
  else
    convert_string(item, prefix)
  end
end
convert_structured_style(item, prefix = []) click to toggle source
# File lib/tmux-erb-parser/converter.rb, line 66
def convert_structured_style(item, prefix = [])
  case item
  when Array
    convert_string(item.join(','), prefix)
  when Hash
    convert_string(
      item.map { |key, value| "#{key}=#{value}" }.join(','),
      prefix
    )
  else
    convert_string(item, prefix)
  end
end