Module: Brief::Model::ClassMethods

Defined in:
lib/brief/model.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
# File 'lib/brief/model.rb', line 387

def method_missing(meth, *args, &block)
  # these methods have a special effect on the behavior of the
  # model definition.  we need to make sure we call finalize after
  # them
  if %w(meta content template example actions helpers).include?(meth.to_s)
    definition.send(meth, *args, &block)
    finalize
  elsif %w(defined_helper_methods defined_actions).include?(meth.to_s)
    definition.send(meth)
  elsif meth.to_s.match(/^on_(.*)_change$/)
    create_change_handler(Regexp.last_match[1], *args, &block)
  else
    super
  end
end

Instance Attribute Details

#nameObject



293
294
295
# File 'lib/brief/model.rb', line 293

def name
  @name || to_s.split('::').last.underscore.gsub('_', ' ').titlecase
end

#type_aliasObject



299
300
301
# File 'lib/brief/model.rb', line 299

def type_alias
  @type_alias || name.parameterize.gsub(/-/, '_')
end

Instance Method Details

#==(other) ⇒ Object



145
146
147
# File 'lib/brief/model.rb', line 145

def ==(other)
  type_alias && type_alias == other.type_alias
end

#accessor_property_namesObject



149
150
151
# File 'lib/brief/model.rb', line 149

def accessor_property_names
  (definition.content_schema.attributes.keys + definition..keys).uniq
end

#after_initialize(&block) ⇒ Object



287
288
289
# File 'lib/brief/model.rb', line 287

def after_initialize(&block)
  (self.after_initialization_hooks ||= []).push(block)
end

#attribute_namesObject



331
332
333
# File 'lib/brief/model.rb', line 331

def attribute_names
  attribute_set.map(&:name)
end

#content_schema_summaryObject



178
179
180
181
182
183
184
185
186
187
188
# File 'lib/brief/model.rb', line 178

def content_schema_summary
  base = definition.content_schema.attributes

  base.keys.inject({}) do |memo, key|
    val = base[key]
    args = Array(val[:args])
    first = args.first
    memo[key] = first if first
    memo
  end
end

#create_change_handler(_attribute, *_args, &block) ⇒ Object



403
404
405
# File 'lib/brief/model.rb', line 403

def create_change_handler(_attribute, *_args, &block)
  block.call(self)
end

#defined_actionsObject



256
257
258
# File 'lib/brief/model.rb', line 256

def defined_actions
  definition.defined_actions ||= []
end

#defined_in(*args) ⇒ Object



368
369
370
# File 'lib/brief/model.rb', line 368

def defined_in(*args)
  definition.send(:defined_in, *args)
end

#definitionObject



303
304
305
# File 'lib/brief/model.rb', line 303

def definition
  @definition ||= Brief::Model::Definition.new(name, type_alias: type_alias, model_class: self)
end

#definition=(_value) ⇒ Object



307
308
309
# File 'lib/brief/model.rb', line 307

def definition=(_value)
  @definition
end

#documentation(*args) ⇒ Object



364
365
366
# File 'lib/brief/model.rb', line 364

def documentation(*args)
  definition.send(:documentation, *args)
end

#documentation_contentObject



224
225
226
227
228
# File 'lib/brief/model.rb', line 224

def documentation_content
  if documentation_path && documentation_path.exist?
    return documentation_path.read.to_s
  end
end

#documentation_path(*args) ⇒ Object



382
383
384
385
# File 'lib/brief/model.rb', line 382

def documentation_path(*args)
  definition.send(:documentation_path=, *args) unless args.empty?
  definition.send(:documentation_path)
end

#each(*args, &block) ⇒ Object



283
284
285
# File 'lib/brief/model.rb', line 283

def each(*args, &block)
  Array(models).send(:each, *args, &block)
end

#example_body(*args) ⇒ Object



340
341
342
# File 'lib/brief/model.rb', line 340

def example_body(*args)
  definition.send(:example_body, *args).to_s.strip
end

#example_contentObject



208
209
210
211
212
213
214
# File 'lib/brief/model.rb', line 208

def example_content
  if example_path && example_path.exist?
    return example_path.read.to_s
  end

  definition.example_body.to_s
end

#example_path(*args) ⇒ Object



377
378
379
380
# File 'lib/brief/model.rb', line 377

def example_path(*args)
  definition.send(:example_path=, *args) unless args.empty?
  definition.send(:example_path)
end

#finalizeObject



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/brief/model.rb', line 260

def finalize
  klass = self

  klass.name ||= klass.to_s.split('::').last.humanize
  klass.type_alias ||= klass.name.parameterize.gsub(/-/, '_')

  klass.attribute_set.map(&:name).each do |attr|
    unless klass.method_defined?("find_by_#{ attr }")
      klass.define_singleton_method("find_by_#{ attr }") do |value|
        where(attr => value).first
      end
    end
  end

  klass.definition.apply_config

  Brief::Repository.define_document_finder_methods
end

#generate_template_content_from(object = {}, include_frontmatter = true) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
# File 'lib/brief/model.rb', line 319

def generate_template_content_from(object={}, include_frontmatter = true)
  @erb ||= ERB.new(template_body)
  content = @erb.result(binding)
  frontmatter = object.slice(*attribute_names)

  base = ''
  base += frontmatter.to_hash.to_yaml + "---\n" if include_frontmatter
  base += content

  base
end

#has_actions?Boolean

Returns:

  • (Boolean)


252
253
254
# File 'lib/brief/model.rb', line 252

def has_actions?
  definition.has_actions?
end

#metadata_schema_summaryObject



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'lib/brief/model.rb', line 190

def 
  base = definition.

  base.keys.inject({}) do |memo, key|
    val = base[key]
    args = Array(val[:args])
    first = args.first.to_s

    if args.length == 1 && first == key.to_s
      memo[key] = "string"
    elsif args.length >= 2
      memo[key] = args.last
    end

    memo
  end
end

#new_doc_name(&block) ⇒ Object



354
355
356
357
358
359
360
361
362
# File 'lib/brief/model.rb', line 354

def new_doc_name(&block)
  if block
    definition.new_doc_name_block = block
  elsif definition.new_doc_name_block
    definition.new_doc_name_block.call
  else
    "#{ self.type_alias }-#{ DateTime.now.strftime("%Y-%m-%d") }.md"
  end
end

#new_doc_template(&block) ⇒ Object



344
345
346
347
348
349
350
351
352
# File 'lib/brief/model.rb', line 344

def new_doc_template(&block)
  if block
    definition.new_doc_template_block = block
  elsif definition.new_doc_template_block
    definition.new_doc_template_block.call
  else
    example_content
  end
end

#section_mapping(*args) ⇒ Object



311
312
313
# File 'lib/brief/model.rb', line 311

def section_mapping(*args)
  definition.send(:section_mapping, *args)
end

#section_mappings(*args) ⇒ Object



315
316
317
# File 'lib/brief/model.rb', line 315

def section_mappings(*args)
  definition.send(:section_mappings, *args)
end

#template_body(*args) ⇒ Object



335
336
337
338
# File 'lib/brief/model.rb', line 335

def template_body(*args)
  res = definition.send(:template_body, *args)
  res.to_s.length == 0 ? example_body : res.to_s.strip
end

#template_contentObject



216
217
218
219
220
221
222
# File 'lib/brief/model.rb', line 216

def template_content
  if template_path && template_path.exist?
    return template_path.read.to_s
  end

  definition.template_body.to_s
end

#template_path(*args) ⇒ Object



372
373
374
375
# File 'lib/brief/model.rb', line 372

def template_path(*args)
  definition.send(:template_path=, *args) unless args.empty?
  definition.send(:template_path)
end

#to_documentationObject

Looks to see if there is a documentation markdown file for the model and if so, will return the documentation info as a Hash



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/brief/model.rb', line 155

def to_documentation
  docs = definition.documentation

  path = if docs.markdown
           Pathname(docs.markdown)
         elsif defined_in
           basename = defined_in.basename.to_s.gsub(/.rb/,'')
           defined_in.parent.join('..','documentation',"#{basename}.md")
         end

  if path
    model_doc = Brief::Briefcase::Documentation::ModelDoc.new(path)

    {
      content: model_doc.content,
      rendered: model_doc.rendered,
      path: path
    }
  else
    { }
  end
end

#to_schemaObject



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/brief/model.rb', line 230

def to_schema
  {
    schema: {
      content: content_schema_summary,
      metadata: ,
    },
    documentation: to_documentation,
    defined_in: defined_in,
    class_name: to_s,
    type_alias: type_alias,
    name: name,
    group: name.to_s.pluralize,
    actions: defined_actions,
    example: example_content,
    template: template_content,
    urls: {
      browse_url: "browse/#{ type_alias.to_s.pluralize }",
      schema_url: "schema/#{ type_alias }"
    }
  }
end

#where(*args, &_block) ⇒ Object



279
280
281
# File 'lib/brief/model.rb', line 279

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