lib/zendesk_api/association.rb
# Represents a parent-to-child association between resources. Options to pass in are: class, path.
# @param [Symbol] resource_name The underlying resource name
# @param [Hash] opts The options to pass to the method definition.
Represents a parent-to-child association between resources. Options to pass in are: class, path.
Represents a parent - to - child association between resources . Options to pass in are : class path .
def has(resource_name, class_level_options = {})
        klass = get_class(class_level_options.delete(:class)) || get_class(resource_name)
        class_level_association = {
          :class => klass,
          :name => resource_name,
          :inline => class_level_options.delete(:inline),
          :path => class_level_options.delete(:path)
        }
        associations << class_level_association
        id_column = "#{resource_name}_id"
        define_method "#{resource_name}_used?" do
          !!instance_variable_get("@#{resource_name}")
        end
        define_method resource_name do |*args|
          instance_options = args.last.is_a?(Hash) ? args.pop : {}
          cached = instance_variable_get("@#{resource_name}")
          return cached if cached && !instance_options[:reload]
          instance_association = Association.new(class_level_association.merge(:parent => self))
          resource = if klass.respond_to?(:find) && resource_id = method_missing(id_column)
            klass.find(@client, :id => resource_id, :association => instance_association)
          elsif found = method_missing(resource_name.to_sym)
            wrap_resource(found, klass, class_level_association)
          elsif klass.ancestors.include?(DataResource)
            rescue_client_error do
              response = @client.connection.get(instance_association.generate_path(:with_parent => true))
              klass.new(@client, response.body[klass.singular_resource_name].merge(:association => instance_association))
            end
          end
          send("#{id_column}=", resource.id) if resource && has_key?(id_column)
          instance_variable_set("@#{resource_name}", resource)
        end
        define_method "#{resource_name}=" do |resource|
          resource = wrap_resource(resource, klass, class_level_association)
          send("#{id_column}=", resource.id) if has_key?(id_column)
          instance_variable_set("@#{resource_name}", resource)
        end
      end
def has ( resource_name , class_level_options = { } ) klass = get_class ( class_level_options . delete ( :class ) ) || get_class ( resource_name ) class_level_association = { :class => klass , :name => resource_name , :inline => class_level_options . delete ( :inline ) , :path => class_level_options . delete ( :path ) } associations << class_level_association id_column = "#{resource_name}_id" define_method "#{resource_name}_used?" do ! ! instance_variable_get ( "@#{resource_name}" ) end define_method resource_name do | * args | instance_options = args . last . is_a? ( Hash ) ? args . pop : { } cached = instance_variable_get ( "@#{resource_name}" ) return cached if cached && ! instance_options [ :reload ] instance_association = Association . new ( class_level_association . merge ( :parent => self ) ) resource = if klass . respond_to? ( :find ) && resource_id = method_missing ( id_column ) klass . find ( @client , :id => resource_id , :association => instance_association ) elsif found = method_missing ( resource_name . to_sym ) wrap_resource ( found , klass , class_level_association ) elsif klass . ancestors . include? ( DataResource ) rescue_client_error do response = @client . connection . get ( instance_association . generate_path ( :with_parent => true ) ) klass . new ( @client , response . body [ klass . singular_resource_name ] . merge ( :association => instance_association ) ) end end send ( "#{id_column}=" , resource . id ) if resource && has_key? ( id_column ) instance_variable_set ( "@#{resource_name}" , resource ) end define_method "#{resource_name}=" do | resource | resource = wrap_resource ( resource , klass , class_level_association ) send ( "#{id_column}=" , resource . id ) if has_key? ( id_column ) instance_variable_set ( "@#{resource_name}" , resource ) end end
Represents a parent-to-child association between resources.
Represents a parent - to - child association between resources .
# return if cached # find and cache association
resource_name
<not_specific>
class_level_options
<not_specific>
<not_specific>
opts The options to pass to the method definition.
opts The options to pass to the method definition .