Class: Nanoc::Core::IdentifiableCollection

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Enumerable, ContractsSupport
Defined in:
lib/nanoc/core/identifiable_collection.rb

Direct Known Subclasses

ItemCollection, LayoutCollection

Instance Method Summary collapse

Methods included from ContractsSupport

enabled?, included, setup_once

Constructor Details

#initializeIdentifiableCollection

Returns a new instance of IdentifiableCollection.



16
17
18
# File 'lib/nanoc/core/identifiable_collection.rb', line 16

def initialize(*)
  raise 'IdentifiableCollection is abstract and cannot be instantiated'
end

Instance Method Details

#[](arg) ⇒ Object



41
42
43
44
45
46
47
# File 'lib/nanoc/core/identifiable_collection.rb', line 41

def [](arg)
  if frozen?
    get_memoized(arg)
  else
    get_unmemoized(arg)
  end
end

#add(obj) ⇒ Object



69
70
71
# File 'lib/nanoc/core/identifiable_collection.rb', line 69

def add(obj)
  self.class.new(@config, @objects.add(obj))
end

#empty?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/nanoc/core/identifiable_collection.rb', line 64

def empty?
  @objects.empty?
end

#find_all(arg) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/nanoc/core/identifiable_collection.rb', line 50

def find_all(arg)
  if frozen?
    find_all_memoized(arg)
  else
    find_all_unmemoized(arg)
  end
end

#freezeObject



33
34
35
36
37
38
# File 'lib/nanoc/core/identifiable_collection.rb', line 33

def freeze
  @objects.freeze
  @objects.each(&:freeze)
  build_mapping
  super
end

#initialize_basic(config, objects = [], name = nil) ⇒ Object



21
22
23
24
25
# File 'lib/nanoc/core/identifiable_collection.rb', line 21

def initialize_basic(config, objects = [], name = nil)
  @config = config
  @objects = Hamster::Vector.new(objects)
  @name = name
end

#inspectObject



28
29
30
# File 'lib/nanoc/core/identifiable_collection.rb', line 28

def inspect
  "<#{self.class}>"
end

#object_with_identifier(identifier) ⇒ Object



79
80
81
82
83
84
85
# File 'lib/nanoc/core/identifiable_collection.rb', line 79

def object_with_identifier(identifier)
  if frozen?
    @mapping[identifier.to_s]
  else
    @objects.find { |i| i.identifier == identifier }
  end
end

#reject(&block) ⇒ Object



74
75
76
# File 'lib/nanoc/core/identifiable_collection.rb', line 74

def reject(&block)
  self.class.new(@config, @objects.reject(&block))
end

#to_aObject



59
60
61
# File 'lib/nanoc/core/identifiable_collection.rb', line 59

def to_a
  @objects.to_a
end