Class: Autoini::Section
- Includes:
- InlineComment
- Defined in:
- lib/autoini/section.rb
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
-
#title ⇒ Object
Returns the value of attribute title.
Attributes included from InlineComment
Class Method Summary collapse
Instance Method Summary collapse
- #<<(contents) ⇒ Object
- #==(e) ⇒ Object
-
#initialize(title, *contents) ⇒ Section
constructor
A new instance of Section.
- #merge!(other_section) ⇒ Object
- #pair(key) ⇒ Object
- #to_a ⇒ Object
- #to_s ⇒ Object
Methods included from InlineComment
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
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
6 7 8 |
# File 'lib/autoini/section.rb', line 6 def lines @lines end |
#title ⇒ Object
Returns the value of attribute title.
5 6 7 |
# File 'lib/autoini/section.rb', line 5 def title @title end |
Class Method Details
Instance Method Details
#<<(contents) ⇒ Object
19 20 21 22 23 24 25 26 |
# File 'lib/autoini/section.rb', line 19 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
36 37 38 39 40 |
# File 'lib/autoini/section.rb', line 36 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 |
#merge!(other_section) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/autoini/section.rb', line 46 def merge!(other_section) unless other_section.is_a?(Section) raise ArgumentError, "must pass a Autoini::Section" end other_section.lines.each do |l| next unless l.is_a?(Pair) if p = pair(l.key) p.value = l.value else self << l end end self end |
#pair(key) ⇒ Object
42 43 44 |
# File 'lib/autoini/section.rb', line 42 def pair(key) lines.select{ |l| l.is_a?(Pair) && l.key.to_s == key.to_s }.first end |
#to_a ⇒ Object
32 33 34 |
# File 'lib/autoini/section.rb', line 32 def to_a [title.to_sym, lines.map(&:to_a).reject(&:empty?).to_h] end |