Class: DotProperties
- Inherits:
-
Object
- Object
- DotProperties
- Extended by:
- Forwardable
- Defined in:
- lib/dot_properties.rb,
lib/dot_properties/version.rb
Constant Summary collapse
- VERSION =
"0.1.3"
Instance Attribute Summary collapse
-
#auto_expand ⇒ Boolean
Whether to expand resolvable variables within values on retrieval (default:
true
). -
#default_delimiter ⇒ String
The delimiter to use when adding new properties or when calling
normalize_delimiters!
(default: ‘=’).
Class Method Summary collapse
Instance Method Summary collapse
- #<<(item) ⇒ Object
- #[](key) ⇒ Object
- #[]=(key, value) ⇒ Object
-
#compact! ⇒ Object
Strip all comments and blank lines, leaving only values.
- #delete(key) ⇒ Object
- #get(key, expand = @auto_expand) ⇒ Object
-
#initialize(lines = []) ⇒ DotProperties
constructor
A new instance of DotProperties.
- #inspect ⇒ Object
-
#normalize_delimiters! ⇒ Object
Replace all delimiters with
default_delimiter
. - #set(key, value) ⇒ Object
-
#strip_blanks! ⇒ Object
Strip all blank lines, leaving only comments and values.
-
#strip_comments! ⇒ Object
Strip all comments, leaving only blank lines and values.
-
#to_a ⇒ Object
The assembled .properties file as an array of lines.
-
#to_h ⇒ Object
All properties as a hash.
-
#to_s ⇒ Object
The assembled .properties file as a string.
Constructor Details
#initialize(lines = []) ⇒ DotProperties
Returns a new instance of DotProperties.
19 20 21 22 23 |
# File 'lib/dot_properties.rb', line 19 def initialize(lines=[]) @content = lines.collect { |item| tokenize(item) } @auto_expand = true @default_delimiter = '=' end |
Instance Attribute Details
#auto_expand ⇒ Boolean
Returns Whether to expand resolvable variables within values on retrieval (default: true
).
17 18 19 |
# File 'lib/dot_properties.rb', line 17 def @auto_expand end |
#default_delimiter ⇒ String
Returns The delimiter to use when adding new properties or when calling normalize_delimiters!
(default: ‘=’).
17 |
# File 'lib/dot_properties.rb', line 17 attr_accessor :auto_expand, :default_delimiter |
Class Method Details
.load(file) ⇒ Object
25 26 27 |
# File 'lib/dot_properties.rb', line 25 def self.load(file) self.parse(File.read(file)) end |
.parse(str) ⇒ Object
29 30 31 |
# File 'lib/dot_properties.rb', line 29 def self.parse(str) self.new(str.split(/(?<!\\)\n/)) end |
Instance Method Details
#<<(item) ⇒ Object
60 61 62 |
# File 'lib/dot_properties.rb', line 60 def <<(item) @content << tokenize(item) end |
#[](key) ⇒ Object
52 53 54 |
# File 'lib/dot_properties.rb', line 52 def [](key) get(key) end |
#[]=(key, value) ⇒ Object
56 57 58 |
# File 'lib/dot_properties.rb', line 56 def []=(key,value) set(key, value) end |
#compact! ⇒ Object
Strip all comments and blank lines, leaving only values
75 76 77 |
# File 'lib/dot_properties.rb', line 75 def compact! @content.reject! { |item| item[:type] != :value } end |
#delete(key) ⇒ Object
64 65 66 67 68 |
# 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) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'lib/dot_properties.rb', line 33 def get(key, =@auto_expand) item = find_key(key) value = (item && item[:value]) || nil if value and value = value.gsub(/\$\{(.+?)\}/) { |v| has_key?($1) ? get($1,true) : v } end return value end |
#inspect ⇒ Object
70 71 72 |
# File 'lib/dot_properties.rb', line 70 def inspect to_h.inspect end |
#normalize_delimiters! ⇒ Object
Replace all delimiters with default_delimiter
80 81 82 |
# 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) ⇒ Object
42 43 44 45 46 47 48 49 50 |
# 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! ⇒ Object
Strip all blank lines, leaving only comments and values
85 86 87 |
# File 'lib/dot_properties.rb', line 85 def strip_blanks! @content.reject! { |item| item[:type] == :blank } end |
#strip_comments! ⇒ Object
Strip all comments, leaving only blank lines and values
90 91 92 |
# File 'lib/dot_properties.rb', line 90 def strip_comments! @content.reject! { |item| item[:type] == :comment } end |
#to_a ⇒ Object
The assembled .properties file as an array of lines
95 96 97 |
# File 'lib/dot_properties.rb', line 95 def to_a @content.collect { |item| assemble(item) } end |
#to_h ⇒ Object
All properties as a hash
100 101 102 |
# 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 ⇒ Object
The assembled .properties file as a string
105 106 107 |
# File 'lib/dot_properties.rb', line 105 def to_s to_a.join("\n") end |