Class: GitDS::ModelItemList

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/git-ds/model/item_list.rb

Overview

A generic list of ModelItem objects.

This associates a ModelItem class with a model and a base path in that model. All elements in this list are subdirectories of the base path, and will be instantiated/created/listed using methods in the ModelItem class.

This is used for ModelItem children of a ModelItem object (NOT Property children).

Direct Known Subclasses

ProxyItemList

Instance Method Summary collapse

Constructor Details

#initialize(cls, model, path) ⇒ ModelItemList

Returns a new instance of ModelItemList.



23
24
25
26
27
# File 'lib/git-ds/model/item_list.rb', line 23

def initialize(cls, model, path)
  @item_class = cls
  @model = model
  @base_path = path
end

Instance Method Details

#[](ident) ⇒ Object

Return instance of ModelItem class for ‘ident’.



71
72
73
# File 'lib/git-ds/model/item_list.rb', line 71

def [](ident)
  @item_class.new(@model, @item_class.instance_path(@base_path, ident))
end

#add(parent, args) ⇒ Object

Add an instance of ModelItem class to ‘parent’ based on ‘args’.

Note: This calls ModelItemClass.create, so args must be a suitable Hash. When a ProxyModelItemClass is used as the item class, the args will be passed to ProxyModelItemClass.create.



82
83
84
# File 'lib/git-ds/model/item_list.rb', line 82

def add(parent, args)
  @item_class.create(parent, args)
end

#countObject

Return number of items in list.



41
42
43
# File 'lib/git-ds/model/item_list.rb', line 41

def count
  keys.count
end

#delete(ident) ⇒ Object

Delete instance of ModelItem from list.

Note: this has the same effect as just calling item#delete.



91
92
93
94
# File 'lib/git-ds/model/item_list.rb', line 91

def delete(ident)
  item = self[ident]
  item.delete if item
end

#eachObject

Yield each ident in list.

See keys.



64
65
66
# File 'lib/git-ds/model/item_list.rb', line 64

def each
  keys.each { |key| yield key }
end

#firstObject

Return first item list.



48
49
50
# File 'lib/git-ds/model/item_list.rb', line 48

def first
  keys.first 
end

#keysObject

List ModelItem class instances contained in this list.

Note: This always returns a sorted list.



34
35
36
# File 'lib/git-ds/model/item_list.rb', line 34

def keys
  @item_class.list_in_path(@model, @base_path)
end

#lastObject

Return last item list.



55
56
57
# File 'lib/git-ds/model/item_list.rb', line 55

def last
  keys.last
end