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



227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/brief/model.rb', line 227

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



176
177
178
# File 'lib/brief/model.rb', line 176

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

#type_aliasObject



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

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

Instance Method Details

#after_initialize(&block) ⇒ Object



170
171
172
# File 'lib/brief/model.rb', line 170

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

#attribute_namesObject



214
215
216
# File 'lib/brief/model.rb', line 214

def attribute_names
  attribute_set.map(&:name)
end

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



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

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

#defined_actionsObject



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

def defined_actions
  definition.defined_actions ||= []
end

#definitionObject



186
187
188
# File 'lib/brief/model.rb', line 186

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

#definition=(_value) ⇒ Object



190
191
192
# File 'lib/brief/model.rb', line 190

def definition=(_value)
  @definition
end

#each(*args, &block) ⇒ Object



166
167
168
# File 'lib/brief/model.rb', line 166

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

#example_body(*args) ⇒ Object



223
224
225
# File 'lib/brief/model.rb', line 223

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

#finalizeObject



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/brief/model.rb', line 143

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



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/brief/model.rb', line 202

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)


135
136
137
# File 'lib/brief/model.rb', line 135

def has_actions?
  definition.has_actions?
end

#purgeObject



111
112
113
# File 'lib/brief/model.rb', line 111

def purge
  models.reject! {|model| !model.document.path.exist? }
end

#section_mapping(*args) ⇒ Object



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

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

#section_mappings(*args) ⇒ Object



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

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

#template_body(*args) ⇒ Object



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

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

#to_schemaObject



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# File 'lib/brief/model.rb', line 115

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



162
163
164
# File 'lib/brief/model.rb', line 162

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