Class: Apstrings::DotStringFile

Inherits:
Object
  • Object
show all
Defined in:
lib/apstrings/dot_string_file.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw_file = '') ⇒ DotStringFile

Returns a new instance of DotStringFile.



6
7
8
9
# File 'lib/apstrings/dot_string_file.rb', line 6

def initialize(raw_file='')
  @kv_pairs = []
  @raw_file = raw_file
end

Instance Attribute Details

#kv_pairsObject

Returns the value of attribute kv_pairs.



4
5
6
# File 'lib/apstrings/dot_string_file.rb', line 4

def kv_pairs
  @kv_pairs
end

#raw_fileObject

Returns the value of attribute raw_file.



5
6
7
# File 'lib/apstrings/dot_string_file.rb', line 5

def raw_file
  @raw_file
end

Instance Method Details

#comments(args = {}) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/apstrings/dot_string_file.rb', line 29

def comments(args={})
  with_keys = args[:with_keys].nil? ? true : args[:with_keys]
  cleaned_pairs = kv_pairs.map do |pair|
    pair
  end
  with_keys ? build_comment_hash(cleaned_pairs) : cleaned_pairs.map(&:comment)
end

#key_valuesObject



11
12
13
14
15
# File 'lib/apstrings/dot_string_file.rb', line 11

def key_values
  kv_pairs.map do |pair|
    {pair.key => pair.value}
  end
end

#keysObject



17
18
19
20
21
# File 'lib/apstrings/dot_string_file.rb', line 17

def keys
  kv_pairs.map do |pair|
    pair.key
  end
end

#to_hash(args = {}) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/apstrings/dot_string_file.rb', line 37

def to_hash(args={})
  with_comments = args[:with_comments].nil? ? true : args[:with_comments]

  build_hash { |hash, pair|
  hash_value = with_comments ? { pair.value => pair.comment } : pair.value
    hash[pair.key] = hash_value
  }
end

#to_json(args = {}) ⇒ Object



46
47
48
# File 'lib/apstrings/dot_string_file.rb', line 46

def to_json(args={})
  self.to_hash(with_comments: args[:with_comments]).to_json
end

#valuesObject



23
24
25
26
27
# File 'lib/apstrings/dot_string_file.rb', line 23

def values
  kv_pairs.map do |pair|
    pair.value
  end
end