Sha256: e0e2f3ffcf140fac72c20b53c62f2aa238bc6084eaaeb0a1b2a2f1236e0ad249

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require_relative 'where_filter'

module Moysklad::Resources
  class Indexed  < SimpleDelegator
    include WhereFilter

    def initialize resource
      raise 'resource должен быть Moysklad::Resources::Base' unless resource.is_a? Moysklad::Resources::Base
      super resource
    end

    def all
      @cached_list || pull_list
    end

    def find uuid
      index[uuid]
    end

    def uuids
      index.keys
    end

    def resource
      __getobj__
    end

    private

    def values
      index.values
    end

    def index
      pull_list unless @_index
      @_index
    end

    def pull_list 
      @cached_list = load_full_list
      @_index = prepare_index @cached_list
      @cached_list
    end

    def load_full_list
      start = 0
      list = []

      page = nil

      begin
        page = collection start: start
        list += page.items
        break if page.items.empty?
        start = list.count
      end while start<page.total

      raise "При загрузке коллекции в результате колиество не совпадает с total: #{list.count}<>#{page.total}" unless list.count==page.total

      list
    end

    def prepare_index cached_list
      i={}
      cached_list.each do |r|
        raise "У объекта нет uuid: #{r.to_xml}" unless r.respond_to?(:uuid) && r.uuid
        i[r.uuid]=r
      end
      return i
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
moysklad-0.1.2 lib/moysklad/resources/indexed.rb
moysklad-0.1.1 lib/moysklad/resources/indexed.rb