Class: MonitoringProtocols::DataStruct
- Inherits:
-
Object
- Object
- MonitoringProtocols::DataStruct
show all
- Includes:
- Comparable
- Defined in:
- lib/monitoring_protocols/data_struct.rb
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(*args) ⇒ DataStruct
Returns a new instance of DataStruct.
15
16
17
|
# File 'lib/monitoring_protocols/data_struct.rb', line 15
def initialize(*args)
merge_data_from!(*args)
end
|
Class Method Details
.attributes ⇒ Object
88
89
90
91
92
93
94
|
# File 'lib/monitoring_protocols/data_struct.rb', line 88
def attributes
if (superclass <= DataStruct) && (superclass.attributes)
superclass.attributes + @attributes
else
@attributes
end
end
|
.properties(*names) ⇒ Object
Also known as:
property
79
80
81
82
83
84
|
# File 'lib/monitoring_protocols/data_struct.rb', line 79
def properties(*names)
names.each do |name|
attr_accessor(name)
(@attributes ||= []) << name
end
end
|
Instance Method Details
#<=>(other) ⇒ Object
73
74
75
|
# File 'lib/monitoring_protocols/data_struct.rb', line 73
def <=>(other)
self.to_a <=> other.to_a
end
|
#list_keys(what) ⇒ Object
45
46
47
48
49
50
51
|
# File 'lib/monitoring_protocols/data_struct.rb', line 45
def list_keys(what)
if what.respond_to?(:keys)
what.keys.clone
else
[]
end
end
|
#merge_data_from!(opts_or_obj = {}, only_fields = nil, allow_nil = false) ⇒ Object
Merge new data in the structure.
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# File 'lib/monitoring_protocols/data_struct.rb', line 28
def merge_data_from!(opts_or_obj = {}, only_fields = nil, allow_nil = false)
keys_left = list_keys(opts_or_obj)
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
keys_left.delete(attr_name)
end
unless keys_left.empty?
raise ArgumentError, "unknown keys: #{keys_left}"
end
end
|
#selected_field?(field, list) ⇒ Boolean
53
54
55
|
# File 'lib/monitoring_protocols/data_struct.rb', line 53
def selected_field?(field, list)
list.nil? || list.include?(field.to_sym)
end
|
#to_a ⇒ Object
65
66
67
|
# File 'lib/monitoring_protocols/data_struct.rb', line 65
def to_a
self.class.attributes.map{|attr_name| send(attr_name) }
end
|
#to_h ⇒ Object
57
58
59
60
61
62
63
|
# File 'lib/monitoring_protocols/data_struct.rb', line 57
def to_h
h = {}
self.class.attributes.each do |attr_name|
h[attr_name] = send(attr_name)
end
h
end
|
#to_msgpack(pack) ⇒ Object
69
70
71
|
# File 'lib/monitoring_protocols/data_struct.rb', line 69
def to_msgpack(pack)
pack.write(to_h)
end
|