Module: Zabbirc::Zabbix::Resource::Associations

Included in:
Base
Defined in:
lib/zabbirc/zabbix/resource/associations.rb

Instance Method Summary collapse

Instance Method Details

#has_many(name, *options) ⇒ Object



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.extract_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

#has_one(name) ⇒ Object



30
31
32
# File 'lib/zabbirc/zabbix/resource/associations.rb', line 30

def has_one name

end