Class: Pandoru::Models::PandoraListModel

Inherits:
PandoraModel show all
Includes:
Enumerable
Defined in:
lib/pandoru/models/_base.rb

Overview

List Model for indexed collections

Direct Known Subclasses

PandoraDictListModel

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from PandoraModel

date_field, field, fields, from_json_list, #inspect, #populate_from_json, #to_h

Constructor Details

#initialize(api_client) ⇒ PandoraListModel

Returns a new instance of PandoraListModel.



300
301
302
303
304
# File 'lib/pandoru/models/_base.rb', line 300

def initialize(api_client)
  super(api_client)
  @items = []
  @index = {}
end

Class Method Details

.from_json(api_client, data) ⇒ Object



256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# File 'lib/pandoru/models/_base.rb', line 256

def self.from_json(api_client, data)
  instance = new(api_client)
  instance.populate_from_json(data)

  # Extract the list items
  list_key = instance.class.list_key
  list_model = instance.class.list_model
  
  if list_key && list_model && data[list_key]
    items = list_model.from_json_list(api_client, data[list_key])
    instance.instance_variable_set(:@items, items)
    
    # Create index if specified
    if instance.class.index_key
      index = {}
      items.each { |item| index[item.send(instance.class.index_key)] = item }
      instance.instance_variable_set(:@index, index)
    end
  else
    instance.instance_variable_set(:@items, [])
    instance.instance_variable_set(:@index, {})
  end

  instance
end

.index_keyObject



290
291
292
# File 'lib/pandoru/models/_base.rb', line 290

def self.index_key
  @index_key
end

.list_keyObject



282
283
284
# File 'lib/pandoru/models/_base.rb', line 282

def self.list_key
  @list_key
end

.list_modelObject



286
287
288
# File 'lib/pandoru/models/_base.rb', line 286

def self.list_model
  @list_model
end

.set_list_config(list_key:, list_model:, index_key: nil) ⇒ Object



294
295
296
297
298
# File 'lib/pandoru/models/_base.rb', line 294

def self.set_list_config(list_key:, list_model:, index_key: nil)
  @list_key = list_key
  @list_model = list_model
  @index_key = index_key
end

Instance Method Details

#[](key) ⇒ Object



310
311
312
313
314
315
316
# File 'lib/pandoru/models/_base.rb', line 310

def [](key)
  if key.is_a?(Integer)
    @items[key]
  else
    @index[key] if @index
  end
end

#[]=(index, value) ⇒ Object



318
319
320
# File 'lib/pandoru/models/_base.rb', line 318

def []=(index, value)
  @items[index] = value if index.is_a?(Integer)
end

#each(&block) ⇒ Object



306
307
308
# File 'lib/pandoru/models/_base.rb', line 306

def each(&block)
  @items.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


330
331
332
# File 'lib/pandoru/models/_base.rb', line 330

def empty?
  @items.empty?
end

#lengthObject



326
327
328
# File 'lib/pandoru/models/_base.rb', line 326

def length
  @items.length
end

#sizeObject



322
323
324
# File 'lib/pandoru/models/_base.rb', line 322

def size
  @items.size
end