Class: DataStruct

Inherits:
Object
  • Object
show all
Defined in:
lib/rrd-grapher/notifier/data_struct.rb

Direct Known Subclasses

RRDNotifier::Packet

Class Method Summary collapse

Instance Method Summary collapse

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

.attributesObject



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.

Parameters:

  • opts_or_obj (Object, Hash) (defaults to: {})

    Source

  • only_fields (Array) (defaults to: nil)

    an array of symbol specifying which fields to copy

  • allow_nil (Boolean) (defaults to: false)

    If false nil values from the source will not be copied in object



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

Returns:

  • (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