class Nanoc::Int::IdentifiableCollection

@api private

Public Class Methods

new(config) click to toggle source
# File lib/nanoc/base/entities/identifiable_collection.rb, line 13
def initialize(config)
  @config = config

  @objects = []
end

Public Instance Methods

[](arg) click to toggle source
# File lib/nanoc/base/entities/identifiable_collection.rb, line 26
def [](arg)
  case arg
  when Nanoc::Identifier
    object_with_identifier(arg)
  when String
    object_with_identifier(arg) || object_matching_glob(arg)
  when Regexp
    @objects.find { |i| i.identifier.to_s =~ arg }
  else
    raise ArgumentError, "don’t know how to fetch objects by #{arg.inspect}"
  end
end
delete_if(&block) click to toggle source
# File lib/nanoc/base/entities/identifiable_collection.rb, line 47
def delete_if(&block)
  @objects.delete_if(&block)
end
empty?() click to toggle source
# File lib/nanoc/base/entities/identifiable_collection.rb, line 43
def empty?
  @objects.empty?
end
freeze() click to toggle source
Calls superclass method
# File lib/nanoc/base/entities/identifiable_collection.rb, line 19
def freeze
  @objects.freeze
  @objects.each(&:freeze)
  build_mapping
  super
end
to_a() click to toggle source
# File lib/nanoc/base/entities/identifiable_collection.rb, line 39
def to_a
  @objects
end

Protected Instance Methods

build_mapping() click to toggle source
# File lib/nanoc/base/entities/identifiable_collection.rb, line 70
def build_mapping
  @mapping = {}
  @objects.each do |object|
    @mapping[object.identifier.to_s] = object
  end
  @mapping.freeze
end
object_matching_glob(glob) click to toggle source
# File lib/nanoc/base/entities/identifiable_collection.rb, line 61
def object_matching_glob(glob)
  if use_globs?
    pat = Nanoc::Int::Pattern.from(glob)
    @objects.find { |i| pat.match?(i.identifier) }
  else
    nil
  end
end
object_with_identifier(identifier) click to toggle source
# File lib/nanoc/base/entities/identifiable_collection.rb, line 53
def object_with_identifier(identifier)
  if frozen?
    @mapping[identifier.to_s]
  else
    @objects.find { |i| i.identifier == identifier }
  end
end
use_globs?() click to toggle source
# File lib/nanoc/base/entities/identifiable_collection.rb, line 78
def use_globs?
  @config[:string_pattern_type] == 'glob'
end