Class: DotProperties

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/dot_properties.rb,
lib/dot_properties/version.rb

Constant Summary collapse

VERSION =
"0.1.2"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lines = []) ⇒ DotProperties

Returns a new instance of DotProperties.



18
19
20
21
22
# File 'lib/dot_properties.rb', line 18

def initialize(lines=[])
  @content = lines.collect { |item| tokenize(item) }
  @auto_expand = true
  @default_delimiter = '='
end

Instance Attribute Details

#auto_expandBoolean

Returns Whether to expand resolvable variables within values on retrieval (default: true).

Returns:

  • (Boolean)

    Whether to expand resolvable variables within values on retrieval (default: true)



16
17
18
# File 'lib/dot_properties.rb', line 16

def auto_expand
  @auto_expand
end

#default_delimiterString

Returns The delimiter to use when adding new properties or when calling normalize_delimiters! (default: ‘=’).

Returns:

  • (String)

    The delimiter to use when adding new properties or when calling normalize_delimiters! (default: ‘=’)



16
# File 'lib/dot_properties.rb', line 16

attr_accessor :auto_expand, :default_delimiter

Class Method Details

.load(file) ⇒ Object



24
25
26
# File 'lib/dot_properties.rb', line 24

def self.load(file)
  self.parse(File.read(file))
end

.parse(str) ⇒ Object



28
29
30
# File 'lib/dot_properties.rb', line 28

def self.parse(str)
  self.new(str.split(/(?<!\\)\n/))
end

Instance Method Details

#<<(item) ⇒ Object



59
60
61
# File 'lib/dot_properties.rb', line 59

def <<(item)
  @content << tokenize(item)
end

#[](key) ⇒ Object



51
52
53
# File 'lib/dot_properties.rb', line 51

def [](key)
  get(key)
end

#[]=(key, value) ⇒ Object



55
56
57
# File 'lib/dot_properties.rb', line 55

def []=(key,value)
  set(key, value)
end

#compact!Object

Strip all comments and blank lines, leaving only values



74
75
76
# File 'lib/dot_properties.rb', line 74

def compact!
  @content.reject! { |item| item[:type] != :value }
end

#delete(key) ⇒ Object



63
64
65
66
67
# File 'lib/dot_properties.rb', line 63

def delete(key)
  value = get(key)
  @content.reject! { |item| item[:type] == :value and item[:key] == key }
  return value
end

#get(key, expand = @auto_expand) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/dot_properties.rb', line 32

def get(key, expand=@auto_expand)
  item = find_key(key)
  value = (item && item[:value]) || nil
  if value and expand
    value = value.gsub(/\$\{(.+?)\}/) { |v| has_key?($1) ? get($1,true) : v }
  end
  return value
end

#inspectObject



69
70
71
# File 'lib/dot_properties.rb', line 69

def inspect
  to_h.inspect
end

#normalize_delimiters!Object

Replace all delimiters with default_delimiter



79
80
81
# File 'lib/dot_properties.rb', line 79

def normalize_delimiters!
  @content.each { |item| item[:delimiter] = default_delimiter if item[:type] == :value }
end

#set(key, value) ⇒ Object



41
42
43
44
45
46
47
48
49
# File 'lib/dot_properties.rb', line 41

def set(key, value)
  item = find_key(key)
  if item
    item[:value] = value
  else
    @content << { type: :value, key: key, delimiter: default_delimiter, value: value }
  end
  return value
end

#strip_blanks!Object

Strip all blank lines, leaving only comments and values



84
85
86
# File 'lib/dot_properties.rb', line 84

def strip_blanks!
  @content.reject! { |item| item[:type] == :blank }
end

#strip_comments!Object

Strip all comments, leaving only blank lines and values



89
90
91
# File 'lib/dot_properties.rb', line 89

def strip_comments!
  @content.reject! { |item| item[:type] == :comment }
end

#to_aObject

The assembled .properties file as an array of lines



94
95
96
# File 'lib/dot_properties.rb', line 94

def to_a
  @content.collect { |item| assemble(item) }
end

#to_hObject

All properties as a hash



99
100
101
# File 'lib/dot_properties.rb', line 99

def to_h
  Hash[@content.select { |item| item[:type] == :value }.collect { |item| item.values_at(:key,:value) }]
end

#to_sObject

The assembled .properties file as a string



104
105
106
# File 'lib/dot_properties.rb', line 104

def to_s
  to_a.join("\n")
end