Sha256: 3af2628704ad5b95eb57205ce9b7d5897c9f7d5186e50b4fa2fcfadacdccd9b4

Contents?: true

Size: 1.8 KB

Versions: 5

Compression:

Stored size: 1.8 KB

Contents

require 'rakuten_web_service/resource'

module RakutenWebService
  class BaseGenre < RakutenWebService::Resource
    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

    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

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

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

    def self.root
      self.new(root_id)
    end

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

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

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

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

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

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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rakuten_web_service-1.9.0 lib/rakuten_web_service/genre.rb
rakuten_web_service-1.8.0 lib/rakuten_web_service/genre.rb
rakuten_web_service-1.7.0 lib/rakuten_web_service/genre.rb
rakuten_web_service-1.6.1 lib/rakuten_web_service/genre.rb
rakuten_web_service-1.6.0 lib/rakuten_web_service/genre.rb