Module: RSMP::Components
Overview
Things shared between sites and site proxies
Instance Attribute Summary collapse
-
#components ⇒ Object
readonly
Returns the value of attribute components.
-
#main ⇒ Object
readonly
Returns the value of attribute main.
Instance Method Summary collapse
- #add_component(component) ⇒ Object
- #aggregated_status_changed(component, options = {}) ⇒ Object
- #check_main_component(settings) ⇒ Object
- #clear_alarm_timestamps ⇒ Object
- #component_list ⇒ Object
- #find_component(component_id, build: true) ⇒ Object
- #infer_component_type(component_id) ⇒ Object
- #initialize_components ⇒ Object
- #natural_sort_key(value) ⇒ Object
- #setup_components(settings) ⇒ Object
Instance Attribute Details
#components ⇒ Object (readonly)
Returns the value of attribute components.
4 5 6 |
# File 'lib/rsmp/component/components.rb', line 4 def components @components end |
#main ⇒ Object (readonly)
Returns the value of attribute main.
4 5 6 |
# File 'lib/rsmp/component/components.rb', line 4 def main @main end |
Instance Method Details
#add_component(component) ⇒ Object
40 41 42 |
# File 'lib/rsmp/component/components.rb', line 40 def add_component(component) @components[component.c_id] = component end |
#aggregated_status_changed(component, options = {}) ⇒ Object
11 |
# File 'lib/rsmp/component/components.rb', line 11 def aggregated_status_changed(component, = {}); end |
#check_main_component(settings) ⇒ Object
33 34 35 36 37 38 |
# File 'lib/rsmp/component/components.rb', line 33 def check_main_component(settings) raise ConfigurationError, 'main component must be defined' unless settings['main'] && settings['main'].size >= 1 return unless settings['main'].size > 1 raise ConfigurationError, "only one main component can be defined, found #{settings['main'].keys.join(', ')}" end |
#clear_alarm_timestamps ⇒ Object
79 80 81 |
# File 'lib/rsmp/component/components.rb', line 79 def @components.each_value(&:clear_alarm_timestamps) end |
#component_list ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/rsmp/component/components.rb', line 50 def component_list @components.values.sort_by { |component| natural_sort_key(component.c_id) }.map do |component| { 'id' => component.c_id, 'type' => component.component_type || 'component', 'name' => component.name || component.c_id } end end |
#find_component(component_id, build: true) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/rsmp/component/components.rb', line 64 def find_component(component_id, build: true) component = @components[component_id] return component if component return unless build inferred_type = infer_component_type component_id component = inferred_type.new node: self, id: component_id @components[component_id] = component class_name = component.class.name.split('::').last class_name << ' component' unless %w[Component ComponentProxy].include?(class_name) log "Added component #{component_id} with the inferred type #{class_name}", level: :debug component end |
#infer_component_type(component_id) ⇒ Object
60 61 62 |
# File 'lib/rsmp/component/components.rb', line 60 def infer_component_type(component_id) raise UnknownComponent, "Component #{component_id} mising and cannot infer type" end |
#initialize_components ⇒ Object
6 7 8 9 |
# File 'lib/rsmp/component/components.rb', line 6 def initialize_components @components = {} @main = nil end |
#natural_sort_key(value) ⇒ Object
44 45 46 47 48 |
# File 'lib/rsmp/component/components.rb', line 44 def natural_sort_key(value) value.to_s.split(/(\d+)/).map do |part| part.match?(/\A\d+\z/) ? part.to_i : part end end |
#setup_components(settings) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/rsmp/component/components.rb', line 13 def setup_components(settings) return unless settings check_main_component settings settings.each_pair do |type, components_by_type| next unless components_by_type components_by_type.each_pair do |id, component_settings| component_settings ||= {} component = build_component(id: id, type: type, settings: component_settings) component.( type: component_settings['type'] || type, name: component_settings['name'] || id ) @components[id] = component @main = @components[id] if type == 'main' end end end |