Sha256: b22b5a92582e2756e913142ee3f4396cac76a23f5f41697c492fa89f90899591

Contents?: true

Size: 1.14 KB

Versions: 18

Compression:

Stored size: 1.14 KB

Contents

require 'twitter/base'
require 'twitter/error/identity_map_key_error'

module Twitter
  class Identity < Twitter::Base

    def self.fetch(attrs)
      return unless identity_map

      id = attrs[:id]
      if id  && object = identity_map.fetch(id)
        return object.update(attrs)
      end

      return yield if block_given?
      raise Twitter::Error::IdentityMapKeyError, "key not found"
    end

    # Stores an object in the identity map.
    #
    # @param object [Object]
    # @return [Twitter::Identity]
    def self.store(object)
      return object unless identity_map
      identity_map.store(object.id, object)
    end

    # Initializes a new object
    #
    # @param attrs [Hash]
    # @raise [ArgumentError] Error raised when supplied argument is missing an :id key.
    # @return [Twitter::Identity]
    def initialize(attrs={})
      super
      raise ArgumentError, "argument must have an :id key" unless id
    end

    # @param other [Twitter::Identity]
    # @return [Boolean]
    def ==(other)
      super || attr_equal(:id, other) || attrs_equal(other)
    end

    # @return [Integer]
    def id
      @attrs[:id]
    end

  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
twitter-4.8.1 lib/twitter/identity.rb
twitter-4.8.0 lib/twitter/identity.rb
twitter-4.7.0 lib/twitter/identity.rb
twitter-4.6.2 lib/twitter/identity.rb
twitter-4.6.1 lib/twitter/identity.rb
twitter-4.6.0 lib/twitter/identity.rb
twitter-4.5.0 lib/twitter/identity.rb
twitter-4.4.4 lib/twitter/identity.rb
twitter-4.4.3 lib/twitter/identity.rb
twitter-4.4.2 lib/twitter/identity.rb
twitter-4.4.1 lib/twitter/identity.rb
twitter-4.4.0 lib/twitter/identity.rb
twitter-4.3.0 lib/twitter/identity.rb
twitter-4.2.0 lib/twitter/identity.rb
twitter-4.1.2 lib/twitter/identity.rb
twitter-4.1.1 lib/twitter/identity.rb
twitter-4.1.0 lib/twitter/identity.rb
twitter-4.0.0 lib/twitter/identity.rb