Module: BuildingSync::XmlGetSet

Overview

Used for getting, setting, and creating XML snippets for BuildingSync classes with an @base_xml attribute

Instance Method Summary collapse

Instance Method Details

#get_prefixObject



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

Parameters:

  • element_name (String)

    non-namespaced element name, 'EnergyResource'

Returns:

  • (REXML::Element)

    if element exists

  • (nil)

    if element doesnt exist



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_idObject

Get the id attribute of the @base_xml

See Also:

  • help_get_attribute_value


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

Examples:

xget_idrefs('Measure') #=> Searches for .//Measures/Measure

Parameters:

  • element_name (String)

    name of the non-pluralized element, i.e. Measure

Returns:

  • (Array<String>)

    all associated IDs ['Measure-1', 'Measure-2', etc.]



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_premisesHash

Get the linked premises ids of the @base_xml element

Examples:

=> ['Building-1', 'Building-1'], 'Section' => ['Section-4']]

Returns:

  • (Hash)

    where keys are premise types and values are an array of ids



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_nameString

Get the name of the element, based on the type of element

Examples:

scenario.get_name, returns text for ScenarioName element

site.get_name, returns text for SiteName

Returns:

  • (String)

    name associated with the @base_xml



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

Parameters:

  • element_name (String)

    non-namespaced element name, 'EnergyResource'

See Also:

  • help_get_or_create


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

Parameters:

  • element_name (String)

    non-namespaced element name, 'EnergyResource'

See Also:

  • help_get_text_value


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

Parameters:

  • element_name (String)

    non-namespaced element name, 'BuildingAutomationSystem'

See Also:

  • help_get_text_value_as_bool


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

Parameters:

  • element_name (String)

    non-namespaced element name, 'RetrocommissioningDate'

See Also:

  • help_get_text_value_as_date


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

Parameters:

  • element_name (String)

    non-namespaced element name, 'StartTimestamp'

See Also:

  • help_get_text_value_as_datetime


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

Parameters:

  • element_name (String)

    non-namespaced element name, 'YearOfConstruction'

See Also:

  • help_get_text_value_as_float


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

Parameters:

  • element_name (String)

    non-namespaced element name, 'YearOfConstruction'

See Also:

  • help_get_text_value_as_integer


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

Parameters:

  • element_name (String)

    non-namespaced element name, 'EnergyResource'

  • new_value (String)

    new text value to use, 'Electricity'

  • override (Boolean) (defaults to: true)

    whether to override the text value if the element already has text

Returns:

  • (REXML::Element)

    the element



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

Parameters:

  • element_name (String)

    non-namespaced element name, 'EnergyResource'

  • new_value (String)

    new text value to use, 'Electricity'

Returns:

  • (REXML::Element)

    if element exists

  • (nil)

    if element doesn't exist



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