Class: Autoini::Contents
- Inherits:
-
Object
- Object
- Autoini::Contents
- Defined in:
- lib/autoini/contents.rb
Constant Summary collapse
Instance Attribute Summary collapse
-
#lines ⇒ Object
readonly
Returns the value of attribute lines.
Class Method Summary collapse
Instance Method Summary collapse
- #<<(contents) ⇒ Object
- #==(c) ⇒ Object
-
#initialize(*contents) ⇒ Contents
constructor
A new instance of Contents.
- #merge!(other_contents) ⇒ Object
- #pair(key) ⇒ Object
- #section(key) ⇒ Object
- #to_h ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(*contents) ⇒ Contents
Returns a new instance of Contents.
42 43 44 45 |
# File 'lib/autoini/contents.rb', line 42 def initialize(*contents) @lines = [] self << contents end |
Instance Attribute Details
#lines ⇒ Object (readonly)
Returns the value of attribute lines.
5 6 7 |
# File 'lib/autoini/contents.rb', line 5 def lines @lines end |
Class Method Details
.[](hash) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/autoini/contents.rb', line 29 def self.[](hash) raise ArgumentError, "must pass a hash" unless hash.is_a?(Hash) new( *hash.map do |k, v| if v.is_a?(Hash) Section[k, v] else Pair.new(k, v) end end ) end |
.hash(contents) ⇒ Object
25 26 27 |
# File 'lib/autoini/contents.rb', line 25 def self.hash(contents) parse(contents).to_h end |
.parse(contents) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/autoini/contents.rb', line 7 def self.parse(contents) return new if contents.nil? || contents.empty? elements = [] section = nil contents.split("\n").each do |l| e = KLASSES.map{ |k| k.parse_with_comment(Autoini.divide(l.strip)) } .select(&:itself) .first || raise(ArgumentError, "couldn't parse line: #{l.inspect}") if e.is_a?(Section) section = e elements << section else (section || elements) << e end end new(*elements) end |
Instance Method Details
#<<(contents) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/autoini/contents.rb', line 47 def <<(contents) Autoini.wrap(contents).each do |c| unless c.is_a?(Element) raise ArgumentError, "#{c.class.name} must extend Autoini::Element" end if !c.is_a?(Section) && lines.last.is_a?(Section) raise ArgumentError, "Error on line #{c.inspect}: all elements " \ "after a section must be in a section" end lines << c end end |
#==(c) ⇒ Object
99 100 101 102 |
# File 'lib/autoini/contents.rb', line 99 def ==(c) c.is_a?(Contents) && c.lines.length == lines.length && lines.map.with_index{ |l, i| c.lines[i] == l }.all? end |
#merge!(other_contents) ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/autoini/contents.rb', line 68 def merge!(other_contents) unless other_contents.is_a?(Contents) raise ArgumentError, "must pass a Autoini::Contents" end other_contents.lines.each do |l| case l when Section if s = section(l.title) s.merge!(l) else self << l end when Pair if p = pair(l.key) p.value = l.value else self << l end end end self end |
#pair(key) ⇒ Object
64 65 66 |
# File 'lib/autoini/contents.rb', line 64 def pair(key) lines.select{ |l| l.is_a?(Pair) && l.key.to_s == key.to_s }.first end |
#section(key) ⇒ Object
60 61 62 |
# File 'lib/autoini/contents.rb', line 60 def section(key) lines.select{ |l| l.is_a?(Section) && l.title.to_s == key.to_s }.first end |
#to_h ⇒ Object
95 96 97 |
# File 'lib/autoini/contents.rb', line 95 def to_h lines.map(&:to_a).reject(&:empty?).to_h end |