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



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
# File 'lib/brief/model.rb', line 305

def method_missing(meth, *args, &block)
  # these methods when called at a class level inside of
  # a class definition body will modify the schema settings for that model
  if %w(meta content template example actions helpers).include?(meth.to_s)
    definition.send(meth, *args, &block)
    finalize
  # these methods allow the model class to inspect itself and report
  # whatever methods and actions are defined on the model class
  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



246
247
248
# File 'lib/brief/model.rb', line 246

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

#type_aliasObject



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

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

Instance Method Details

#==(other) ⇒ Object



123
124
125
# File 'lib/brief/model.rb', line 123

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

#accessor_property_namesObject



127
128
129
# File 'lib/brief/model.rb', line 127

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

#after_initialize(&block) ⇒ Object



240
241
242
# File 'lib/brief/model.rb', line 240

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

#attribute_namesObject



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

def attribute_names
  attribute_set.map(&:name)
end

#content_schema_summaryObject



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

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



322
323
324
# File 'lib/brief/model.rb', line 322

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

#defined_actionsObject



209
210
211
# File 'lib/brief/model.rb', line 209

def defined_actions
  definition.defined_actions ||= []
end

#defined_in(*args) ⇒ Object



301
302
303
# File 'lib/brief/model.rb', line 301

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

#definitionObject



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

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

#definition=(_value) ⇒ Object



260
261
262
# File 'lib/brief/model.rb', line 260

def definition=(_value)
  @definition
end

#documentation(*args) ⇒ Object



297
298
299
# File 'lib/brief/model.rb', line 297

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

#each(*args, &block) ⇒ Object



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

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

#example_body(*args) ⇒ Object



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

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

#finalizeObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
# File 'lib/brief/model.rb', line 213

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



272
273
274
275
276
277
278
279
280
281
282
# File 'lib/brief/model.rb', line 272

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)


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

def has_actions?
  definition.has_actions?
end

#metadata_schema_summaryObject



165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/brief/model.rb', line 165

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

#section_mapping(*args) ⇒ Object



264
265
266
# File 'lib/brief/model.rb', line 264

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

#section_mappings(*args) ⇒ Object



268
269
270
# File 'lib/brief/model.rb', line 268

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

#template_body(*args) ⇒ Object



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

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

#to_documentationObject



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/brief/model.rb', line 131

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
    }
  else
    { }
  end
end

#to_schemaObject



183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
# File 'lib/brief/model.rb', line 183

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: definition.example_body.to_s,
    template: definition.template_body.to_s,
    urls: {
      browse_url: "browse/#{ type_alias.to_s.pluralize }",
      schema_url: "schema/#{ type_alias }"
    }
  }
end

#where(*args, &_block) ⇒ Object



232
233
234
# File 'lib/brief/model.rb', line 232

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