Class: Brief::Briefcase

Inherits:
Object
  • Object
show all
Includes:
Documentation, DSL
Defined in:
lib/brief/briefcase.rb,
lib/brief/briefcase/initializer.rb,
lib/brief/briefcase/documentation.rb

Defined Under Namespace

Modules: Documentation Classes: Initializer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Documentation

#render_documentation, #schema_map

Methods included from DSL

#action, #define, #extend, #view

Constructor Details

#initialize(options = {}) ⇒ Briefcase

Returns a new instance of Briefcase.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/brief/briefcase.rb', line 9

def initialize(options = {})
  @options = options.to_mash

  load_configuration
  use(:app, options[:app]) if options[:app]

  load_model_definitions

  if Brief.case(false).nil?
    Brief.case = self
  end

  Brief.cases[root.basename.to_s] ||= self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



281
282
283
284
285
286
287
288
289
290
# File 'lib/brief/briefcase.rb', line 281

def method_missing(meth, *args, &block)
  if Brief.views.key?(meth.to_sym)
    block = Brief.views[meth.to_sym]
    block.call(self, args.extract_options!)
  elsif repository.respond_to?(meth)
    repository.send(meth, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#model_definitionsObject (readonly)

Returns the value of attribute model_definitions.



6
7
8
# File 'lib/brief/briefcase.rb', line 6

def model_definitions
  @model_definitions
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/brief/briefcase.rb', line 6

def options
  @options
end

Class Method Details

.create_new_briefcase(options = {}) ⇒ Object



292
293
294
# File 'lib/brief/briefcase.rb', line 292

def self.create_new_briefcase(options={})
  Brief::Briefcase::Initializer.new(options).run
end

Instance Method Details

#app_config_pathObject



177
178
179
# File 'lib/brief/briefcase.rb', line 177

def app_config_path
  uses_app? && app_path.join("config.rb")
end

#app_modelsObject



189
190
191
# File 'lib/brief/briefcase.rb', line 189

def app_models
  app_namespace.constants.map {|c| app_namespace.const_get(c) }
end

#app_models_folderObject



181
182
183
# File 'lib/brief/briefcase.rb', line 181

def app_models_folder
  uses_app? && app_path.join("models")
end

#app_namespaceObject



185
186
187
# File 'lib/brief/briefcase.rb', line 185

def app_namespace
  Brief::Apps.find_namespace(options[:app])
end

#app_pathObject



173
174
175
# File 'lib/brief/briefcase.rb', line 173

def app_path
  uses_app? && Brief::Apps.path_for(options[:app]).to_pathname
end

#as_default(params = {}) ⇒ Object

TODO The serialization of an entire briefcase at once is important enough to be its own module



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/brief/briefcase.rb', line 75

def as_default(params={})
  params.symbolize_keys!

  base = info_hash

  if params[:include_data] || params[:data]
    base[:data] = data.as_json
  end

  if params[:include_schema] || params[:schema]
    base[:schema] = schema_map
  end

  if params[:include_documentation] || params[:documentation]
    base[:documentation] = render_documentation
  end

  if params[:include_models] || params[:models]
    model_settings = {
      docs_path: docs_path
    }

    %w(urls content rendered attachments).each do |opt|
      model_settings[opt.to_sym] = !!(params[opt.to_sym] || params["include_#{opt}".to_sym])
    end

    all = all_models.compact

    base[:models] = all.map do |m|
      m.document.refresh! if params[:refresh_models]
      m.as_json(model_settings)
    end
  end

  base
end

#as_full_export(options = {}) ⇒ Object



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

def as_full_export(options={})
  options.reverse_merge!(content: true,
                         rendered: true,
                         models: true,
                         schema: true,
                         documentation: true,
                         attachments: true)
  as_default(options)
end

#assets_pathObject



231
232
233
# File 'lib/brief/briefcase.rb', line 231

def assets_path
  root.join(options.fetch(:assets_path) { config.assets_path }).expand_path
end

#assets_trailObject



243
244
245
246
247
248
# File 'lib/brief/briefcase.rb', line 243

def assets_trail
  @assets_trail ||= Hike::Trail.new(assets_path).tap do |trail|
    trail.append_extensions '.svg', '.png', '.pdf', '.jpg', '.gif', '.mov'
    assets_path.children.select(&:directory?).each {|dir| trail.prepend_path(assets_path); trail.append_path(assets_path.join(dir)) }
  end
end

#cache_keyObject



35
36
37
# File 'lib/brief/briefcase.rb', line 35

def cache_key
  "#{slug}:#{repository.cache_key}"
end

#config(&block) ⇒ Object



136
137
138
139
140
# File 'lib/brief/briefcase.rb', line 136

def config(&block)
  Brief::Configuration.instance.tap do |cfg|
    cfg.instance_eval(&block) if block.respond_to?(:call)
  end
end

#dataObject



128
129
130
# File 'lib/brief/briefcase.rb', line 128

def data
  @data ||= data!
end

#data!Object



132
133
134
# File 'lib/brief/briefcase.rb', line 132

def data!
  @data = Brief::Data::Wrapper.new(root: data_path)
end

#data_pathObject



239
240
241
# File 'lib/brief/briefcase.rb', line 239

def data_path
  root.join(options.fetch(:data_path) { config.data_path }).expand_path
end

#data_trailObject



258
259
260
261
262
263
# File 'lib/brief/briefcase.rb', line 258

def data_trail
  @docs_trail ||= Hike::Trail.new(data_path).tap do |trail|
    trail.append_extensions '.yaml', '.js', '.json', '.xls', '.xlsx', '.csv', '.txt'
    trail.append_path(data_path)
  end
end

#docs_pathObject



235
236
237
# File 'lib/brief/briefcase.rb', line 235

def docs_path
  root.join(options.fetch(:docs_path) { config.docs_path }).expand_path
end

#docs_trailObject



250
251
252
253
254
255
# File 'lib/brief/briefcase.rb', line 250

def docs_trail
  @docs_trail ||= Hike::Trail.new(docs_path).tap do |trail|
    trail.append_extensions '.md', '.html.md', '.markdown'
    docs_path.children.select(&:directory?).each {|dir| trail.prepend_path(docs_path); trail.append_path(docs_path.join(dir)) }
  end
end

#find_asset(needle) ⇒ Object



226
227
228
229
# File 'lib/brief/briefcase.rb', line 226

def find_asset(needle)
  found = assets_trail.find(needle)
  found && Pathname(found)
end

#folder_nameObject



146
147
148
# File 'lib/brief/briefcase.rb', line 146

def folder_name
  root.basename
end

#generic_model_class_for(document) ⇒ Object



198
199
200
# File 'lib/brief/briefcase.rb', line 198

def generic_model_class_for(document)
  Brief::Model.for_type(document.document_type) || Brief::Model.for_folder_name(document.parent_folder_name)
end

#info_hashObject



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/brief/briefcase.rb', line 54

def info_hash
  {
    BRIEF_VERSION: Brief::VERSION,
    views: Brief.views.keys,
    key: folder_name.to_s.parameterize,
    name: folder_name.to_s.titlecase,
    settings: settings,
    cache_key: cache_key,
    root: root.to_s,
    paths:{
      docs_path: docs_path.to_s,
      assets_path: assets_path.to_s,
      models_path: models_path.to_s,
      data_path: data_path.to_s
    }
  }
end

#load_configurationObject

Loads the configuration for this briefcase, either from the current working directory or the configured path for the configuration file.



152
153
154
155
156
157
158
159
160
161
162
# File 'lib/brief/briefcase.rb', line 152

def load_configuration
  config_path = options.fetch(:config_path) do
    root.join('brief.rb')
  end

  if config_path.is_a?(String)
    config_path = root.join(config_path)
  end

  run(config_path) if config_path.exist?
end

#load_model_definitionsObject



202
203
204
205
206
207
208
209
# File 'lib/brief/briefcase.rb', line 202

def load_model_definitions
  if uses_app?
    Brief.load_modules_from(app_path.join("models"))
  end

  Brief.load_modules_from(models_path) if models_path.exist?
  Brief::Model.finalize
end

#model(name_or_type) ⇒ Object

Returns a model name by its human readable description or its type alias



212
213
214
215
216
217
218
219
220
# File 'lib/brief/briefcase.rb', line 212

def model(name_or_type)
  table = Brief::Model.table

  table.fetch(name_or_type) do
    table.values.find do |k|
      k.name == name_or_type
    end
  end
end

#model_class_for(document) ⇒ Object



193
194
195
196
# File 'lib/brief/briefcase.rb', line 193

def model_class_for(document)
  return generic_model_class_for(document) unless uses_app?
  app_models.find {|k| k.type_alias == document.document_type }
end

#models_pathObject



265
266
267
268
269
270
271
272
273
274
275
# File 'lib/brief/briefcase.rb', line 265

def models_path
  value = options.fetch(:models_path) { config.models_path }

  if value.to_s.match(/\./)
    Pathname(Brief.pwd).join(value)
  elsif value.to_s.match(/\//)
    Pathname(value)
  else
    root.join(value)
  end
end

#present(style = "default", params = {}) ⇒ Object



24
25
26
27
28
29
30
31
32
33
# File 'lib/brief/briefcase.rb', line 24

def present(style="default", params={})
  style = "default" if style.nil?

  if respond_to?("as_#{style}")
    send("as_#{style}", params)
  elsif Brief.views.key?(style.to_sym)
    block = Brief.views[style.to_sym]
    block.call(self, params)
  end
end

#repositoryObject



277
278
279
# File 'lib/brief/briefcase.rb', line 277

def repository
  @repository ||= Brief::Repository.new(self, options)
end

#rootObject



222
223
224
# File 'lib/brief/briefcase.rb', line 222

def root
  Pathname(options.fetch(:root) { Brief.pwd }).expand_path
end

#run(code_or_file) ⇒ Object



164
165
166
167
# File 'lib/brief/briefcase.rb', line 164

def run(code_or_file)
  code = code_or_file.is_a?(Pathname) ? code_or_file.read : code
  instance_eval(code) rescue nil
end

#server(options = {}) ⇒ Object



142
143
144
# File 'lib/brief/briefcase.rb', line 142

def server(options={})
  @server ||= Brief::Server.new(self, options)
end

#settingsObject



43
44
45
# File 'lib/brief/briefcase.rb', line 43

def settings
  @settings ||= settings!
end

#settings!Object



47
48
49
50
51
52
# File 'lib/brief/briefcase.rb', line 47

def settings!
  if root.join("settings.yml").exist?
    y = YAML.load(root.join("settings.yml").read) rescue nil
    (y || {}).to_mash
  end
end

#slugObject



39
40
41
# File 'lib/brief/briefcase.rb', line 39

def slug
  options.fetch(:slug) { root.basename.to_s.parameterize }
end

#use(module_type = :app, module_id) ⇒ Object



122
123
124
125
126
# File 'lib/brief/briefcase.rb', line 122

def use(module_type=:app, module_id)
  options[:app] = module_id.to_s

  run(app_config_path) if app_path.try(&:exist?)
end

#uses_app?Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/brief/briefcase.rb', line 169

def uses_app?
  options.key?(:app) && Brief::Apps.available?(options[:app].to_s)
end