Class: Athenaeum::Item

Inherits:
Object
  • Object
show all
Defined in:
lib/athenaeum/item.rb

Overview

Athenaeum::Item

Synopsis

Represents an item from your collection. This is the semi-abstract superclass from which the actual object types descend.

Authors

Copyright © 2007 LAIKA, Inc

Version

$Id: item.rb 290 2007-08-02 18:26:29Z bbleything $

Direct Known Subclasses

Book, Game, Movie, Music

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item) ⇒ Item

the initializer is not used directly, rather it is inherited by the subclasses and used in them.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/athenaeum/item.rb', line 29

def initialize( item )
  item.attributes.keys.each do |name|
    # add getter
    (class << self ; self ; end).instance_eval {
      attr_reader name.to_s.intern
    }
    
    # set ivar.  Use raw_attributes because that's the only way to
    # get the un-unescaped content.
    instance_variable_set "@#{name}", item.raw_attributes[ name ]
  end
  
  @@registry ||= []
  @@registry << self
end

Class Method Details

.create(item) ⇒ Object

this is the initializer that actually gets used. Pass the appropriate kind of Hpricot::Elem object (representing an item in the xml) and it will give you the appropriate type of item back.



48
49
50
51
52
53
# File 'lib/athenaeum/item.rb', line 48

def self::create( item )
  raise "Must provide an Hpricot::Elem object" unless
    item.is_a? Hpricot::Elem
  
  Athenaeum.const_get( item.etag.name.capitalize ).new( item )
end

.find_by_type(type) ⇒ Object

searches through the registry, finding all items of a given subclass



90
91
92
# File 'lib/athenaeum/item.rb', line 90

def self::find_by_type( type )
  return @@registry.select {|item| item.is_a? type }
end

.find_by_uuid(uuid) ⇒ Object

searches through the registry, returning the item with the uuid specified



96
97
98
# File 'lib/athenaeum/item.rb', line 96

def self::find_by_uuid( uuid )
  return @@registry.detect {|item| item.uuid == uuid }
end

.inherited(klass) ⇒ Object

keep a registry of subclasses, for self::types, below



60
61
62
63
# File 'lib/athenaeum/item.rb', line 60

def self::inherited( klass )
  @@subclasses ||= []
  @@subclasses <<  klass
end

.registryObject

simple getter for the item registry



70
71
72
73
# File 'lib/athenaeum/item.rb', line 70

def self::registry
  @@registry ||= []
  return @@registry
end

.type_nameObject

the name of this particular type… book, music, game, movie, etc



76
77
78
# File 'lib/athenaeum/item.rb', line 76

def self::type_name
  self.to_s.split( /::/ ).last
end

.typesObject

simple getter for the subclass registry



81
82
83
# File 'lib/athenaeum/item.rb', line 81

def self::types
  @@subclasses || []
end

Instance Method Details

#to_htmlObject

returns a decent HTML representation of the item



105
106
107
# File 'lib/athenaeum/item.rb', line 105

def to_html
  return "<span class='title'>#{self.title}</span> <span class='creator'>by #{self.creator}</span>"
end