Class: Tap::Support::Manifest
- Includes:
- Enumerable, Minimap
- Defined in:
- lib/tap/support/manifest.rb
Overview
Stores an array of paths and makes them available for lookup by minipath. Manifests may be bound to a Tap::Env, allowing searches across a full environment (including nested environments).
Manifest has a number of hooks used by subclasses like ConstantManifest to lazily add entries as needed.
Direct Known Subclasses
Constant Summary collapse
- SEARCH_REGEXP =
Matches a compound manifest search key. After the match, if the key is compound then:
$1:: env_key $4:: key
If the key is not compound, $4 is nil and $1 is the key.
/^(([A-z]:)?.*?)(:(.*))?$/
Instance Attribute Summary collapse
-
#entries ⇒ Object
readonly
An array entries in self.
-
#env ⇒ Object
readonly
The bound Tap::Env, or nil.
-
#reader ⇒ Object
readonly
The reader on Tap::Env accessing manifests of the same type as self.
Class Method Summary collapse
-
.intern(*args, &block) ⇒ Object
Interns a new manifest, overriding the minikey method with the block (the minikey method converts entries to the path used during minimap and minimatch lookup, see Minimap).
Instance Method Summary collapse
-
#[](key) ⇒ Object
Alias for Minimap#minimatch.
-
#bind(env, reader) ⇒ Object
Binds self to an env and reader.
-
#bound? ⇒ Boolean
True if the env and reader have been set.
-
#build ⇒ Object
A hook for dynamically building entries.
-
#built? ⇒ Boolean
A hook to flag when self is built.
-
#each ⇒ Object
Iterates over each entry entry in self.
-
#empty? ⇒ Boolean
True if entries are empty.
-
#initialize(entries = []) ⇒ Manifest
constructor
Initializes a new, unbound Manifest.
- #inspect(traverse = true) ⇒ Object
-
#reset ⇒ Object
A hook to reset a build.
-
#search(key) ⇒ Object
Search across env.each for the first entry minimatching key.
-
#unbind ⇒ Object
Unbinds self from env.
Methods included from Minimap
Constructor Details
#initialize(entries = []) ⇒ Manifest
Initializes a new, unbound Manifest.
52 53 54 55 56 |
# File 'lib/tap/support/manifest.rb', line 52 def initialize(entries=[]) @entries = entries @env = nil @reader = nil end |
Instance Attribute Details
#entries ⇒ Object (readonly)
An array entries in self.
42 43 44 |
# File 'lib/tap/support/manifest.rb', line 42 def entries @entries end |
#env ⇒ Object (readonly)
The bound Tap::Env, or nil.
45 46 47 |
# File 'lib/tap/support/manifest.rb', line 45 def env @env end |
#reader ⇒ Object (readonly)
The reader on Tap::Env accessing manifests of the same type as self. reader is set during bind.
49 50 51 |
# File 'lib/tap/support/manifest.rb', line 49 def reader @reader end |
Class Method Details
.intern(*args, &block) ⇒ Object
Interns a new manifest, overriding the minikey method with the block (the minikey method converts entries to the path used during minimap and minimatch lookup, see Minimap).
19 20 21 22 23 24 25 26 |
# File 'lib/tap/support/manifest.rb', line 19 def intern(*args, &block) instance = new(*args) if block_given? instance.extend Support::Intern(:minikey) instance.minikey_block = block end instance end |
Instance Method Details
#[](key) ⇒ Object
Alias for Minimap#minimatch.
114 115 116 |
# File 'lib/tap/support/manifest.rb', line 114 def [](key) minimatch(key) end |
#bind(env, reader) ⇒ Object
Binds self to an env and reader. The manifests returned by env.reader will be used during traversal methods like search. Raises an error if env does not respond to reader; returns self.
61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/tap/support/manifest.rb', line 61 def bind(env, reader) if env == nil raise ArgumentError, "env may not be nil" end unless env.respond_to?(reader) raise ArgumentError, "env does not respond to #{reader}" end @env = env @reader = reader self end |
#bound? ⇒ Boolean
True if the env and reader have been set.
83 84 85 |
# File 'lib/tap/support/manifest.rb', line 83 def bound? @env != nil && @reader != nil end |
#build ⇒ Object
A hook for dynamically building entries. By default build simply returns self
89 90 91 |
# File 'lib/tap/support/manifest.rb', line 89 def build self end |
#built? ⇒ Boolean
A hook to flag when self is built. By default built? returns true.
94 95 96 |
# File 'lib/tap/support/manifest.rb', line 94 def built? true end |
#each ⇒ Object
Iterates over each entry entry in self.
109 110 111 |
# File 'lib/tap/support/manifest.rb', line 109 def each entries.each {|entry| yield(entry) } end |
#empty? ⇒ Boolean
True if entries are empty.
104 105 106 |
# File 'lib/tap/support/manifest.rb', line 104 def empty? entries.empty? end |
#inspect(traverse = true) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/tap/support/manifest.rb', line 148 def inspect(traverse=true) if traverse && bound? lines = [] env.each do |env| manifest = env.send(reader).build next if manifest.empty? lines << "== #{env.root.root}" manifest.minimap.each do |mini, value| lines << " #{mini}: #{value.inspect}" end end return lines.join("\n") end lines = minimap.collect do |mini, value| " #{mini}: #{value.inspect}" end "#{self.class}:#{object_id} (#{bound? ? env.root.root : ''})\n#{lines.join("\n")}" end |
#reset ⇒ Object
A hook to reset a build. By default reset simply returns self.
99 100 101 |
# File 'lib/tap/support/manifest.rb', line 99 def reset self end |
#search(key) ⇒ Object
Search across env.each for the first entry minimatching key. A single env can be specified by using a compound key like ‘env_key:key’. Returns nil if no matching entry is found.
Search raises an error unless bound?
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/tap/support/manifest.rb', line 123 def search(key) raise "cannot search unless bound" unless bound? key =~ SEARCH_REGEXP envs = if $4 != nil # compound key, match for env key = $4 [env.minimatch($1)].compact else # not a compound key, search all # envs by iterating env itself env end # traverse envs looking for the first # manifest entry matching key envs.each do |env| if result = env.send(reader).minimatch(key) return result end end nil end |
#unbind ⇒ Object
Unbinds self from env. Returns self.
76 77 78 79 80 |
# File 'lib/tap/support/manifest.rb', line 76 def unbind @env = nil @reader = nil self end |