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



239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/brief/model.rb', line 239

def method_missing(meth, *args, &block)
  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



188
189
190
# File 'lib/brief/model.rb', line 188

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

#type_aliasObject



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

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

Instance Method Details

#==(other) ⇒ Object



119
120
121
# File 'lib/brief/model.rb', line 119

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

#accessor_property_namesObject



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

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

#after_initialize(&block) ⇒ Object



182
183
184
# File 'lib/brief/model.rb', line 182

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

#attribute_namesObject



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

def attribute_names
  attribute_set.map(&:name)
end

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



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

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

#defined_actionsObject



151
152
153
# File 'lib/brief/model.rb', line 151

def defined_actions
  definition.defined_actions ||= []
end

#definitionObject



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

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

#definition=(_value) ⇒ Object



202
203
204
# File 'lib/brief/model.rb', line 202

def definition=(_value)
  @definition
end

#each(*args, &block) ⇒ Object



178
179
180
# File 'lib/brief/model.rb', line 178

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

#example_body(*args) ⇒ Object



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

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

#finalizeObject



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

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



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

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)


147
148
149
# File 'lib/brief/model.rb', line 147

def has_actions?
  definition.has_actions?
end

#section_mapping(*args) ⇒ Object



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

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

#section_mappings(*args) ⇒ Object



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

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

#template_body(*args) ⇒ Object



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

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

#to_schemaObject



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/brief/model.rb', line 127

def to_schema
  {
    schema: {
      content: definition.content_schema,
      metadata: definition.,
    },
    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



174
175
176
# File 'lib/brief/model.rb', line 174

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