Class: Properties
- Inherits:
-
Object
- Object
- Properties
- Defined in:
- lib/xproperties.rb
Overview
Parse properties file.
Instance Method Summary collapse
-
#[](key) ⇒ String
Getting the value of a property.
-
#[]=(key, value) ⇒ NilClass
Setting the value of a property.
-
#clear ⇒ NilClass
Remove all properties.
-
#contains?(key) ⇒ Boolean
Returns a value whether the key exists.
-
#count ⇒ Integer
Get the number of properties.
-
#delete_property(key) ⇒ NilClass
Deleting the value of a property.
-
#each ⇒ Array
Get an iterator object.
-
#equals?(other) ⇒ Boolean
Returns a value whether two object instances are equal.
-
#get_property(key, default = "") ⇒ String
Getting the value of a property.
-
#initialize ⇒ Properties
constructor
A new instance of Properties.
-
#items ⇒ Array
Getting the list of properties key-value pair.
-
#keys ⇒ Array
Getting the list of properties name.
-
#load(file_name) ⇒ void
Loads a property list(key and element pairs) from the .properties file.
-
#load_from_xml(file_name) ⇒ NilClass
Loads a property list(key and element pairs) from the .xml file.
-
#save(file_name) ⇒ NilClass
Saves a property list(key and element pairs) to the .properties file.
-
#save_to_xml(file_name) ⇒ NilClass
Saves a property list(key and element pairs) from the .xml file.
-
#set_property(key, value) ⇒ NilClass
Setting the value of a property.
-
#to_s ⇒ String
Convert the current object to string.
-
#to_string ⇒ String
Convert the current object to string.
-
#values ⇒ Array
Getting the list of properties value.
Constructor Details
#initialize ⇒ Properties
Returns a new instance of Properties.
7 8 9 |
# File 'lib/xproperties.rb', line 7 def initialize @properties = { } end |
Instance Method Details
#[](key) ⇒ String
Getting the value of a property.
25 26 27 |
# File 'lib/xproperties.rb', line 25 def [](key) get_property key end |
#[]=(key, value) ⇒ NilClass
Setting the value of a property.
16 17 18 19 |
# File 'lib/xproperties.rb', line 16 def []=(key, value) set_property key, value nil? end |
#clear ⇒ NilClass
Remove all properties
240 241 242 |
# File 'lib/xproperties.rb', line 240 def clear @properties.clear end |
#contains?(key) ⇒ Boolean
Returns a value whether the key exists.
275 276 277 |
# File 'lib/xproperties.rb', line 275 def contains?(key) @properties.key? key end |
#count ⇒ Integer
Get the number of properties.
268 269 270 |
# File 'lib/xproperties.rb', line 268 def count @properties.count end |
#delete_property(key) ⇒ NilClass
Deleting the value of a property.
233 234 235 |
# File 'lib/xproperties.rb', line 233 def delete_property(key) @properties.delete key end |
#each ⇒ Array
Get an iterator object.
32 33 34 35 36 |
# File 'lib/xproperties.rb', line 32 def each items.each do |item| yield item end end |
#equals?(other) ⇒ Boolean
Returns a value whether two object instances are equal.
283 284 285 |
# File 'lib/xproperties.rb', line 283 def equals?(other) self == other end |
#get_property(key, default = "") ⇒ String
Getting the value of a property.
221 222 223 224 225 226 |
# File 'lib/xproperties.rb', line 221 def get_property(key, default = "") if @properties.key? key return @properties[key] end default end |
#items ⇒ Array
Getting the list of properties key-value pair.
261 262 263 |
# File 'lib/xproperties.rb', line 261 def items @properties.keys.zip @properties.values end |
#keys ⇒ Array
Getting the list of properties name.
247 248 249 |
# File 'lib/xproperties.rb', line 247 def keys @properties.keys end |
#load(file_name) ⇒ void
This method returns an undefined value.
Loads a property list(key and element pairs) from the .properties file. The file is assumed to use the ISO-8859-1(Latin1) character encoding.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/xproperties.rb', line 53 def load(file_name) temp = File.read file_name, :encoding => "iso-8859-1" temp = temp.gsub /^[ \t]*[#!].*[\r\n\f]+/, "" temp = temp.gsub /(?<!\\)\\[ \t]*[\r\n\f]+[ \t]*/, "" raw_data = temp.split /[\r\n\f]+/ raw_data.each do |i| pair = i.split /(?<!\\)[ \t]*(?<!\\)[=:][ \t]*/, 2 if pair[0] != nil and pair[0].strip != "" key = load_convert pair[0], :is_convert_key => true if pair.length == 2 value = load_convert pair[1] @properties[key] = value else @properties[key] = "" end end end nil? end |
#load_from_xml(file_name) ⇒ NilClass
Loads a property list(key and element pairs) from the .xml file.
The XML document will have the following DOCTYPE declaration: <!DOCTYPE properties SYSTEM “java.sun.com/dtd/properties.dtd”>
100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/xproperties.rb', line 100 def load_from_xml(file_name) raw = File.read file_name, :encoding => "utf-8" doc = REXML::Document.new raw pairs = doc.root.elements pairs.each "entry" do |pair| if pair.attributes.key? "key" key = pair.attributes["key"] value = pair.text @properties[key] = value else raise ParseException "Malformed XML format." end end end |
#save(file_name) ⇒ NilClass
Saves a property list(key and element pairs) to the .properties file. The file will be written in ISO-8859-1(Latin1) character encoding.
79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/xproperties.rb', line 79 def save(file_name) file = File.open file_name, "w", :encoding => "iso-8859-1" @properties.each_pair do |k, v| key = save_convert k, :is_convert_key => true value = save_convert v pair = key + "=" + value file.write pair + "\n" end file.close nil? end |
#save_to_xml(file_name) ⇒ NilClass
Saves a property list(key and element pairs) from the .xml file.
The XML document will have the following DOCTYPE declaration: <!DOCTYPE properties SYSTEM “java.sun.com/dtd/properties.dtd”>
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'lib/xproperties.rb', line 123 def save_to_xml(file_name) xml_declaration = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" doctype = "<!DOCTYPE properties SYSTEM " + "\"http://java.sun.com/dtd/properties.dtd\">\n" root = REXML::Element.new "properties" @properties.each do |k, v| entry = REXML::Element.new "entry" entry.add_attribute "key", k entry.text = v root.add_element entry end file = File.open file_name, "w", :encoding => "utf-8" file.write xml_declaration file.write doctype doc = REXML::Document.new doc.add_element root doc.write file, 2, :encoding => "utf-8" end |
#set_property(key, value) ⇒ NilClass
Setting the value of a property.
211 212 213 |
# File 'lib/xproperties.rb', line 211 def set_property(key, value) @properties[key] = value end |
#to_s ⇒ String
Convert the current object to string.
41 42 43 |
# File 'lib/xproperties.rb', line 41 def to_s to_string end |
#to_string ⇒ String
Convert the current object to string.
290 291 292 |
# File 'lib/xproperties.rb', line 290 def to_string JSON.dump @properties end |
#values ⇒ Array
Getting the list of properties value.
254 255 256 |
# File 'lib/xproperties.rb', line 254 def values @properties.values end |