Class: DotStrings::File
- Inherits:
-
Object
- Object
- DotStrings::File
- Defined in:
- lib/dotstrings/file.rb
Overview
Represents a .strings file.
It provides methods to parse .strings, as well as methods for accessing and manipulating localized string items.
Instance Attribute Summary collapse
-
#items ⇒ Object
readonly
All items in the file.
Class Method Summary collapse
-
.parse(io, strict: true) ⇒ DotStrings::File
Parses a file from the given IO object.
-
.parse_file(path, strict: true) ⇒ DotStrings::File
Parses the file at the given path.
Instance Method Summary collapse
-
#<<(item) ⇒ DotStrings::Item
Appends an item to the file.
- #==(other) ⇒ Object
-
#[](key) ⇒ DotStrings::Item
Returns an item by key, if it exists, otherwise nil.
-
#append(item) ⇒ Object
Appends an item to the file.
-
#count(&block) ⇒ Object
Returns the number of items in the file.
-
#delete(key) ⇒ Object
Deletes an item by key.
-
#delete_if(&block) ⇒ Object
Deletes all items for which the block returns true.
-
#each(&block) ⇒ Object
Calls the given block once for each item in the file.
-
#empty? ⇒ Boolean
Returns ‘true` if the file doen’t contain any items.
- #eql?(other) ⇒ Boolean
-
#initialize(items = []) ⇒ File
constructor
A new instance of File.
-
#keys ⇒ Object
Returns all keys in the file.
-
#length ⇒ Object
Returns the number of items in the file.
-
#sort(&block) ⇒ Object
Returns a new File with the items sorted using the given comparator block.
-
#sort!(&block) ⇒ Object
Sort the items using the given block.
-
#to_s(escape_single_quotes: false, comments: true) ⇒ Object
Serializes the file to a string.
Constructor Details
#initialize(items = []) ⇒ File
Returns a new instance of File.
18 19 20 |
# File 'lib/dotstrings/file.rb', line 18 def initialize(items = []) @items = items end |
Instance Attribute Details
#items ⇒ Object (readonly)
All items in the file.
16 17 18 |
# File 'lib/dotstrings/file.rb', line 16 def items @items end |
Class Method Details
.parse(io, strict: true) ⇒ DotStrings::File
Parses a file from the given IO object.
51 52 53 54 55 56 57 58 59 |
# File 'lib/dotstrings/file.rb', line 51 def self.parse(io, strict: true) items = [] parser = Parser.new(strict: strict) parser.on_item { |item| items << item } parser << normalize_encoding(io.read) File.new(items) end |
.parse_file(path, strict: true) ⇒ DotStrings::File
Parses the file at the given path.
71 72 73 74 75 |
# File 'lib/dotstrings/file.rb', line 71 def self.parse_file(path, strict: true) ::File.open(path, 'r') do |file| parse(file, strict: strict) end end |
Instance Method Details
#<<(item) ⇒ DotStrings::Item
Appends an item to the file.
106 107 108 109 |
# File 'lib/dotstrings/file.rb', line 106 def <<(item) @items << item self end |
#==(other) ⇒ Object
173 174 175 |
# File 'lib/dotstrings/file.rb', line 173 def ==(other) eql?(other) end |
#[](key) ⇒ DotStrings::Item
Returns an item by key, if it exists, otherwise nil.
94 95 96 |
# File 'lib/dotstrings/file.rb', line 94 def [](key) @items.find { |item| item.key == key } end |
#append(item) ⇒ Object
Appends an item to the file.
116 117 118 |
# File 'lib/dotstrings/file.rb', line 116 def append(item) self << item end |
#count(&block) ⇒ Object
Returns the number of items in the file.
If a block is given, it will count the number of items for which the block returns true.
163 164 165 |
# File 'lib/dotstrings/file.rb', line 163 def count(&block) @items.count(&block) end |
#delete(key) ⇒ Object
Deletes an item by key.
122 123 124 |
# File 'lib/dotstrings/file.rb', line 122 def delete(key) @items.delete_if { |item| item.key == key } end |
#delete_if(&block) ⇒ Object
Deletes all items for which the block returns true.
131 132 133 134 |
# File 'lib/dotstrings/file.rb', line 131 def delete_if(&block) @items.delete_if(&block) self end |
#each(&block) ⇒ Object
Calls the given block once for each item in the file.
144 145 146 147 |
# File 'lib/dotstrings/file.rb', line 144 def each(&block) @items.each(&block) self end |
#empty? ⇒ Boolean
Returns ‘true` if the file doen’t contain any items.
169 170 171 |
# File 'lib/dotstrings/file.rb', line 169 def empty? @items.empty? end |
#eql?(other) ⇒ Boolean
177 178 179 |
# File 'lib/dotstrings/file.rb', line 177 def eql?(other) other.is_a?(self.class) && @items.eql?(other.items) end |
#keys ⇒ Object
Returns all keys in the file.
79 80 81 |
# File 'lib/dotstrings/file.rb', line 79 def keys @items.map(&:key) end |
#length ⇒ Object
Returns the number of items in the file.
151 152 153 |
# File 'lib/dotstrings/file.rb', line 151 def length @items.length end |
#sort(&block) ⇒ Object
Returns a new File with the items sorted using the given comparator block.
If no block is given, the items will be sorted by key.
26 27 28 29 |
# File 'lib/dotstrings/file.rb', line 26 def sort(&block) new_file = dup new_file.sort!(&block) end |
#sort!(&block) ⇒ Object
Sort the items using the given block.
If no block is given, the items will be sorted by key.
35 36 37 38 |
# File 'lib/dotstrings/file.rb', line 35 def sort!(&block) @items.sort!(&block || ->(a, b) { a.key <=> b.key }) self end |
#to_s(escape_single_quotes: false, comments: true) ⇒ Object
Serializes the file to a string.
186 187 188 189 190 191 192 193 194 195 196 197 198 199 |
# File 'lib/dotstrings/file.rb', line 186 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 |