Class: Properties
- Inherits:
-
Object
- Object
- Properties
- Defined in:
- lib/xproperties/properties.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.
513 514 515 |
# File 'lib/xproperties/properties.rb', line 513 def initialize @properties = { } end |
Instance Method Details
#[](key) ⇒ String
Getting the value of a property.
531 532 533 |
# File 'lib/xproperties/properties.rb', line 531 def [](key) get_property key end |
#[]=(key, value) ⇒ NilClass
Setting the value of a property.
522 523 524 525 |
# File 'lib/xproperties/properties.rb', line 522 def []=(key, value) set_property key, value nil? end |
#clear ⇒ NilClass
Remove all properties
750 751 752 |
# File 'lib/xproperties/properties.rb', line 750 def clear @properties.clear end |
#contains?(key) ⇒ Boolean
Returns a value whether the key exists.
785 786 787 |
# File 'lib/xproperties/properties.rb', line 785 def contains?(key) @properties.key? key end |
#count ⇒ Integer
Get the number of properties.
778 779 780 |
# File 'lib/xproperties/properties.rb', line 778 def count @properties.count end |
#delete_property(key) ⇒ NilClass
Deleting the value of a property.
743 744 745 |
# File 'lib/xproperties/properties.rb', line 743 def delete_property(key) @properties.delete key end |
#each ⇒ Array
Get an iterator object.
538 539 540 541 542 |
# File 'lib/xproperties/properties.rb', line 538 def each items.each do |item| yield item end end |
#equals?(other) ⇒ Boolean
Returns a value whether two object instances are equal.
793 794 795 |
# File 'lib/xproperties/properties.rb', line 793 def equals?(other) self == other end |
#get_property(key, default = "") ⇒ String
Getting the value of a property.
731 732 733 734 735 736 |
# File 'lib/xproperties/properties.rb', line 731 def get_property(key, default = "") if not @properties.key? key and default != "" return default end @properties[key] end |
#items ⇒ Array
Getting the list of properties key-value pair.
771 772 773 |
# File 'lib/xproperties/properties.rb', line 771 def items @properties.keys.zip @properties.values end |
#keys ⇒ Array
Getting the list of properties name.
757 758 759 |
# File 'lib/xproperties/properties.rb', line 757 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.
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 |
# File 'lib/xproperties/properties.rb', line 559 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”>
606 607 608 609 610 611 612 613 614 615 616 617 618 619 |
# File 'lib/xproperties/properties.rb', line 606 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.
585 586 587 588 589 590 591 592 593 594 595 |
# File 'lib/xproperties/properties.rb', line 585 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”>
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
# File 'lib/xproperties/properties.rb', line 629 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.
721 722 723 |
# File 'lib/xproperties/properties.rb', line 721 def set_property(key, value) @properties[key] = value end |
#to_s ⇒ String
Convert the current object to string.
547 548 549 |
# File 'lib/xproperties/properties.rb', line 547 def to_s to_string end |
#to_string ⇒ String
Convert the current object to string.
800 801 802 |
# File 'lib/xproperties/properties.rb', line 800 def to_string JSON.dump @properties end |
#values ⇒ Array
Getting the list of properties value.
764 765 766 |
# File 'lib/xproperties/properties.rb', line 764 def values @properties.values end |