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

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object



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

def method_missing(meth, *args, &block)
  if model_groups.include?(meth.to_s)
    find_models_by_type(meth)
  end
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



120
121
122
123
124
125
126
# File 'lib/brief/repository.rb', line 120

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|

  end
end

Instance Method Details

#all_modelsObject



95
96
97
98
99
100
101
102
103
# File 'lib/brief/repository.rb', line 95

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

                    list
                  end
end

#all_models_by_typeObject



105
106
107
108
109
110
111
112
# File 'lib/brief/repository.rb', line 105

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

#docs_pathObject



81
82
83
# File 'lib/brief/repository.rb', line 81

def docs_path
  briefcase.docs_path
end

#document_at(path) ⇒ Object



38
39
40
41
42
# File 'lib/brief/repository.rb', line 38

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



91
92
93
# File 'lib/brief/repository.rb', line 91

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

#documentsObject



64
65
66
67
# File 'lib/brief/repository.rb', line 64

def documents
  return @documents if @documents
  load_documents
end

#documents_at(*paths) ⇒ Object



52
53
54
55
56
57
58
# File 'lib/brief/repository.rb', line 52

def documents_at(*paths)
  paths.compact!

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

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

#documents_at!(*paths) ⇒ Object



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

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

#find_models_by_type(group_name) ⇒ Object



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

def find_models_by_type(group_name)
  type = group_name.to_s.singularize
  all_models_by_type.fetch(type) { [] }
end

#load_documentsObject



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

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

#model_groupsObject



34
35
36
# File 'lib/brief/repository.rb', line 34

def model_groups
  documents.map(&:document_type).tap {|l| l.compact!; l.uniq!; l.map! {|i| i.pluralize } }
end

#models_at(*paths) ⇒ Object



60
61
62
# File 'lib/brief/repository.rb', line 60

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

#normalize_path(p) ⇒ Object



48
49
50
# File 'lib/brief/repository.rb', line 48

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

#order_by(*args) ⇒ Object



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

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

#purge(model_type = nil) ⇒ Object



114
115
116
117
118
# File 'lib/brief/repository.rb', line 114

def purge(model_type=nil)
  load_documents
  @all_models_by_type = nil
  @all_models = nil
end

#respond_to?(meth) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(meth)
  super || model_groups.include?(meth.to_s)
end

#rootObject



77
78
79
# File 'lib/brief/repository.rb', line 77

def root
  briefcase.root
end

#where(*args) ⇒ Object



69
70
71
# File 'lib/brief/repository.rb', line 69

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