Class: Nanoc::IdentifiableCollectionView

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

Instance Method Summary collapse

Methods inherited from View

#frozen?, #inspect

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:

    • string (String)

    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



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/nanoc/base/views/identifiable_collection_view.rb', line 86

def [](arg)
  prop_attribute =
    case arg
    when String, Nanoc::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

#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)


32
33
34
35
36
# File 'lib/nanoc/base/views/identifiable_collection_view.rb', line 32

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

#find_all(arg) ⇒ Enumerable

Finds all objects whose identifier matches the given argument.

Parameters:

  • arg (String, Regex)

Returns:

  • (Enumerable)


49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/nanoc/base/views/identifiable_collection_view.rb', line 49

def find_all(arg)
  prop_attribute =
    case arg
    when String, Nanoc::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)


39
40
41
42
# File 'lib/nanoc/base/views/identifiable_collection_view.rb', line 39

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