Sha256: b3f832b8a78a7b6e8781b8579523ac8d0d3dc907a3056edba0c7f54a6ec9a81d

Contents?: true

Size: 2 KB

Versions: 1

Compression:

Stored size: 2 KB

Contents

require 'set'

module OAuthActiveResource
  class UniqueArray < Array
    def initialize(*args)
      if args.size == 1 and args[0].is_a? Array then
        super(args[0].uniq)
      else
        super(*args)
      end
    end

    def insert(i, v)
      super(i, v) unless include?(v)
    end

    def <<(v)
      super(v) unless include?(v)
    end

    def []=(*args)
      # note: could just call super(*args) then uniq!, but this is faster

      # there are three different versions of this call:
      # 1. start, length, value
      # 2. index, value
      # 3. range, value
      # We just need to get the value
      v = case args.size
        when 3 then args[2]
        when 2 then args[1]
        else nil
      end

      super(*args) if v.nil? or not include?(v)
    end
  end
  
  # see has_many in Resource
  class UniqueResourceArray < UniqueArray
    def initialize(connection, resource,  collection_path)   
      super()   
      
      @connection = connection
      @collection_path = collection_path
      @resource = resource
      reload
    end
   
    def to_json
      return "[ #{self.map { |obj| obj.to_json }.join(',')} ]"
    end
    
    def to_xml
      # or use __method__ here?
      pt = @resource.element_name.pluralize
      return "<#{pt}> #{self.map { |obj| obj.to_xml({:skip_instruct => true})}.join(' ')} </#{pt}>"
    end
    
    
    # DEPRECATED...
    #def find(look_for)
    #  if not (look_for.is_a? String or look_for.is_a? Integer)
    #    look_for_id = look_for
    #  else      
    #    look_for_id = look_for.id
    #  end  
    #  
    #  self.each do |obj|
    #      obj.id == look_for_id and return obj 
    #  end
    #  return nil
    #end
    
    def save
      response = @connection.handle_response( @connection.put("#{@collection_path}",self.to_xml) )
      self.replace( @resource.load_collection( @connection.format.decode(response.body) ) )
    end
    
    def reload
      self.replace(@resource.find(:all, :from => @collection_path))
      return self
    end
  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jwagener-oauth-active-resource-0.1.5 lib/oauth_active_resource/unique_resource_array.rb