Class: Twitter::Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/twitter/factory.rb

Overview

Factory for creating Twitter objects based on type

Direct Known Subclasses

GeoFactory, MediaFactory

Class Method Summary collapse

Class Method Details

.new(method, klass, attrs = {}) ⇒ Twitter::Base

Constructs a new object based on type

Examples:

Twitter::Factory.new(:type, Twitter::Geo, attrs)

Parameters:

  • method (Symbol)

    The key to look up the type

  • klass (Class)

    The base class for the type

  • attrs (Hash) (defaults to: {})

    The attributes hash

Returns:

Raises:

  • (IndexError)

    Error raised when argument is missing a key



15
16
17
18
19
# File 'lib/twitter/factory.rb', line 15

def new(method, klass, attrs = {})
  type = attrs.fetch(method.to_sym)
  const_name = type.split("_").collect(&:capitalize).join
  klass.const_get(const_name.to_sym).new(attrs)
end