Module: TheTvDB::Model

Included in:
Episode, Series, Series::Collection
Defined in:
lib/the_tv_db/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#klassObject

Returns the value of attribute klass.



4
5
6
# File 'lib/the_tv_db/model.rb', line 4

def klass
  @klass
end

Instance Method Details

#attribute_for_inspect(attr_name) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/the_tv_db/model.rb', line 42

def attribute_for_inspect(attr_name)
  value = send(attr_name)

  if value.is_a?(String) && value.length > 50
    "#{value[0..50]}...".inspect
  elsif value.is_a?(Date) || value.is_a?(Time)
    %("#{value.to_s(:db)}")
  else
    value.inspect
  end
end

#attributesObject



23
24
25
26
27
28
29
# File 'lib/the_tv_db/model.rb', line 23

def attributes
  attrs = {}
  class_attributes.each do |name|
    attrs[name] = send(name)
  end
  attrs
end

#attributes=(params = nil) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'lib/the_tv_db/model.rb', line 13

def attributes=(params=nil)
  params.each do |attr, value|
    begin
      self.public_send("#{attributes_map.key(attr)}=", value)
    rescue NoMethodError
      raise UnknownAttributeError, "unknown attribute: #{attr}"
    end
  end if params
end

#attributes_mapObject



54
55
56
# File 'lib/the_tv_db/model.rb', line 54

def attributes_map
  klass::ATTRS_MAP
end

#class_attributesObject



58
59
60
# File 'lib/the_tv_db/model.rb', line 58

def class_attributes
  attributes_map.keys
end

#initialize(params = nil, &block) ⇒ Object



6
7
8
9
10
11
# File 'lib/the_tv_db/model.rb', line 6

def initialize(params=nil, &block)
  @klass = self.class
  self.attributes = params
  
  self.instance_eval(&block) if block_given?
end

#inspectObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/the_tv_db/model.rb', line 31

def inspect
  inspection = unless id.nil?
    class_attributes.collect { |name|
      "#{name}: #{attribute_for_inspect(name)}"
    }.compact.join(", ")
   else
     "not initialized"
   end
  "#<#{self.class} #{inspection}>"
end