class DotProperties

Constants

VERSION

Attributes

auto_expand[RW]

@!attribute [rw] auto_expand

@return [Boolean] Whether to expand resolvable variables within values on retrieval (default: +true+)

@!attribute [rw] default_delimiter

@return [String] The delimiter to use when adding new properties or when calling +normalize_delimiters!+ (default: '=')
default_delimiter[RW]

@!attribute [rw] auto_expand

@return [Boolean] Whether to expand resolvable variables within values on retrieval (default: +true+)

@!attribute [rw] default_delimiter

@return [String] The delimiter to use when adding new properties or when calling +normalize_delimiters!+ (default: '=')

Public Class Methods

load(file) click to toggle source
# File lib/dot_properties.rb, line 25
def self.load(file)
  self.parse(File.read(file))
end
new(lines=[]) click to toggle source
# File lib/dot_properties.rb, line 19
def initialize(lines=[])
  @content = lines.collect { |item| tokenize(item) }
  @auto_expand = true
  @default_delimiter = '='
end
parse(str) click to toggle source
# File lib/dot_properties.rb, line 29
def self.parse(str)
  self.new(str.split(/(?<!\\)\n/))
end

Public Instance Methods

<<(item) click to toggle source
# File lib/dot_properties.rb, line 60
def <<(item)
  @content << tokenize(item)
end
[](key) click to toggle source
# File lib/dot_properties.rb, line 52
def [](key)
  get(key)
end
[]=(key,value) click to toggle source
# File lib/dot_properties.rb, line 56
def []=(key,value)
  set(key, value)
end
compact!() click to toggle source

Strip all comments and blank lines, leaving only values

# File lib/dot_properties.rb, line 75
def compact!
  @content.reject! { |item| item[:type] != :value }
end
delete(key) click to toggle source
# File lib/dot_properties.rb, line 64
def delete(key)
  value = get(key)
  @content.reject! { |item| item[:type] == :value and item[:key] == key }
  return value
end
get(key, expand=@auto_expand) click to toggle source
# File lib/dot_properties.rb, line 33
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
inspect() click to toggle source
# File lib/dot_properties.rb, line 70
def inspect
  to_h.inspect
end
normalize_delimiters!() click to toggle source

Replace all delimiters with default_delimiter

# File lib/dot_properties.rb, line 80
def normalize_delimiters!
  @content.each { |item| item[:delimiter] = default_delimiter if item[:type] == :value }
end
set(key, value) click to toggle source
# File lib/dot_properties.rb, line 42
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!() click to toggle source

Strip all blank lines, leaving only comments and values

# File lib/dot_properties.rb, line 85
def strip_blanks!
  @content.reject! { |item| item[:type] == :blank }
end
strip_comments!() click to toggle source

Strip all comments, leaving only blank lines and values

# File lib/dot_properties.rb, line 90
def strip_comments!
  @content.reject! { |item| item[:type] == :comment }
end
to_a() click to toggle source

The assembled .properties file as an array of lines

# File lib/dot_properties.rb, line 95
def to_a
  @content.collect { |item| assemble(item) }
end
to_h() click to toggle source

All properties as a hash

# File lib/dot_properties.rb, line 100
def to_h
  Hash[@content.select { |item| item[:type] == :value }.collect { |item| item.values_at(:key,:value) }]
end
to_s() click to toggle source

The assembled .properties file as a string

# File lib/dot_properties.rb, line 105
def to_s
  to_a.join("\n")
end

Protected Instance Methods

assemble(item) click to toggle source
# File lib/dot_properties.rb, line 110
def assemble(item)
  if item[:type] == :value
    if item[:value].nil? or item[:value].empty?
      escape(item[:key])
    else
      "#{escape(item[:key])}#{item[:delimiter]}#{encode(item[:value])}"
    end
  else
    item[:value]
  end
end
decode(v) click to toggle source
# File lib/dot_properties.rb, line 141
def decode(v)
  v.gsub(/\\u([0-9A-Fa-f]{4})/) { |m| $1.hex.chr('UTF-8') }
end
encode(v) click to toggle source
# File lib/dot_properties.rb, line 137
def encode(v)
  v.gsub(/[\r\n]/) { |m| "\\u#{'%4.4X' % m.codepoints.to_a}" }.gsub(/\\/,'\\'*3)
end
escape(v) click to toggle source
# File lib/dot_properties.rb, line 145
def escape(v)
  v.gsub(/[\s:=\\]/) { |m| "\\#{m}" }
end
find_key(key) click to toggle source
# File lib/dot_properties.rb, line 133
def find_key(key)
  @content.find { |item| item[:type] == :value and item[:key] == key }
end
tokenize(item) click to toggle source
# File lib/dot_properties.rb, line 122
def tokenize(item)
  if item =~ /^\s*[#!]/
    { type: :comment, value: item }
  elsif item =~ /^\s*$/
    { type: :blank, value: item }
  else
    key, delimiter, value = item.strip.split /(\s*(?<!\\)[\s:=]\s*)/, 2
    { type: :value, key: unescape(decode(key)), delimiter: delimiter, value: unescape(decode(value.to_s.gsub(/\\\n\s*/,''))) }
  end
end
unescape(v) click to toggle source
# File lib/dot_properties.rb, line 149
def unescape(v)
  v.gsub(/(?<!\\)\\/,'')
end