lib/zendesk_api/association.rb
# Represents a parent-to-children association between resources. Options to pass in are: class, path.
# @param [Symbol] resource The underlying resource name
# @param [Hash] opts The options to pass to the method definition.
Represents a parent-to-children association between resources. Options to pass in are: class, path.
Represents a parent - to - children association between resources . Options to pass in are : class path .
def has_many(resource_name, class_level_opts = {})
        klass = get_class(class_level_opts.delete(:class)) || get_class(resource_name.to_s.singular)
        class_level_association = {
          :class => klass,
          :name => resource_name,
          :inline => class_level_opts.delete(:inline),
          :path => class_level_opts.delete(:path)
        }
        associations << class_level_association
        id_column = "#{resource_name}_ids"
        define_method "#{resource_name}_used?" do
          !!instance_variable_get("@#{resource_name}")
        end
        define_method resource_name do |*args|
          instance_opts = args.last.is_a?(Hash) ? args.pop : {}
          cached = instance_variable_get("@#{resource_name}")
          return cached if cached && !instance_opts[:reload]
          instance_association = Association.new(class_level_association.merge(:parent => self))
          singular_resource_name = resource_name.to_s.singular
          resources = if (ids = method_missing("#{singular_resource_name}_ids")) && ids.any?
            ids.map do |id|
              klass.find(@client, :id => id, :association => instance_association)
            end.compact
          elsif (resources = method_missing(resource_name.to_sym)) && resources.any?
            resources.map do |res|
              klass.new(@client, res.merge(:association => instance_association))
            end
          else
            ZendeskAPI::Collection.new(@client, klass, instance_opts.merge(:association => instance_association))
          end
          send("#{id_column}=", resources.map(&:id)) if resource && has_key?(id_column)
          instance_variable_set("@#{resource_name}", resources)
        end
        define_method "#{resource_name}=" do |resources|
          if resources.is_a?(Array)
            resources.map! { |attr| wrap_resource(attr, klass, class_level_association) }
            send(resource_name).replace(resources)
          else
            resources.association = instance_association
            instance_variable_set("@#{resource_name}", resources)
          end
          send("#{id_column}=", resources.map(&:id)) if resources && has_key?(id_column)
          resource
        end
      end
def has_many ( resource_name , class_level_opts = { } ) klass = get_class ( class_level_opts . delete ( :class ) ) || get_class ( resource_name . to_s . singular ) class_level_association = { :class => klass , :name => resource_name , :inline => class_level_opts . delete ( :inline ) , :path => class_level_opts . delete ( :path ) } associations << class_level_association id_column = "#{resource_name}_ids" define_method "#{resource_name}_used?" do ! ! instance_variable_get ( "@#{resource_name}" ) end define_method resource_name do | * args | instance_opts = args . last . is_a? ( Hash ) ? args . pop : { } cached = instance_variable_get ( "@#{resource_name}" ) return cached if cached && ! instance_opts [ :reload ] instance_association = Association . new ( class_level_association . merge ( :parent => self ) ) singular_resource_name = resource_name . to_s . singular resources = if ( ids = method_missing ( "#{singular_resource_name}_ids" ) ) && ids . any? ids . map do | id | klass . find ( @client , :id => id , :association => instance_association ) end . compact elsif ( resources = method_missing ( resource_name . to_sym ) ) && resources . any? resources . map do | res | klass . new ( @client , res . merge ( :association => instance_association ) ) end else ZendeskAPI :: Collection . new ( @client , klass , instance_opts . merge ( :association => instance_association ) ) end send ( "#{id_column}=" , resources . map ( & :id ) ) if resource && has_key? ( id_column ) instance_variable_set ( "@#{resource_name}" , resources ) end define_method "#{resource_name}=" do | resources | if resources . is_a? ( Array ) resources . map! { | attr | wrap_resource ( attr , klass , class_level_association ) } send ( resource_name ) . replace ( resources ) else resources . association = instance_association instance_variable_set ( "@#{resource_name}" , resources ) end send ( "#{id_column}=" , resources . map ( & :id ) ) if resources && has_key? ( id_column ) resource end end
Represents a parent-to-children association between resources.
Represents a parent - to - children association between resources .
# return if cached # find and cache association
resource_name
<not_specific>
class_level_opts
<not_specific>
<not_specific>
opts The options to pass to the method definition.
opts The options to pass to the method definition .