class Nanoc::Core::IdentifiableCollection

Public Class Methods

new() click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 16
def initialize
  raise 'IdentifiableCollection is abstract and cannot be instantiated'
end

Public Instance Methods

[](arg) click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 41
def [](arg)
  if frozen?
    get_memoized(arg)
  else
    get_unmemoized(arg)
  end
end
add(obj) click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 69
def add(obj)
  self.class.new(@config, @objects.add(obj))
end
empty?() click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 64
def empty?
  @objects.empty?
end
find_all(arg) click to toggle source
# 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
freeze() click to toggle source
Calls superclass method
# File lib/nanoc/core/identifiable_collection.rb, line 33
def freeze
  @objects.freeze
  each(&:freeze)
  build_mapping
  super
end
initialize_basic(config, objects = [], name = nil) click to toggle source
# 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
inspect() click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 28
def inspect
  "<#{self.class}>"
end
object_with_identifier(identifier) click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 79
def object_with_identifier(identifier)
  if frozen?
    @mapping[identifier.to_s]
  else
    find { |i| i.identifier == identifier }
  end
end
reject(&block) click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 74
def reject(&block)
  self.class.new(@config, @objects.reject(&block))
end
to_a() click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 59
def to_a
  @objects.to_a
end

Protected Instance Methods

build_mapping() click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 126
def build_mapping
  @mapping = {}
  each do |object|
    @mapping[object.identifier.to_s] = object
  end
end
find_all_memoized(arg) click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 115
         def find_all_memoized(arg)
  find_all_unmemoized(arg)
end
find_all_unmemoized(arg) click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 109
def find_all_unmemoized(arg)
  pat = Pattern.from(arg)
  select { |i| pat.match?(i.identifier) }
end
get_memoized(arg) click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 104
         def get_memoized(arg)
  get_unmemoized(arg)
end
get_unmemoized(arg) click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 90
def get_unmemoized(arg)
  case arg
  when Nanoc::Core::Identifier
    object_with_identifier(arg)
  when String
    object_with_identifier(arg) || object_matching_glob(arg)
  when Regexp
    find { |i| i.identifier.to_s =~ arg }
  else
    raise ArgumentError, "don’t know how to fetch objects by #{arg.inspect}"
  end
end
object_matching_glob(glob) click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 119
def object_matching_glob(glob)
  if use_globs?
    pat = Pattern.from(glob)
    find { |i| pat.match?(i.identifier) }
  end
end
use_globs?() click to toggle source
# File lib/nanoc/core/identifiable_collection.rb, line 134
def use_globs?
  @config[:string_pattern_type] == 'glob'
end