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.



57
58
59
60
# File 'lib/rakuten_web_service/genre.rb', line 57

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

Class Method Details

.[](id) ⇒ Object



45
46
47
# File 'lib/rakuten_web_service/genre.rb', line 45

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

.[]=(id, genre) ⇒ Object



49
50
51
# File 'lib/rakuten_web_service/genre.rb', line 49

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

.genre_id_keyObject



33
34
35
# File 'lib/rakuten_web_service/genre.rb', line 33

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
# File 'lib/rakuten_web_service/genre.rb', line 5

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

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

.new(params) ⇒ Object



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

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

.repositoryObject



53
54
55
# File 'lib/rakuten_web_service/genre.rb', line 53

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

.rootObject



41
42
43
# File 'lib/rakuten_web_service/genre.rb', line 41

def self.root
  self.new(root_id)
end

.root_id(id = nil) ⇒ Object



37
38
39
# File 'lib/rakuten_web_service/genre.rb', line 37

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

Instance Method Details

#brothersObject



66
67
68
# File 'lib/rakuten_web_service/genre.rb', line 66

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

#childrenObject



62
63
64
# File 'lib/rakuten_web_service/genre.rb', line 62

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

#parentsObject



70
71
72
# File 'lib/rakuten_web_service/genre.rb', line 70

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