lib/brief/repository.rb in brief-1.4.4 vs lib/brief/repository.rb in brief-1.5.0
- old
+ new
@@ -70,10 +70,37 @@
def document_paths
Dir[root.join('**/*.md').to_s].map { |p| Pathname(p) }
end
+ def all_models
+ list = documents.map(&:to_model)
+ list.compact!
+ list.select!(&:exists?)
+
+ list
+ end
+
+ def all_models_by_type
+ all_models.reduce({}) do |memo, model|
+ (memo[model.class.type_alias] ||= []) << model if model.exists?
+ memo
+ end
+ end
+
+ def purge(model_type=nil)
+ if model_type
+ plural = model_type.to_s.pluralize
+
+ if instance_variable_get("@#{ plural }")
+ instance_variable_set("@#{plural}",nil)
+ end
+ end
+
+ documents.reject! {|doc| !doc.path.exist? }
+ end
+
def self.define_document_finder_methods
# Create a finder method on the repository
# which lets us find instances of models by their class name
Brief::Model.table.keys.each do |type|
plural = type.to_s.pluralize
@@ -81,10 +108,10 @@
define_method("#{ plural }") do
instance_variable_get("@#{ plural }") || send("#{ plural }!")
end
define_method("#{ plural }!") do
- instance_variable_set("@#{plural}", Brief::Model.for_type(type).models.to_a)
+ instance_variable_set("@#{plural}", Brief::Model.existing_models_for_type(type))
instance_variable_get("@#{ plural }")
end
end
end
end