Class: VersionFile
- Inherits:
-
Object
- Object
- VersionFile
- Defined in:
- lib/vamper/version_file.rb
Instance Attribute Summary collapse
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Instance Method Summary collapse
- #add_attribute(parent_node, attr_definitions) ⇒ Object
- #add_child_element(parent_element, element_definitions) ⇒ Object
- #add_child_list_element(parent_element, *element_symbols) ⇒ Object
- #files ⇒ Object
-
#initialize(io) ⇒ VersionFile
constructor
A new instance of VersionFile.
- #write_to(io) ⇒ Object
Constructor Details
#initialize(io) ⇒ VersionFile
Returns a new instance of VersionFile.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/vamper/version_file.rb', line 9 def initialize(io) if io @doc = Nokogiri::XML(io, &:noblanks) unless @doc.root.name == 'Version' raise BadVersionFile, 'Root element must be Version' end else @doc = Nokogiri::XML::Document.new @doc.root.add_child(Nokogiri::XML::Element.new('Version', @doc)) end add_attribute @doc.root, :BuildValueType => :JDate add_child_list_element @doc.root, :Files, :Tags add_child_element , :Major => 1, :Minor => 0, :Build => 0, :Patch => 0, :Revision => 0, :TimeZone => 'UTC' add_child_element , {:StartYear => TZInfo::Timezone.get(self.time_zone).now.year} end |
Instance Attribute Details
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
7 8 9 |
# File 'lib/vamper/version_file.rb', line 7 def @tags end |
Instance Method Details
#add_attribute(parent_node, attr_definitions) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/vamper/version_file.rb', line 28 def add_attribute(parent_node, attr_definitions) attr_definitions.each { |attr_symbol, attr_default| method_name = attr_symbol.to_s.underscore unless parent_node[attr_symbol] parent_node[attr_symbol] = attr_default end define_singleton_method(method_name.to_sym) { parent_node[attr_symbol].to_sym } define_singleton_method((method_name + '=').to_sym) { |value| parent_node[attr_symbol] = value.to_s } } end |
#add_child_element(parent_element, element_definitions) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/vamper/version_file.rb', line 58 def add_child_element(parent_element, element_definitions) element_definitions.each { |element_symbol, element_default| name = element_symbol.to_s elem = parent_element.at(name) unless elem elem = parent_element.add_child(Nokogiri::XML::Element.new(name, @doc)) elem.content = element_default.to_s end method_name = name.underscore case element_default when Fixnum define_singleton_method(method_name.to_sym) { Integer(elem.content) } when String define_singleton_method(method_name.to_sym) { elem.content.to_s } end define_singleton_method((method_name + '=').to_sym) { |value| elem.content = value.to_s } } end |
#add_child_list_element(parent_element, *element_symbols) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/vamper/version_file.rb', line 43 def add_child_list_element(parent_element, *element_symbols) element_symbols.each { |element_symbol| name = element_symbol.to_s elem = parent_element.at(name) unless elem elem = parent_element.add_child(Nokogiri::XML::Element.new(name, @doc)) end method_name = name.underscore + '_element' define_singleton_method(method_name.to_sym) { elem } } end |
#files ⇒ Object
91 92 93 |
# File 'lib/vamper/version_file.rb', line 91 def files files_element.children.select {|node| node.name != 'text'}.map {|node| node.content} end |
#write_to(io) ⇒ Object
84 85 86 |
# File 'lib/vamper/version_file.rb', line 84 def write_to(io) @doc.write_xml_to(io, :indent_text => ' ', :indent => 2) end |