Class: Nanoc::Core::IdentifiableCollectionView

Inherits:
View
  • Object
show all
Includes:
Enumerable
Defined in:
lib/nanoc/core/identifiable_collection_view.rb

Constant Summary collapse

NOTHING =
Object.new

Instance Method Summary collapse

Methods inherited from View

#_context, #frozen?, #inspect

Methods included from ContractsSupport

enabled?, included, setup_once, warn_about_performance

Constructor Details

#initialize(objects, context) ⇒ IdentifiableCollectionView

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of IdentifiableCollectionView.



11
12
13
14
# File 'lib/nanoc/core/identifiable_collection_view.rb', line 11

def initialize(objects, context)
  super(context)
  @objects = objects
end

Instance Method Details

#[](string) ⇒ nil, #identifier #[](regex) ⇒ nil, #identifier

Overloads:

  • #[](string) ⇒ nil, #identifier

    Finds the object whose identifier matches the given string.

    If the glob syntax is enabled, the string can be a glob, in which case this method finds the first object that matches the given glob.

    Parameters:

    Returns:

    • (nil)

      if no object matches the string

    • (#identifier)

      if an object was found

  • #[](regex) ⇒ nil, #identifier

    Finds the object whose identifier matches the given regular expression.

    Parameters:

    • regex (Regex)

    Returns:

    • (nil)

      if no object matches the regex

    • (#identifier)

      if an object was found



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/nanoc/core/identifiable_collection_view.rb', line 94

def [](arg)
  prop_attribute =
    case arg
    when String, Nanoc::Core::Identifier
      [arg.to_s]
    when Regexp
      [arg]
    else
      true
    end

  @context.dependency_tracker.bounce(_unwrap, raw_content: prop_attribute)
  res = @objects[arg]
  res && view_class.new(res, @context)
end

#_unwrapObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



17
18
19
# File 'lib/nanoc/core/identifiable_collection_view.rb', line 17

def _unwrap
  @objects
end

#each {|object| ... } ⇒ self

Calls the given block once for each object, passing that object as a parameter.

Yield Parameters:

  • object (#identifier)

Yield Returns:

  • (void)

Returns:

  • (self)


35
36
37
38
39
# File 'lib/nanoc/core/identifiable_collection_view.rb', line 35

def each
  @context.dependency_tracker.bounce(_unwrap, raw_content: true)
  @objects.each { |i| yield view_class.new(i, @context) }
  self
end

#find_all(arg = NOTHING, &block) ⇒ Enumerable

Finds all objects whose identifier matches the given argument.

Parameters:

  • arg (String, Regex) (defaults to: NOTHING)

Returns:

  • (Enumerable)


52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nanoc/core/identifiable_collection_view.rb', line 52

def find_all(arg = NOTHING, &block)
  if NOTHING.equal?(arg)
    @context.dependency_tracker.bounce(_unwrap, raw_content: true)
    return @objects.map { |i| view_class.new(i, @context) }.select(&block)
  end

  prop_attribute =
    case arg
    when String, Nanoc::Core::Identifier
      [arg.to_s]
    when Regexp
      [arg]
    else
      true
    end

  @context.dependency_tracker.bounce(_unwrap, raw_content: prop_attribute)
  @objects.find_all(arg).map { |i| view_class.new(i, @context) }
end

#sizeInteger

Returns:

  • (Integer)


42
43
44
45
# File 'lib/nanoc/core/identifiable_collection_view.rb', line 42

def size
  @context.dependency_tracker.bounce(_unwrap, raw_content: true)
  @objects.size
end

#view_classObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method is abstract.

Raises:

  • (NotImplementedError)


24
25
26
# File 'lib/nanoc/core/identifiable_collection_view.rb', line 24

def view_class
  raise NotImplementedError
end