lib/jsus/container.rb in jsus-0.2.5 vs lib/jsus/container.rb in jsus-0.2.6
- old
+ new
@@ -1,11 +1,11 @@
-#
-# Container is an array which contains source files. Main difference
-# between it and array is the fact that container maintains topological
-# sort for the source files.
-#
module Jsus
+ #
+ # Container is an array that contains source files. Main difference
+ # from an array is the fact that container maintains topological
+ # sort for the source files.
+ #
class Container
#
# Every argument for initializer is pushed into the container.
#
def initialize(*sources)
@@ -33,11 +33,13 @@
# Flattens the container items.
def flatten
map {|item| item.respond_to?(:flatten) ? item.flatten : item }.flatten
end
- # Contains the source files in the correct order.
+ # Contains the source files. Please, don't use sources directly, if you
+ # depend on them to be topologically sorted. Use collection methods like
+ # inject/reject/map directly on the container instead.
def sources
@sources ||= []
end
alias_method :to_a, :sources
@@ -84,85 +86,86 @@
items = sources
items.each {|item| graph.add_vertex(item) }
# init edges
items.each do |item|
item.dependencies.each do |dependency|
- cache[dependency] ||= provides_tree.glob("/" + dependency.to_s).map {|node| node.value }
- cache[dependency].each do |required_item|
+ # If we can find items that provide the required dependency...
+ # (dependency could be a wildcard as well, hence items)
+ dependency_cache[dependency] ||= provides_tree.glob(dependency)
+ # ... we draw an edge from every required item to the dependant item
+ dependency_cache[dependency].each do |required_item|
graph.add_edge(required_item, item)
end
end
end
result = []
graph.topsort_iterator.each { |item| result << item }
result
end
- def cache # :nodoc:
- @cache ||= {}
+ def dependency_cache # :nodoc:
+ @dependency_cache ||= {}
end
def provides_tree # :nodoc:
@provides_tree ||= provides_tree!
end
+ # Provides tree contains
def provides_tree! # :nodoc:
- tree = Tree.new
+ tree = Util::Tree.new
# Provisions
sources.each do |file|
file.provides.each do |tag|
- tree["/#{tag}"] = file
+ tree[tag] = file
end
end
# Replacements
sources.each do |file|
if file.replaces
- tree["/#{file.replaces}"] = file
+ tree[file.replaces] = file
end
end
tree
end
- def remove_replaced_files!
+ def remove_replaced_files! # :nodoc:
sources.reject! do |sf|
- !sf.provides.empty? && sf.provides.any? { |tag|
- replacements_tree["/#{tag}"] &&
- replacements_tree["/#{tag}"].value &&
- replacements_tree["/#{tag}"].value != sf
- }
+ !sf.provides.empty? && sf.provides.any? { |tag| replacements_tree[tag] && replacements_tree[tag] != sf }
end
end
- def replacements_tree
+ def replacements_tree # :nodoc:
@replacements_tree ||= replacements_tree!
end
- def replacements_tree!
- tree = Tree.new
+ def replacements_tree! # :nodoc:
+ tree = Util::Tree.new
sources.each do |file|
if file.replaces
- tree["/#{file.replaces}"] = file
+ tree[file.replaces] = file
end
end
tree
end
def clear_cache! # :nodoc:
@provides_tree = nil
@replacements_tree = nil
- @cache = nil
- @sorted = nil
+ @dependency_cache = nil
+ @sorted = false
end
CACHE_CLEAR_METHODS = [
- "map!", "reject!", "inject!"
+ "map!", "reject!", "inject!", "collect!", "delete", "delete_at"
] # :nodoc:
DELEGATED_METHODS = [
"==", "to_a", "map", "map!", "each", "inject", "inject!",
- "reject", "reject!", "detect", "size", "length", "[]",
- "empty?", "index", "include?", "select", "-", "+", "|", "&"
+ "collect", "collect!", "reject", "reject!", "detect", "size",
+ "length", "[]", "empty?", "index", "include?", "select",
+ "delete_if", "delete", "-", "+", "|", "&"
] # :nodoc:
# delegates most Enumerable methods to #sources
(DELEGATED_METHODS).each do |m|
class_eval <<-EVAL
def #{m}(*args, &block)
\ No newline at end of file