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
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/brief/model.rb', line 181
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 meth.to_s.match(/^on_(.*)_change$/)
create_change_handler(Regexp.last_match[1], *args, &block)
else
super
end
end
|
Instance Attribute Details
#name ⇒ Object
130
131
132
|
# File 'lib/brief/model.rb', line 130
def name
@name || to_s.split('::').last.underscore.gsub('_', ' ').titlecase
end
|
#type_alias ⇒ Object
136
137
138
|
# File 'lib/brief/model.rb', line 136
def type_alias
@type_alias || name.parameterize.gsub(/-/, '_')
end
|
Instance Method Details
#after_initialize(&block) ⇒ Object
124
125
126
|
# File 'lib/brief/model.rb', line 124
def after_initialize(&block)
(self.after_initialization_hooks ||= []).push(block)
end
|
#attribute_names ⇒ Object
168
169
170
|
# File 'lib/brief/model.rb', line 168
def attribute_names
attribute_set.map(&:name)
end
|
#create_change_handler(_attribute, *_args, &block) ⇒ Object
192
193
194
|
# File 'lib/brief/model.rb', line 192
def create_change_handler(_attribute, *_args, &block)
block.call(self)
end
|
#definition ⇒ Object
140
141
142
|
# File 'lib/brief/model.rb', line 140
def definition
@definition ||= Brief::Model::Definition.new(name, type_alias: type_alias, model_class: self)
end
|
#definition=(_value) ⇒ Object
144
145
146
|
# File 'lib/brief/model.rb', line 144
def definition=(_value)
@definition
end
|
#each(*args, &block) ⇒ Object
120
121
122
|
# File 'lib/brief/model.rb', line 120
def each(*args, &block)
Array(models).send(:each, *args, &block)
end
|
#example_body(*args) ⇒ Object
177
178
179
|
# File 'lib/brief/model.rb', line 177
def example_body(*args)
definition.send(:example_body, *args).to_s.strip
end
|
#finalize ⇒ Object
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
# File 'lib/brief/model.rb', line 97
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
156
157
158
159
160
161
162
163
164
165
166
|
# File 'lib/brief/model.rb', line 156
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
93
94
95
|
# File 'lib/brief/model.rb', line 93
def has_actions?
definition.has_actions?
end
|
#section_mapping(*args) ⇒ Object
148
149
150
|
# File 'lib/brief/model.rb', line 148
def section_mapping(*args)
definition.send(:section_mapping, *args)
end
|
#section_mappings(*args) ⇒ Object
152
153
154
|
# File 'lib/brief/model.rb', line 152
def section_mappings(*args)
definition.send(:section_mappings, *args)
end
|
#template_body(*args) ⇒ Object
172
173
174
175
|
# File 'lib/brief/model.rb', line 172
def template_body(*args)
res = definition.send(:template_body, *args)
res.to_s.length == 0 ? example_body : res.to_s.strip
end
|
#where(*args, &_block) ⇒ Object
116
117
118
|
# File 'lib/brief/model.rb', line 116
def where(*args, &_block)
Brief::DocumentMapper::Query.new(self).send(:where, *args)
end
|