Class: RakutenWebService::BaseGenre

Inherits:
Resource
  • Object
show all
Defined in:
lib/rakuten_web_service/genre.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Resource

#[], attribute, client, endpoint, #get_attribute, resource_name, search, set_parser, set_resource_name

Constructor Details

#initialize(params) ⇒ BaseGenre

Returns a new instance of BaseGenre.



55
56
57
58
# File 'lib/rakuten_web_service/genre.rb', line 55

def initialize(params)
  super
  self.class[self.id.to_s] = self
end

Class Method Details

.[](id) ⇒ Object



43
44
45
# File 'lib/rakuten_web_service/genre.rb', line 43

def self.[](id)
  repository[id.to_s]
end

.[]=(id, genre) ⇒ Object



47
48
49
# File 'lib/rakuten_web_service/genre.rb', line 47

def self.[]=(id, genre)
  repository[id.to_s] = genre
end

.genre_id_keyObject



31
32
33
# File 'lib/rakuten_web_service/genre.rb', line 31

def self.genre_id_key
  :"#{resource_name}_id"
end

.inherited(klass) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'lib/rakuten_web_service/genre.rb', line 5

def self.inherited(klass)
  klass.set_parser do |response|
    current = response['current']
    if children = response['children']
      children = children.map { |child| klass.new(child['child']) }
      current.merge!('children' => children)
    end
    if parents = response['parents']
      parents = parents.map { |parent| klass.new(parent['parent']) }
      current.merge!('parents' => parents)
    end

    genre = klass.new(current)
    [genre]
  end
end

.new(params) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/rakuten_web_service/genre.rb', line 22

def self.new(params)
  case params
  when Integer, String
    self[params.to_s] || search(genre_id_key => params.to_s).first
  when Hash
    super
  end
end

.repositoryObject



51
52
53
# File 'lib/rakuten_web_service/genre.rb', line 51

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

.rootObject



39
40
41
# File 'lib/rakuten_web_service/genre.rb', line 39

def self.root
  self.new(root_id)
end

.root_id(id = nil) ⇒ Object



35
36
37
# File 'lib/rakuten_web_service/genre.rb', line 35

def self.root_id(id=nil)
  @root_id = id || @root_id
end

Instance Method Details

#childrenObject



60
61
62
# File 'lib/rakuten_web_service/genre.rb', line 60

def children
  @params['children'] ||= self.class.search(self.class.genre_id_key => self.id).first.children
end