Class: Bunto::Collection
- Inherits:
-
Object
- Object
- Bunto::Collection
- Defined in:
- lib/bunto/collection.rb
Instance Attribute Summary collapse
-
#docs ⇒ Object
Fetch the Documents in this collection.
-
#label ⇒ Object
readonly
Returns the value of attribute label.
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#site ⇒ Object
readonly
Returns the value of attribute site.
Instance Method Summary collapse
-
#collection_dir(*files) ⇒ Object
The full path to the directory containing the collection, with optional subpaths.
-
#directory ⇒ Object
The full path to the directory containing the collection.
-
#entries ⇒ Object
All the entries in this collection.
-
#entry_filter ⇒ Object
The entry filter for this collection.
-
#exists? ⇒ Boolean
Checks whether the directory “exists” for this collection.
-
#extract_metadata ⇒ Object
Extract options for this collection from the site configuration.
-
#files ⇒ Object
Fetch the static files in this collection.
-
#filtered_entries ⇒ Object
Filtered version of the entries in this collection.
-
#initialize(site, label) ⇒ Collection
constructor
Create a new Collection.
-
#inspect ⇒ Object
An inspect string.
-
#method_missing(method, *args, &blck) ⇒ Object
Override of method_missing to check in @data for the key.
-
#read ⇒ Object
Read the allowed documents into the collection’s array of docs.
-
#relative_directory ⇒ Object
The directory for this Collection, relative to the site source.
-
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Override of normal respond_to? to match method_missing’s logic for looking in @data.
-
#sanitize_label(label) ⇒ Object
Produce a sanitized label name Label names may not contain anything but alphanumeric characters, underscores, and hyphens.
-
#to_liquid ⇒ Object
Produce a representation of this Collection for use in Liquid.
-
#url_template ⇒ Object
The URL template to render collection’s documents at.
-
#write? ⇒ Boolean
Whether the collection’s documents ought to be written as individual files in the output.
Constructor Details
#initialize(site, label) ⇒ Collection
Create a new Collection.
site - the site to which this collection belongs. label - the name of the collection
Returns nothing.
12 13 14 15 16 |
# File 'lib/bunto/collection.rb', line 12 def initialize(site, label) @site = site @label = sanitize_label(label) = end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method, *args, &blck) ⇒ Object
Override of method_missing to check in @data for the key.
33 34 35 36 37 38 39 40 41 42 |
# File 'lib/bunto/collection.rb', line 33 def method_missing(method, *args, &blck) if docs.respond_to?(method.to_sym) Bunto.logger.warn "Deprecation:", "#{label}.#{method} should be changed to" \ "#{label}.docs.#{method}." Bunto.logger.warn "", "Called by #{caller.first}." docs.public_send(method.to_sym, *args, &blck) else super end end |
Instance Attribute Details
#docs ⇒ Object
Fetch the Documents in this collection. Defaults to an empty array if no documents have been read in.
Returns an array of Bunto::Document objects.
22 23 24 |
# File 'lib/bunto/collection.rb', line 22 def docs @docs ||= [] end |
#label ⇒ Object (readonly)
Returns the value of attribute label.
3 4 5 |
# File 'lib/bunto/collection.rb', line 3 def label @label end |
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
3 4 5 |
# File 'lib/bunto/collection.rb', line 3 def end |
#site ⇒ Object (readonly)
Returns the value of attribute site.
3 4 5 |
# File 'lib/bunto/collection.rb', line 3 def site @site end |
Instance Method Details
#collection_dir(*files) ⇒ Object
The full path to the directory containing the collection, with
optional subpaths.
*files - (optional) any other path pieces relative to the
directory to append to the path
Returns a String containing th directory name where the collection
is stored on the filesystem.
120 121 122 123 |
# File 'lib/bunto/collection.rb', line 120 def collection_dir(*files) return directory if files.empty? site.in_source_dir(relative_directory, *files) end |
#directory ⇒ Object
The full path to the directory containing the collection.
Returns a String containing th directory name where the collection
is stored on the filesystem.
108 109 110 |
# File 'lib/bunto/collection.rb', line 108 def directory @directory ||= site.in_source_dir(relative_directory) end |
#entries ⇒ Object
All the entries in this collection.
Returns an Array of file paths to the documents in this collection
relative to the collection's directory
72 73 74 75 76 77 78 79 |
# File 'lib/bunto/collection.rb', line 72 def entries return [] unless exists? @entries ||= Utils.safe_glob(collection_dir, ["**", "*"]).map do |entry| entry["#{collection_dir}/"] = "" entry end end |
#entry_filter ⇒ Object
The entry filter for this collection. Creates an instance of Bunto::EntryFilter.
Returns the instance of Bunto::EntryFilter for this collection.
139 140 141 |
# File 'lib/bunto/collection.rb', line 139 def entry_filter @entry_filter ||= Bunto::EntryFilter.new(site, relative_directory) end |
#exists? ⇒ Boolean
Checks whether the directory “exists” for this collection. The directory must exist on the filesystem and must not be a symlink
if in safe mode.
Returns false if the directory doesn’t exist or if it’s a symlink
and we're in safe mode.
131 132 133 |
# File 'lib/bunto/collection.rb', line 131 def exists? File.directory?(directory) && !entry_filter.symlink?(directory) end |
#extract_metadata ⇒ Object
Extract options for this collection from the site configuration.
Returns the metadata for this collection
191 192 193 194 195 196 197 |
# File 'lib/bunto/collection.rb', line 191 def if site.config["collections"].is_a?(Hash) site.config["collections"][label] || {} else {} end end |
#files ⇒ Object
Fetch the static files in this collection. Defaults to an empty array if no static files have been read in.
Returns an array of Bunto::StaticFile objects.
48 49 50 |
# File 'lib/bunto/collection.rb', line 48 def files @files ||= [] end |
#filtered_entries ⇒ Object
Filtered version of the entries in this collection. See ‘Bunto::EntryFilter#filter` for more information.
Returns a list of filtered entry paths.
85 86 87 88 89 90 91 92 93 94 |
# File 'lib/bunto/collection.rb', line 85 def filtered_entries return [] unless exists? @filtered_entries ||= Dir.chdir(directory) do entry_filter.filter(entries).reject do |f| path = collection_dir(f) File.directory?(path) || entry_filter.symlink?(f) end end end |
#inspect ⇒ Object
An inspect string.
Returns the inspect string
146 147 148 |
# File 'lib/bunto/collection.rb', line 146 def inspect "#<Bunto::Collection @label=#{label} docs=#{docs}>" end |
#read ⇒ Object
Read the allowed documents into the collection’s array of docs.
Returns the sorted array of docs.
55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/bunto/collection.rb', line 55 def read filtered_entries.each do |file_path| full_path = collection_dir(file_path) next if File.directory?(full_path) if Utils.has_yaml_header? full_path read_document(full_path) else read_static_file(file_path, full_path) end end docs.sort! end |
#relative_directory ⇒ Object
The directory for this Collection, relative to the site source.
Returns a String containing the directory name where the collection
is stored on the filesystem.
100 101 102 |
# File 'lib/bunto/collection.rb', line 100 def relative_directory @relative_directory ||= "_#{label}" end |
#respond_to_missing?(method, include_private = false) ⇒ Boolean
Override of normal respond_to? to match method_missing’s logic for looking in @data.
28 29 30 |
# File 'lib/bunto/collection.rb', line 28 def respond_to_missing?(method, include_private = false) docs.respond_to?(method.to_sym, include_private) || super end |
#sanitize_label(label) ⇒ Object
Produce a sanitized label name Label names may not contain anything but alphanumeric characters,
underscores, and hyphens.
label - the possibly-unsafe label
Returns a sanitized version of the label.
157 158 159 |
# File 'lib/bunto/collection.rb', line 157 def sanitize_label(label) label.gsub(%r![^a-z0-9_\-\.]!i, "") end |
#to_liquid ⇒ Object
Produce a representation of this Collection for use in Liquid. Exposes two attributes:
- label
- docs
Returns a representation of this collection for use in Liquid.
167 168 169 |
# File 'lib/bunto/collection.rb', line 167 def to_liquid Drops::CollectionDrop.new self end |
#url_template ⇒ Object
The URL template to render collection’s documents at.
Returns the URL template to render collection’s documents at.
182 183 184 185 186 |
# File 'lib/bunto/collection.rb', line 182 def url_template @url_template ||= .fetch("permalink") do Utils.add_permalink_suffix("/:collection/:path", site.permalink_style) end end |
#write? ⇒ Boolean
Whether the collection’s documents ought to be written as individual
files in the output.
Returns true if the ‘write’ metadata is true, false otherwise.
175 176 177 |
# File 'lib/bunto/collection.rb', line 175 def write? !!.fetch("output", false) end |