Class: Brief::Repository

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

Constant Summary collapse

InvalidPath =
Class.new(Exception)

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 if options[:eager]
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/brief/repository.rb', line 28

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

  if options[:caching] == false || options[:cache] == false
    @all_models = nil
    @all_models_by_type = nil
  end

  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



150
151
152
153
154
155
156
# File 'lib/brief/repository.rb', line 150

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



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

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



135
136
137
138
139
140
141
142
# File 'lib/brief/repository.rb', line 135

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

#cache_keyObject



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

def cache_key
  "#{documents.count}-#{documents.map {|d| d.path.mtime.to_i }.max}"
end

#docs_pathObject



107
108
109
# File 'lib/brief/repository.rb', line 107

def docs_path
  briefcase.docs_path
end

#document_at(path) ⇒ Object



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

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



117
118
119
# File 'lib/brief/repository.rb', line 117

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

#documentsObject



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

def documents
  return @documents if @documents
  load_documents
end

#documents_at(*paths) ⇒ Object



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

def documents_at(*paths)
  paths.compact!

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

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

#documents_at!(*paths) ⇒ Object



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

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



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

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

#load_documentsObject



111
112
113
114
115
# File 'lib/brief/repository.rb', line 111

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

#model_classesObject



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

def model_classes
  all_models.compact.map(&:class).uniq
end

#model_groupsObject



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

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

#models_at(*paths) ⇒ Object



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

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

#normalize_path(p) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/brief/repository.rb', line 67

def normalize_path(p)
  p = p.to_s
  p = p + '.md' unless p.match(/\.\w+$/)

  docs_path.join(p).tap do |normalized|
    if normalized.to_s.split("/").length < docs_path.realpath.to_s.split("/").length
      raise InvalidPath
    end
  end
end

#order_by(*args) ⇒ Object



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

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

#purge(model_type = nil) ⇒ Object



144
145
146
147
148
# File 'lib/brief/repository.rb', line 144

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

#respond_to?(*args) ⇒ Boolean

Returns:

  • (Boolean)


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

def respond_to?(*args)
  meth = args.first
  super || model_groups.include?(meth.to_s)
end

#rootObject



103
104
105
# File 'lib/brief/repository.rb', line 103

def root
  briefcase.root
end

#where(*args) ⇒ Object



95
96
97
# File 'lib/brief/repository.rb', line 95

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