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

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

Constructor Details

#initialize(params) ⇒ BaseGenre

Returns a new instance of BaseGenre.



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

def initialize(params)
  super
  self.class[self.id.to_s] = @params.reject { |k, v| k == 'itemCount' }
end

Class Method Details

.[](id) ⇒ Object



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

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

.[]=(id, genre) ⇒ Object



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

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

.genre_id_keyObject



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

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
30
31
32
33
# File 'lib/rakuten_web_service/genre.rb', line 22

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

.repositoryObject



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

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

.rootObject



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

def self.root
  self.new(root_id)
end

.root_id(id = nil) ⇒ Object



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

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

Instance Method Details

#childrenObject



64
65
66
# File 'lib/rakuten_web_service/genre.rb', line 64

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

#parentsObject



68
69
70
# File 'lib/rakuten_web_service/genre.rb', line 68

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