Class: Nanoc::Core::IdentifiableCollectionView
- Includes:
- Enumerable
- Defined in:
- lib/nanoc/core/identifiable_collection_view.rb
Direct Known Subclasses
ItemCollectionWithRepsView, ItemCollectionWithoutRepsView, LayoutCollectionView, MutableIdentifiableCollectionView, PostCompileItemCollectionView
Constant Summary collapse
- NOTHING =
Object.new
Instance Method Summary collapse
- #[](arg) ⇒ Object
- #_unwrap ⇒ Object private
-
#each {|object| ... } ⇒ self
Calls the given block once for each object, passing that object as a parameter.
-
#find_all(arg = NOTHING, &block) ⇒ Enumerable
Finds all objects whose identifier matches the given argument.
-
#initialize(objects, context) ⇒ IdentifiableCollectionView
constructor
private
A new instance of IdentifiableCollectionView.
- #size ⇒ Integer
- #view_class ⇒ Object abstract private
Methods inherited from View
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
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 |
#_unwrap ⇒ Object
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.
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.
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 |
#size ⇒ 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_class ⇒ Object
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.
24 25 26 |
# File 'lib/nanoc/core/identifiable_collection_view.rb', line 24 def view_class raise NotImplementedError end |