Class: Nanoc::BasicItemRepCollectionView

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

Defined Under Namespace

Classes: NoSuchItemRepError

Instance Method Summary collapse

Methods inherited from View

#frozen?, #inspect

Instance Method Details

#[](rep_name) ⇒ nil, Nanoc::BasicItemRepView

Return the item rep with the given name, or nil if no item rep exists.

Parameters:

  • rep_name (Symbol)

Returns:

  • (nil)

    if no item rep with the given name was found

  • (Nanoc::BasicItemRepView)

    if an item rep with the given name was found



57
58
59
60
61
62
63
64
65
66
67
# File 'lib/nanoc/base/views/basic_item_rep_collection_view.rb', line 57

def [](rep_name)
  case rep_name
  when Symbol
    res = @item_reps.find { |ir| ir.name == rep_name }
    res && view_class.new(res, @context)
  when Integer
    raise ArgumentError, "expected BasicItemRepCollectionView#[] to be called with a symbol (you likely want `.reps[:default]` rather than `.reps[#{rep_name}]`)"
  else
    raise ArgumentError, 'expected BasicItemRepCollectionView#[] to be called with a symbol'
  end
end

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

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

Yield Parameters:

  • item (Object)

    rep view

Yield Returns:

  • (void)

Returns:

  • (self)


40
41
42
43
# File 'lib/nanoc/base/views/basic_item_rep_collection_view.rb', line 40

def each
  @item_reps.each { |ir| yield view_class.new(ir, @context) }
  self
end

#fetch(rep_name) ⇒ Nanoc::BasicItemRepView

Return the item rep with the given name, or raises an exception if there is no rep with the given name.

Parameters:

  • rep_name (Symbol)

Returns:

Raises:

  • if no rep was found



77
78
79
80
81
82
83
84
# File 'lib/nanoc/base/views/basic_item_rep_collection_view.rb', line 77

def fetch(rep_name)
  res = @item_reps.find { |ir| ir.name == rep_name }
  if res
    view_class.new(res, @context)
  else
    raise NoSuchItemRepError.new(rep_name)
  end
end

#sizeInteger

Returns:

  • (Integer)


46
47
48
# File 'lib/nanoc/base/views/basic_item_rep_collection_view.rb', line 46

def size
  @item_reps.size
end

#to_aryObject



29
30
31
# File 'lib/nanoc/base/views/basic_item_rep_collection_view.rb', line 29

def to_ary
  @item_reps.map { |ir| view_class.new(ir, @context) }
end