Class: Brief::Briefcase

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

Defined Under Namespace

Classes: Initializer

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from DSL

#action, #define, #view

Constructor Details

#initialize(options = {}) ⇒ Briefcase

Returns a new instance of Briefcase.



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

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

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

  load_model_definitions

  if Brief.case.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



225
226
227
228
229
230
231
232
233
234
# File 'lib/brief/briefcase.rb', line 225

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.



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

def model_definitions
  @model_definitions
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

Class Method Details

.create_new_briefcase(options = {}) ⇒ Object



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

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

Instance Method Details

#app_config_pathObject



152
153
154
# File 'lib/brief/briefcase.rb', line 152

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

#app_modelsObject



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

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

#app_models_folderObject



156
157
158
# File 'lib/brief/briefcase.rb', line 156

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

#app_namespaceObject



160
161
162
# File 'lib/brief/briefcase.rb', line 160

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

#app_pathObject



148
149
150
# File 'lib/brief/briefcase.rb', line 148

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

#as_default(params = {}) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/brief/briefcase.rb', line 59

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

  model_settings = {
    docs_path: docs_path
  }

  model_settings[:rendered] = !!(params.key?(:rendered))
  model_settings[:content] = !!(params.key?(:content))

  all = all_models.compact

  schema = schema_map
  models = all.map {|m| m.as_json(model_settings) }

  {
    views: Brief.views.keys,
    key: briefcase.folder_name.to_s.parameterize,
    name: briefcase.folder_name.to_s.titlecase,
    schema: schema,
    models: models,
    settings: settings,
    cache_key: briefcase.cache_key
  }.tap do |hash|
    hash[:table_of_contents] = briefcase.table_of_contents.as_json() rescue {}
  end
end

#as_full_exportObject



87
88
89
# File 'lib/brief/briefcase.rb', line 87

def as_full_export
  as_default(content: true, rendered: true)
end

#cache_keyObject



32
33
34
# File 'lib/brief/briefcase.rb', line 32

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

#config(&block) ⇒ Object



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

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

#dataObject



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

def data
  @data ||= data!
end

#data!Object



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

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

#data_pathObject



205
206
207
# File 'lib/brief/briefcase.rb', line 205

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

#docs_pathObject



201
202
203
# File 'lib/brief/briefcase.rb', line 201

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

#folder_nameObject



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

def folder_name
  root.basename
end

#generic_model_class_for(document) ⇒ Object



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

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

#load_configurationObject

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



127
128
129
130
131
132
133
134
135
136
137
# File 'lib/brief/briefcase.rb', line 127

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



177
178
179
180
181
182
183
184
# File 'lib/brief/briefcase.rb', line 177

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



187
188
189
190
191
192
193
194
195
# File 'lib/brief/briefcase.rb', line 187

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



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

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



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

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



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

def present(style="default", params={})
  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



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

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

#rootObject



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

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

#run(code_or_file) ⇒ Object



139
140
141
142
# File 'lib/brief/briefcase.rb', line 139

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

#schema_map(include_all = false) ⇒ Object



97
98
99
100
101
# File 'lib/brief/briefcase.rb', line 97

def schema_map(include_all=false)
  list = include_all ? Brief::Model.classes : model_classes
  list.map(&:to_schema)
    .reduce({}) {|m, k| m[k[:type_alias]] = k; m }
end

#server(options = {}) ⇒ Object



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

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

#settingsObject



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

def settings
  @settings ||= settings!
end

#settings!Object



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

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

#slugObject



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

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

#table_of_contentsObject



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

def table_of_contents
  table_of_contents_document.to_model
end

#table_of_contents_documentObject



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

def table_of_contents_document
  Brief::Document.new(docs_path.join("index.md"), document_type: "outline")
end

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



91
92
93
94
95
# File 'lib/brief/briefcase.rb', line 91

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)


144
145
146
# File 'lib/brief/briefcase.rb', line 144

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