module EditorConfigGenerator # Object representation of an EditorConfig file # rubocop:disable Style/AccessorMethodName class EditorConfig attr_reader :root, :indent_style, :indent_size, :end_of_line, :charset, :trim_trailing_whitespace, :insert_final_newline, :file_type def initialize(options = {}) @root = nil @file_type = :* @indent_style = nil @end_of_line = nil @charset = nil @trim_trailing_whitespace = nil @insert_final_newline = nil OptionTransformer.transform_options(options) set_options(options) end def set_options(options) set_root_option(options) set_indent_style_option(options) set_indent_size_option(options) set_end_of_line_option(options) set_charset_option(options) set_trailing_space_option(options) set_final_newline_option(options) set_file_type_option(options) end def set_file_type_option(options) return if options[:file_type].nil? @file_type = options[:file_type] end def set_final_newline_option(options) return if options[:insert_final_newline].nil? @insert_final_newline = options[:insert_final_newline] end def set_trailing_space_option(options) return if options[:trim_trailing_whitespace].nil? @trim_trailing_whitespace = options[:trim_trailing_whitespace] end def set_charset_option(options) return if options[:charset].nil? @charset = options[:charset] end def set_end_of_line_option(options) return if options[:end_of_line].nil? @end_of_line = options[:end_of_line] end def set_indent_size_option(options) return if options[:indent_size].nil? @indent_size = options[:indent_size] end def set_indent_style_option(options) return if options[:indent_style].nil? @indent_style = options[:indent_style] end def set_root_option(options) return if options[:root].nil? @root = options[:root] end def to_s config_string = '' append_root_to_s(config_string) append_file_type_to_s(config_string) append_indent_style_to_s(config_string) append_indent_size_to_s(config_string) append_end_of_line_to_s(config_string) append_charset_to_s(config_string) append_whitespace_to_s(config_string) append_final_newline_to_s(config_string) config_string end def append_final_newline_to_s(config_string) return if @insert_final_newline.nil? config_string << "insert_final_newline = #{@insert_final_newline}\n" end def append_whitespace_to_s(config_string) return if @trim_trailing_whitespace.nil? config_string << 'trim_trailing_whitespace ' \ "= #{@trim_trailing_whitespace}\n" end def append_charset_to_s(config_string) return if @charset.nil? config_string << "charset = #{@charset}\n" end def append_end_of_line_to_s(config_string) return if @end_of_line.nil? config_string << "end_of_line = #{@end_of_line}\n" end def append_indent_size_to_s(config_string) return if @indent_size.nil? config_string << "indent_size = #{@indent_size}\n" end def append_indent_style_to_s(config_string) return if @indent_style.nil? config_string << "indent_style = #{@indent_style}\n" end def append_file_type_to_s(config_string) config_string << "[#{@file_type}]\n" end def append_root_to_s(config_string) return if @root.nil? config_string << "root = #{@root}\n" end def to_s_without_root lines = to_s.lines lines.delete_at 0 lines.join end def to_s_without_line(line_identifier) lines = to_s.lines index = lines.index(line_identifier) lines.delete_at index lines.join end def minimum? # The minimum editor config file is one with only root and file_type set @indent_style.nil? && @indent_size.nil? && @end_of_line.nil? && @charset.nil? && @trim_trailing_whitespace.nil? && @insert_final_newline.nil? end def defaults? minimum? && @file_type == :* && @root.nil? end end end