Class: Nanoc::ItemRepCollectionView

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

Defined Under Namespace

Classes: NoSuchItemRepError

Instance Method Summary collapse

Methods inherited from View

#_context, #frozen?

Instance Method Details

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

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::ItemRepView)

    if an item rep with the given name was found



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

def [](rep_name)
  case rep_name
  when Symbol
    res = @item_reps.find { |ir| ir.name == rep_name }
    res && Nanoc::ItemRepView.new(res, @context)
  when Fixnum
    raise ArgumentError, "expected ItemRepCollectionView#[] to be called with a symbol (you likely want `.reps[:default]` rather than `.reps[#{rep_name}]`)"
  else
    raise ArgumentError, 'expected ItemRepCollectionView#[] 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:

Yield Returns:

  • (void)

Returns:

  • (self)


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

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

#fetch(rep_name) ⇒ Nanoc::ItemRepView

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



70
71
72
73
74
75
76
77
# File 'lib/nanoc/base/views/item_rep_collection_view.rb', line 70

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

#sizeInteger

Returns:

  • (Integer)


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

def size
  @item_reps.size
end

#to_aryObject



22
23
24
# File 'lib/nanoc/base/views/item_rep_collection_view.rb', line 22

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