Class: EditorConfigGenerator::EditorConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/editorconfig/editor_config.rb

Overview

Object representation of an EditorConfig file rubocop:disable Style/AccessorMethodName

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EditorConfig



9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/editorconfig/editor_config.rb', line 9

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

Instance Attribute Details

#charsetObject (readonly)

Returns the value of attribute charset.



5
6
7
# File 'lib/editorconfig/editor_config.rb', line 5

def charset
  @charset
end

#end_of_lineObject (readonly)

Returns the value of attribute end_of_line.



5
6
7
# File 'lib/editorconfig/editor_config.rb', line 5

def end_of_line
  @end_of_line
end

#file_typeObject (readonly)

Returns the value of attribute file_type.



5
6
7
# File 'lib/editorconfig/editor_config.rb', line 5

def file_type
  @file_type
end

#indent_sizeObject (readonly)

Returns the value of attribute indent_size.



5
6
7
# File 'lib/editorconfig/editor_config.rb', line 5

def indent_size
  @indent_size
end

#indent_styleObject (readonly)

Returns the value of attribute indent_style.



5
6
7
# File 'lib/editorconfig/editor_config.rb', line 5

def indent_style
  @indent_style
end

#insert_final_newlineObject (readonly)

Returns the value of attribute insert_final_newline.



5
6
7
# File 'lib/editorconfig/editor_config.rb', line 5

def insert_final_newline
  @insert_final_newline
end

#rootObject (readonly)

Returns the value of attribute root.



5
6
7
# File 'lib/editorconfig/editor_config.rb', line 5

def root
  @root
end

#trim_trailing_whitespaceObject (readonly)

Returns the value of attribute trim_trailing_whitespace.



5
6
7
# File 'lib/editorconfig/editor_config.rb', line 5

def trim_trailing_whitespace
  @trim_trailing_whitespace
end

Instance Method Details

#append_charset_to_s(config_string) ⇒ Object



97
98
99
100
# File 'lib/editorconfig/editor_config.rb', line 97

def append_charset_to_s(config_string)
  return if @charset.nil?
  config_string << "charset = #{@charset}\n"
end

#append_end_of_line_to_s(config_string) ⇒ Object



102
103
104
105
# File 'lib/editorconfig/editor_config.rb', line 102

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

#append_file_type_to_s(config_string) ⇒ Object



117
118
119
# File 'lib/editorconfig/editor_config.rb', line 117

def append_file_type_to_s(config_string)
  config_string << "[#{@file_type}]\n"
end

#append_final_newline_to_s(config_string) ⇒ Object



86
87
88
89
# File 'lib/editorconfig/editor_config.rb', line 86

def append_final_newline_to_s(config_string)
  return if @insert_final_newline.nil?
  config_string << "insert_final_newline = #{@insert_final_newline}\n"
end

#append_indent_size_to_s(config_string) ⇒ Object



107
108
109
110
# File 'lib/editorconfig/editor_config.rb', line 107

def append_indent_size_to_s(config_string)
  return if @indent_size.nil?
  config_string << "indent_size = #{@indent_size}\n"
end

#append_indent_style_to_s(config_string) ⇒ Object



112
113
114
115
# File 'lib/editorconfig/editor_config.rb', line 112

def append_indent_style_to_s(config_string)
  return if @indent_style.nil?
  config_string << "indent_style = #{@indent_style}\n"
end

#append_root_to_s(config_string) ⇒ Object



121
122
123
124
# File 'lib/editorconfig/editor_config.rb', line 121

def append_root_to_s(config_string)
  return if @root.nil?
  config_string << "root = #{@root}\n"
end

#append_whitespace_to_s(config_string) ⇒ Object



91
92
93
94
95
# File 'lib/editorconfig/editor_config.rb', line 91

def append_whitespace_to_s(config_string)
  return if @trim_trailing_whitespace.nil?
  config_string << 'trim_trailing_whitespace ' \
                   "= #{@trim_trailing_whitespace}\n"
end

#defaults?Boolean



149
150
151
152
153
# File 'lib/editorconfig/editor_config.rb', line 149

def defaults?
  minimum? &&
    @file_type == :* &&
    @root.nil?
end

#minimum?Boolean



139
140
141
142
143
144
145
146
147
# File 'lib/editorconfig/editor_config.rb', line 139

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

#set_charset_option(options) ⇒ Object



48
49
50
51
# File 'lib/editorconfig/editor_config.rb', line 48

def set_charset_option(options)
  return if options[:charset].nil?
  @charset = options[:charset]
end

#set_end_of_line_option(options) ⇒ Object



53
54
55
56
# File 'lib/editorconfig/editor_config.rb', line 53

def set_end_of_line_option(options)
  return if options[:end_of_line].nil?
  @end_of_line = options[:end_of_line]
end

#set_file_type_option(options) ⇒ Object



33
34
35
36
# File 'lib/editorconfig/editor_config.rb', line 33

def set_file_type_option(options)
  return if options[:file_type].nil?
  @file_type = options[:file_type]
end

#set_final_newline_option(options) ⇒ Object



38
39
40
41
# File 'lib/editorconfig/editor_config.rb', line 38

def set_final_newline_option(options)
  return if options[:insert_final_newline].nil?
  @insert_final_newline = options[:insert_final_newline]
end

#set_indent_size_option(options) ⇒ Object



58
59
60
61
# File 'lib/editorconfig/editor_config.rb', line 58

def set_indent_size_option(options)
  return if options[:indent_size].nil?
  @indent_size = options[:indent_size]
end

#set_indent_style_option(options) ⇒ Object



63
64
65
66
# File 'lib/editorconfig/editor_config.rb', line 63

def set_indent_style_option(options)
  return if options[:indent_style].nil?
  @indent_style = options[:indent_style]
end

#set_options(options) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/editorconfig/editor_config.rb', line 22

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

#set_root_option(options) ⇒ Object



68
69
70
71
# File 'lib/editorconfig/editor_config.rb', line 68

def set_root_option(options)
  return if options[:root].nil?
  @root = options[:root]
end

#set_trailing_space_option(options) ⇒ Object



43
44
45
46
# File 'lib/editorconfig/editor_config.rb', line 43

def set_trailing_space_option(options)
  return if options[:trim_trailing_whitespace].nil?
  @trim_trailing_whitespace = options[:trim_trailing_whitespace]
end

#to_sObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/editorconfig/editor_config.rb', line 73

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

#to_s_without_line(line_identifier) ⇒ Object



132
133
134
135
136
137
# File 'lib/editorconfig/editor_config.rb', line 132

def to_s_without_line(line_identifier)
  lines = to_s.lines
  index = lines.index(line_identifier)
  lines.delete_at index
  lines.join
end

#to_s_without_rootObject



126
127
128
129
130
# File 'lib/editorconfig/editor_config.rb', line 126

def to_s_without_root
  lines = to_s.lines
  lines.delete_at 0
  lines.join
end