class AmoCRM::Resources::Indexed
Constants
- ObjectHaveNoUuidError
- WrongEntriesCountError
Public Class Methods
new(resource)
click to toggle source
Calls superclass method
# File lib/amo_crm/resources/indexed.rb, line 10 def initialize resource raise TypeError, 'resource должен быть AmoCRM::Resources::Base' unless resource.is_a? AmoCRM::Resources::Base super resource end
Public Instance Methods
all()
click to toggle source
Автоматически подгружает постранично данныез из API и возвращает их все сразу.
@return [Array of AmoCRM::Entities::Base
]
# File lib/amo_crm/resources/indexed.rb, line 18 def all @cached_list || pull_list end
find(uuid)
click to toggle source
Возвращает запрашивемую запись из кеша. Предварительно подгружает все записи через метод ‘all`
@return Moyskald::Entities::Base
# File lib/amo_crm/resources/indexed.rb, line 26 def find uuid index[uuid] end
resource()
click to toggle source
Неиндексированный ресурс
@return AmoCRM::Resources::Base
# File lib/amo_crm/resources/indexed.rb, line 40 def resource __getobj__ end
uuids()
click to toggle source
Перечень uuid-ов всех элементов в ресуресе
@return [Array of uuids]
# File lib/amo_crm/resources/indexed.rb, line 33 def uuids index.keys end
Private Instance Methods
index()
click to toggle source
# File lib/amo_crm/resources/indexed.rb, line 50 def index pull_list unless @_index @_index end
load_full_list()
click to toggle source
# File lib/amo_crm/resources/indexed.rb, line 61 def load_full_list start = 0 list = [] _page = nil begin _page = page start: start list += _page.items break if _page.items.empty? start = list.count end while start<_page.total raise WrongEntriesCountError, "При загрузке коллекции в результате колиество не совпадает с total: #{list.count}<>#{_page.total}" unless list.count==_page.total list end
prepare_index(cached_list)
click to toggle source
# File lib/amo_crm/resources/indexed.rb, line 79 def prepare_index cached_list i={} cached_list.each do |r| raise ObjectHaveNoUuidError, "У объекта нет uuid: #{r.to_xml}" unless r.respond_to?(:uuid) && r.uuid i[r.uuid]=r end return i end
pull_list()
click to toggle source
# File lib/amo_crm/resources/indexed.rb, line 55 def pull_list @cached_list = load_full_list @_index = prepare_index @cached_list @cached_list end
values()
click to toggle source
# File lib/amo_crm/resources/indexed.rb, line 46 def values index.values end