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
28
29
30
31
32
33
34
# File 'lib/brief/repository.rb', line 23

def method_missing(meth, *args, &block)
  in_model_group = model_groups.include?(meth.to_s)

  if in_model_group && args.empty?
    find_models_by_type(meth)
  elsif in_model_group && !args.empty?
    group = find_models_by_type(meth)
    Brief::DocumentMapper::Query.new(group).send(:where, *args)
  else
    super
  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



127
128
129
130
131
132
133
# File 'lib/brief/repository.rb', line 127

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



102
103
104
105
106
107
108
109
110
# File 'lib/brief/repository.rb', line 102

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



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

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



88
89
90
# File 'lib/brief/repository.rb', line 88

def docs_path
  briefcase.docs_path
end

#document_at(path) ⇒ Object



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

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



98
99
100
# File 'lib/brief/repository.rb', line 98

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

#documentsObject



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

def documents
  return @documents if @documents
  load_documents
end

#documents_at(*paths) ⇒ Object



59
60
61
62
63
64
65
# File 'lib/brief/repository.rb', line 59

def documents_at(*paths)
  paths.compact!

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

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

#documents_at!(*paths) ⇒ Object



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

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



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

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

#load_documentsObject



92
93
94
95
96
# File 'lib/brief/repository.rb', line 92

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

#model_groupsObject



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

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

#models_at(*paths) ⇒ Object



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

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

#normalize_path(p) ⇒ Object



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

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

#order_by(*args) ⇒ Object



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

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

#purge(model_type = nil) ⇒ Object



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

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



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

def root
  briefcase.root
end

#where(*args) ⇒ Object



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

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