5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
# File 'lib/zabbirc/zabbix/resource/associations.rb', line 5
def has_many name, *options
options = options..reverse_merge({
zabbix_attribute: name,
class_name: name.to_s.singularize.camelize
})
extend_key = :"select#{options[:zabbix_attribute].to_s.camelize.pluralize || name.to_s.camelize}"
define_method name do
@associations ||= ActiveSupport::HashWithIndifferentAccess.new
@associations[name] ||= begin
assoc_class = Zabbix.const_get(options[:class_name])
hash_data = @attrs[options[:zabbix_attribute]]
if hash_data.blank?
this = self.class.find id, extend_key => :extend
raise StandardError, "zabbix response does not contain #{options[:zabbix_attribute]}" if this[options[:zabbix_attribute]].blank?
hash_data = this[options[:zabbix_attribute]]
end
hash_data.collect do |obj|
assoc_class.new obj
end
end
end
end
|