Class: DataStruct
- Inherits:
-
Object
- Object
- DataStruct
- Defined in:
- lib/rrd-grapher/notifier/data_struct.rb
Direct Known Subclasses
Class Method Summary collapse
- .attributes ⇒ Object
- .properties(*names) ⇒ Object (also: property)
Instance Method Summary collapse
-
#initialize(*args) ⇒ DataStruct
constructor
A new instance of DataStruct.
-
#merge_data_from(opts_or_obj = {}, only_fields = nil, allow_nil = false) ⇒ Object
Merge new data in the structure.
- #selected_field?(field, list) ⇒ Boolean
Constructor Details
#initialize(*args) ⇒ DataStruct
Returns a new instance of DataStruct.
4 5 6 |
# File 'lib/rrd-grapher/notifier/data_struct.rb', line 4 def initialize(*args) merge_data_from(*args) end |
Class Method Details
.attributes ⇒ Object
41 42 43 |
# File 'lib/rrd-grapher/notifier/data_struct.rb', line 41 def attributes @attributes end |
.properties(*names) ⇒ Object Also known as: property
32 33 34 35 36 37 |
# File 'lib/rrd-grapher/notifier/data_struct.rb', line 32 def properties(*names) names.each do |name| attr_accessor(name) (@attributes ||= []) << name end end |
Instance Method Details
#merge_data_from(opts_or_obj = {}, only_fields = nil, allow_nil = false) ⇒ Object
Merge new data in the structure.
17 18 19 20 21 22 23 24 |
# File 'lib/rrd-grapher/notifier/data_struct.rb', line 17 def merge_data_from(opts_or_obj = {}, only_fields = nil, allow_nil = false) self.class.attributes.select{|attr_name| selected_field?(attr_name, only_fields) }.each do |attr_name| v = opts_or_obj.is_a?(Hash) ? (opts_or_obj[attr_name.to_s] || opts_or_obj[attr_name]) : opts_or_obj.send(attr_name) if allow_nil || !v.nil? send("#{attr_name}=", v) end end end |
#selected_field?(field, list) ⇒ Boolean
26 27 28 |
# File 'lib/rrd-grapher/notifier/data_struct.rb', line 26 def selected_field?(field, list) list.nil? || list.include?(field.to_sym) end |