Class: DotStrings::Item

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

Overview

Represents a localized string item.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(key:, value:, comment: nil) ⇒ Item

Returns a new instance of Item.



9
10
11
12
13
# File 'lib/dotstrings/item.rb', line 9

def initialize(key:, value:, comment: nil)
  @comment = comment
  @key = key
  @value = value
end

Instance Attribute Details

#commentObject (readonly)

Returns the value of attribute comment.



7
8
9
# File 'lib/dotstrings/item.rb', line 7

def comment
  @comment
end

#keyObject (readonly)

Returns the value of attribute key.



7
8
9
# File 'lib/dotstrings/item.rb', line 7

def key
  @key
end

#valueObject (readonly)

Returns the value of attribute value.



7
8
9
# File 'lib/dotstrings/item.rb', line 7

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



15
16
17
# File 'lib/dotstrings/item.rb', line 15

def ==(other)
  eql?(other)
end

#eql?(other) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
22
23
24
# File 'lib/dotstrings/item.rb', line 19

def eql?(other)
  other.is_a?(Item) &&
    comment == other.comment &&
    key == other.key &&
    value == other.value
end

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

Serializes the item to string.

Parameters:

  • escape_single_quotes (Boolean) (defaults to: false)

    Whether to escape single quotes.

  • include_comment (Boolean) (defaults to: true)

    Whether to include the comment.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/dotstrings/item.rb', line 31

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

  result << "/* #{comment} */" unless comment.nil? || !include_comment
  result << format('"%<key>s" = "%<value>s";', {
    key: serialize_string(key, escape_single_quotes: escape_single_quotes),
    value: serialize_string(value, escape_single_quotes: escape_single_quotes)
  })

  result.join("\n")
end