Class: Vodpod::Record

Inherits:
Object
  • Object
show all
Defined in:
lib/vodpod/record.rb

Overview

Represents a generic Vodpod API record.

Vodpod objects like Video, Pod, and Tag inherit from this class. It wraps a store (usually the deserialized JSON hash from an API call) with automatic accessors, so you can call video.title instead of video.store. Records are instantiated with a connection object and a default store of an empty hash.

Direct Known Subclasses

Collection, CollectionVideo, Comment, Tag, User, Video

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection, values = {}) ⇒ Record

Create a new Record. Takes two parameters: a Connection object so the record can perform further requests, and an optional default value for the value hash



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/vodpod/record.rb', line 39

def initialize(connection, values = {})
  @connection = connection
  @values = values

  self.class.casters.each do |name, cast|
    if values = @values[name.to_s]
      # Lazily load classes
      unless cast[:class].kind_of? Class
        cast[:class] = Vodpod.const_get(cast[:class])
      end

      # Convert sub-values to objects
      @values[name.to_s] = case cast[:type]
      when :one
        cast[:class].new(@connection, values)
      when :many
        values.map do |value|
          cast[:class].new(@connection, value)
        end
      when :parse
        cast[:class].parse(values)
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object

Pass requests to store by default.



84
85
86
87
88
# File 'lib/vodpod/record.rb', line 84

def method_missing(meth, *args)
  if @values.include? meth.to_s
    @values[meth.to_s]
  end
end

Instance Attribute Details

#valuesObject

Returns the value of attribute values.



10
11
12
# File 'lib/vodpod/record.rb', line 10

def values
  @values
end

Class Method Details

.cast(name, opts = {}) ⇒ Object

Association metaprogramming!



17
18
19
20
21
# File 'lib/vodpod/record.rb', line 17

def self.cast(name, opts = {})
  # Total hack :)
  opts[:class] ||= name.to_s.sub(/s$/, '').capitalize
  casters[name] = opts
end

.castersObject



12
13
14
# File 'lib/vodpod/record.rb', line 12

def self.casters
  @casters ||= {}
end

.date(name, opts = {}) ⇒ Object

The given attribute is casted to a DateTime



24
25
26
# File 'lib/vodpod/record.rb', line 24

def self.date(name, opts = {})
  cast name, opts.merge(:type => :parse, :class => DateTime)
end

.many(name, opts = {}) ⇒ Object



32
33
34
# File 'lib/vodpod/record.rb', line 32

def self.many(name, opts = {})
  cast name, opts.merge(:type => :many)
end

.one(name, opts = {}) ⇒ Object



28
29
30
# File 'lib/vodpod/record.rb', line 28

def self.one(name, opts = {})
  cast name, opts.merge(:type => :one)
end

Instance Method Details

#==(other) ⇒ Object



65
66
67
# File 'lib/vodpod/record.rb', line 65

def ==(other)
  self.class == other.class and self.key == other.key rescue false
end

#inspectObject



69
70
71
# File 'lib/vodpod/record.rb', line 69

def inspect
  "#<#{self.class} #{key} #{@values.inspect}>"
end

#to_sObject



73
74
75
76
77
78
79
80
81
# File 'lib/vodpod/record.rb', line 73

def to_s
  if respond_to? :title
    title.to_s
  elsif respond_to? :name
    name.to_s
  else
    key.to_s
  end
end