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

#extrasObject

Returns the value of attribute extras.



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

def extras
  @extras
end

#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



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

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



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

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

#attributes=(params = nil) ⇒ Object



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

def attributes=(params=nil)
  params.each do |attr, value|
    begin
      self.public_send("#{attributes_map.key(attr)}=", value)
    rescue NoMethodError
      extras[attr] = value
    end
  end if params
end

#attributes_mapObject



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

def attributes_map
  klass::ATTRS_MAP
end

#class_attributesObject



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

def class_attributes
  attributes_map.keys
end

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



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

def initialize(params=nil, &block)
  @klass = self.class
  @extras = {}
  self.attributes = params

  self.instance_eval(&block) if block_given?
end

#inspectObject



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

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