Class: DotStrings::File

Inherits:
Object
  • Object
show all
Defined in:
lib/dotstrings/file.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items = []) ⇒ File

Returns a new instance of File.



11
12
13
# File 'lib/dotstrings/file.rb', line 11

def initialize(items = [])
  @items = items
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



9
10
11
# File 'lib/dotstrings/file.rb', line 9

def items
  @items
end

Class Method Details

.parse(io) ⇒ Object



15
16
17
18
19
20
21
22
23
# File 'lib/dotstrings/file.rb', line 15

def self.parse(io)
  items = []

  parser = Parser.new
  parser.on_item { |item| items << item }
  parser << normalize_encoding(io.read)

  File.new(items)
end

.parse_file(path) ⇒ Object



25
26
27
28
29
# File 'lib/dotstrings/file.rb', line 25

def self.parse_file(path)
  ::File.open(path, 'r') do |file|
    parse(file)
  end
end

Instance Method Details

#<<(item) ⇒ Object



39
40
41
# File 'lib/dotstrings/file.rb', line 39

def <<(item)
  @items << item
end

#[](key) ⇒ Object



35
36
37
# File 'lib/dotstrings/file.rb', line 35

def [](key)
  @items.find { |item| item.key == key }
end

#append(item) ⇒ Object



43
44
45
# File 'lib/dotstrings/file.rb', line 43

def append(item)
  self << item
end

#delete(key) ⇒ Object



47
48
49
# File 'lib/dotstrings/file.rb', line 47

def delete(key)
  @items.delete_if { |item| item.key == key }
end

#keysObject



31
32
33
# File 'lib/dotstrings/file.rb', line 31

def keys
  @items.map(&:key)
end

#to_s(escape_single_quotes: false, comments: true) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dotstrings/file.rb', line 51

def to_s(escape_single_quotes: false, comments: true)
  result = []

  @items.each do |item|
    result << item.to_s(
      escape_single_quotes: escape_single_quotes,
      include_comment: comments
    )

    result << ''
  end

  result.join("\n")
end