Class: Root::Manifest
- Inherits:
-
Object
- Object
- Root::Manifest
- Includes:
- Enumerable, Minimap
- Defined in:
- lib/root/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:: keyIf 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 Env, or nil.
-
#reader ⇒ Object
readonly
The reader on Env accessing manifests of the same type as this value.
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.
51 52 53 54 55 |
# File 'lib/root/manifest.rb', line 51 def initialize(entries=[]) @entries = entries @env = nil @reader = nil end |
Instance Attribute Details
#entries ⇒ Object (readonly)
An array entries in self.
41 42 43 |
# File 'lib/root/manifest.rb', line 41 def entries @entries end |
#env ⇒ Object (readonly)
The bound Env, or nil.
44 45 46 |
# File 'lib/root/manifest.rb', line 44 def env @env end |
#reader ⇒ Object (readonly)
The reader on Env accessing manifests of the same type as this value. Reader is set during bind.
48 49 50 |
# File 'lib/root/manifest.rb', line 48 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).
18 19 20 21 22 23 24 25 |
# File 'lib/root/manifest.rb', line 18 def intern(*args, &block) instance = new(*args) if block_given? instance.extend Intern(:minikey) instance.minikey_block = block end instance end |
Instance Method Details
#[](key) ⇒ Object
Alias for Minimap#minimatch.
113 114 115 |
# File 'lib/root/manifest.rb', line 113 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/root/manifest.rb', line 60 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.
82 83 84 |
# File 'lib/root/manifest.rb', line 82 def bound? @env != nil && @reader != nil end |
#build ⇒ Object
A hook for dynamically building entries. By default build simply returns self
88 89 90 |
# File 'lib/root/manifest.rb', line 88 def build self end |
#built? ⇒ Boolean
A hook to flag when self is built. By default built? returns true.
93 94 95 |
# File 'lib/root/manifest.rb', line 93 def built? true end |
#each ⇒ Object
Iterates over each entry entry in self.
108 109 110 |
# File 'lib/root/manifest.rb', line 108 def each entries.each {|entry| yield(entry) } end |
#empty? ⇒ Boolean
True if entries are empty.
103 104 105 |
# File 'lib/root/manifest.rb', line 103 def empty? entries.empty? end |
#inspect(traverse = true) ⇒ Object
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/root/manifest.rb', line 147 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.
98 99 100 |
# File 'lib/root/manifest.rb', line 98 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?
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/root/manifest.rb', line 122 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.
75 76 77 78 79 |
# File 'lib/root/manifest.rb', line 75 def unbind @env = nil @reader = nil self end |