Module: BuildingSync::XmlGetSet
- Included in:
- AllResourceTotal, AuditDate, BuildingSection, BuildingSystem, Contact, Facility, HVACSystem, LightingSystemType, Measure, Report, ResourceUse, Scenario, SpatialElement, TimeSeries, Utility
- Defined in:
- lib/buildingsync/helpers/xml_get_set.rb
Overview
Used for getting, setting, and creating XML snippets for BuildingSync classes with an @base_xml attribute
Instance Method Summary collapse
- #get_prefix ⇒ Object
- #xget_attribute_for_element(element_name, attribute) ⇒ Object
-
#xget_element(element_name) ⇒ REXML::Element?
returns the first element with the specified element name.
-
#xget_id ⇒ Object
Get the id attribute of the @base_xml.
-
#xget_idrefs(element_name) ⇒ Array<String>
Gets all of the IDref attributes of the element_name provided assumes there is a parent child containment downstream of the base_xml, where the parent is a plural version of the element_name provided.
-
#xget_linked_premises ⇒ Hash
Get the linked premises ids of the @base_xml element.
-
#xget_name ⇒ String
Get the name of the element, based on the type of element.
-
#xget_or_create(element_name) ⇒ Object
get or create a new element.
- #xget_plurals_text_value(element_name) ⇒ Object
- #xget_text(element_name) ⇒ Object
- #xget_text_as_bool(element_name) ⇒ Object
- #xget_text_as_date(element_name) ⇒ Object
- #xget_text_as_dt(element_name) ⇒ Object
- #xget_text_as_float(element_name) ⇒ Object
- #xget_text_as_integer(element_name) ⇒ Object
-
#xset_or_create(element_name, new_value, override = true) ⇒ REXML::Element
Sets the text for an element if it exists, or creates the element then sets the text if it doesn't exist.
-
#xset_text(element_name, new_value) ⇒ REXML::Element?
Only sets the text for an element if it exists.
Instance Method Details
#get_prefix ⇒ Object
47 48 49 50 51 52 53 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 47 def get_prefix if @ns == '' return '' else return "#{@ns}:" end end |
#xget_attribute_for_element(element_name, attribute) ⇒ Object
77 78 79 80 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 77 def xget_attribute_for_element(element_name, attribute) element = @base_xml.elements["./#{get_prefix}#{element_name}"] return help_get_attribute_value(element, attribute) end |
#xget_element(element_name) ⇒ REXML::Element?
returns the first element with the specified element name
86 87 88 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 86 def xget_element(element_name) return @base_xml.elements["./#{get_prefix}#{element_name}"] end |
#xget_id ⇒ Object
Get the id attribute of the @base_xml
57 58 59 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 57 def xget_id return help_get_attribute_value(@base_xml, 'ID') end |
#xget_idrefs(element_name) ⇒ Array<String>
Gets all of the IDref attributes of the element_name provided assumes there is a parent child containment downstream of the base_xml, where the parent is a plural version of the element_name provided
139 140 141 142 143 144 145 146 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 139 def xget_idrefs(element_name) id_elements = @base_xml.get_elements(".//#{get_prefix}#{element_name}s/#{get_prefix}#{element_name}") to_return = [] id_elements.each do |id| to_return << help_get_attribute_value(id, 'IDref') end return to_return end |
#xget_linked_premises ⇒ Hash
Get the linked premises ids of the @base_xml element
160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 160 def xget_linked_premises map = {} premises = @base_xml.get_elements(".//#{get_prefix}LinkedPremises").first premises&.elements&.each do |premise_type| map[premise_type.name] = [] idref_elements = premise_type.get_elements('.//*[@IDref]') idref_elements&.each do |element| map[premise_type.name] << element.attributes['IDref'] end end return map end |
#xget_name ⇒ String
Get the name of the element, based on the type of element
65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 65 def xget_name premises = ['Site', 'Building', 'Section', 'ThermalZone', 'Space'] if premises.include? @base_xml.name return xget_text('PremisesName') elsif @base_xml.name == 'Measure' m = @base_xml.elements[".//#{get_prefix}MeasureName"] return help_get_text_value(m) else return xget_text("#{@base_xml.name}Name") end end |
#xget_or_create(element_name) ⇒ Object
get or create a new element
93 94 95 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 93 def xget_or_create(element_name) return help_get_or_create(@base_xml, "#{get_prefix}#{element_name}") end |
#xget_plurals_text_value(element_name) ⇒ Object
148 149 150 151 152 153 154 155 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 148 def xget_plurals_text_value(element_name) plurals = @base_xml.get_elements(".//#{get_prefix}#{element_name}s/#{get_prefix}#{element_name}") to_return = [] plurals.each do |p| to_return << help_get_text_value(p) end return to_return end |
#xget_text(element_name) ⇒ Object
99 100 101 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 99 def xget_text(element_name) return help_get_text_value(@base_xml.elements["./#{get_prefix}#{element_name}"]) end |
#xget_text_as_bool(element_name) ⇒ Object
117 118 119 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 117 def xget_text_as_bool(element_name) return help_get_text_value_as_bool(@base_xml.elements["./#{get_prefix}#{element_name}"]) end |
#xget_text_as_date(element_name) ⇒ Object
123 124 125 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 123 def xget_text_as_date(element_name) return help_get_text_value_as_date(@base_xml.elements["./#{get_prefix}#{element_name}"]) end |
#xget_text_as_dt(element_name) ⇒ Object
129 130 131 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 129 def xget_text_as_dt(element_name) return help_get_text_value_as_datetime(@base_xml.elements["./#{get_prefix}#{element_name}"]) end |
#xget_text_as_float(element_name) ⇒ Object
105 106 107 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 105 def xget_text_as_float(element_name) return help_get_text_value_as_float(@base_xml.elements["./#{get_prefix}#{element_name}"]) end |
#xget_text_as_integer(element_name) ⇒ Object
111 112 113 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 111 def xget_text_as_integer(element_name) return help_get_text_value_as_integer(@base_xml.elements["./#{get_prefix}#{element_name}"]) end |
#xset_or_create(element_name, new_value, override = true) ⇒ REXML::Element
Sets the text for an element if it exists, or creates the element then sets the text if it doesn't exist
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 192 def xset_or_create(element_name, new_value, override = true) element = @base_xml.elements["./#{get_prefix}#{element_name}"] if !element.nil? # if there is no value, we set it if element.text.nil? || element.text.empty? element.text = new_value # if there is a value but we are overriding, we set it elsif override element.text = new_value end else element = REXML::Element.new("#{get_prefix}#{element_name}", @base_xml) element.text = new_value end if element.text.nil? raise StandardError, "Unable to set #{element_name} to nil" end if element.text.empty? raise StandardError, "Unable to set #{element_name} to be empty" end return element end |
#xset_text(element_name, new_value) ⇒ REXML::Element?
Only sets the text for an element if it exists
178 179 180 181 182 183 184 |
# File 'lib/buildingsync/helpers/xml_get_set.rb', line 178 def xset_text(element_name, new_value) element = @base_xml.elements["./#{get_prefix}#{element_name}"] if !element.nil? element.text = new_value end return element end |