Class: Autoini::Section

Inherits:
Element show all
Includes:
InlineComment
Defined in:
lib/autoini/section.rb

Instance Attribute Summary collapse

Attributes included from InlineComment

#comment

Class Method Summary collapse

Instance Method Summary collapse

Methods included from InlineComment

included, #line_comment

Constructor Details

#initialize(title, *contents) ⇒ Section

Returns a new instance of Section.



8
9
10
11
12
# File 'lib/autoini/section.rb', line 8

def initialize(title, *contents)
  @title = title
  @lines = []
  self << contents
end

Instance Attribute Details

#linesObject (readonly)

Returns the value of attribute lines.



6
7
8
# File 'lib/autoini/section.rb', line 6

def lines
  @lines
end

#titleObject

Returns the value of attribute title.



5
6
7
# File 'lib/autoini/section.rb', line 5

def title
  @title
end

Class Method Details

.parse(line) ⇒ Object



33
34
35
# File 'lib/autoini/section.rb', line 33

def self.parse(line)
  Section.new(line[1]) if line.length == 3 && line[0] == '[' && line[2] == ']'
end

Instance Method Details

#<<(contents) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/autoini/section.rb', line 14

def <<(contents)
  Autoini.wrap(contents).each do |c|
    unless c.is_a?(AbstractLine)
      raise ArgumentError, "#{c.class.name} must extend Autoini::AbstractLine"
    end
    @lines << c
  end
end

#==(e) ⇒ Object



27
28
29
30
31
# File 'lib/autoini/section.rb', line 27

def ==(e)
  e.is_a?(Section) && e.title == title && e.comment == comment &&
    e.lines.length == lines.length &&
    lines.map.with_index{ |l, i| e.lines[i] == l }.all?
end

#to_sObject



23
24
25
# File 'lib/autoini/section.rb', line 23

def to_s
  [line_comment("[#{title}]"), lines.map(&:to_s)].flatten.join(Autoini.newline)
end