Class: Brief::Repository

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/brief/repository.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(briefcase, options = {}) ⇒ Repository

Returns a new instance of Repository.



12
13
14
15
16
17
# File 'lib/brief/repository.rb', line 12

def initialize(briefcase, options = {})
  @briefcase = briefcase
  @options = options

  load_documents
end

Instance Attribute Details

#briefcaseObject (readonly)

Returns the value of attribute briefcase.



3
4
5
# File 'lib/brief/repository.rb', line 3

def briefcase
  @briefcase
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/brief/repository.rb', line 3

def options
  @options
end

Class Method Details

.define_document_finder_methodsObject



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/brief/repository.rb', line 103

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

    define_method("#{ plural }") do
      instance_variable_get("@#{ plural }") || send("#{ plural }!")
    end

    define_method("#{ plural }!") do
      instance_variable_set("@#{plural}", Brief::Model.existing_models_for_type(type))
      instance_variable_get("@#{ plural }")
    end
  end
end

Instance Method Details

#all_modelsObject



76
77
78
79
80
81
82
# File 'lib/brief/repository.rb', line 76

def all_models
  list = documents.select(&:refresh!).map(&:to_model)
  list.compact!
  list.select!(&:exists?)

  list
end

#all_models_by_typeObject



84
85
86
87
88
89
# File 'lib/brief/repository.rb', line 84

def all_models_by_type
  all_models.reduce({}) do |memo, model|
    (memo[model.class.type_alias] ||= []) << model if model.exists?
    memo
  end
end

#docs_pathObject



62
63
64
# File 'lib/brief/repository.rb', line 62

def docs_path
  briefcase.docs_path
end

#document_at(path) ⇒ Object



19
20
21
22
23
# File 'lib/brief/repository.rb', line 19

def document_at(path)
  path = normalize_path(path)
  found = documents.find {|doc| doc.path == path }
  found || Brief::Document.new(path).in_briefcase(briefcase)
end

#document_pathsObject



72
73
74
# File 'lib/brief/repository.rb', line 72

def document_paths
  Dir[root.join('**/*.md').to_s].map { |p| Pathname(p) }
end

#documentsObject



45
46
47
48
# File 'lib/brief/repository.rb', line 45

def documents
  return @documents if @documents
  load_documents
end

#documents_at(*paths) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/brief/repository.rb', line 33

def documents_at(*paths)
  paths.compact!

  paths.map! {|p| normalize_path(p) }

  paths.map {|p| p && document_at(p) }
end

#documents_at!(*paths) ⇒ Object



25
26
27
# File 'lib/brief/repository.rb', line 25

def documents_at!(*paths)
  documents_at(*paths).select {|doc| doc.path.exist? }
end

#each(*args, &block) ⇒ Object

should compare vs yield



8
9
10
# File 'lib/brief/repository.rb', line 8

def each(*args, &block)
  documents.send(:each, *args, &block)
end

#load_documentsObject



66
67
68
69
70
# File 'lib/brief/repository.rb', line 66

def load_documents
  @documents = document_paths.map do |path|
    Brief::Document.new(path).in_briefcase(briefcase)
  end
end

#models_at(*paths) ⇒ Object



41
42
43
# File 'lib/brief/repository.rb', line 41

def models_at(*paths)
  documents_at(*paths).map(&:to_model)
end

#normalize_path(p) ⇒ Object



29
30
31
# File 'lib/brief/repository.rb', line 29

def normalize_path(p)
  docs_path.join(p)
end

#order_by(*args) ⇒ Object



54
55
56
# File 'lib/brief/repository.rb', line 54

def order_by(*args)
  Brief::DocumentMapper::Query.new(self).send(:order_by, *args)
end

#purge(model_type = nil) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
# File 'lib/brief/repository.rb', line 91

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

#rootObject



58
59
60
# File 'lib/brief/repository.rb', line 58

def root
  briefcase.root
end

#where(*args) ⇒ Object



50
51
52
# File 'lib/brief/repository.rb', line 50

def where(*args)
  Brief::DocumentMapper::Query.new(self).send(:where, *args)
end