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
184
185
186
187
188
189
190
191
192
193
194
195
|
# File 'lib/brief/model.rb', line 184
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
#name ⇒ Object
133
134
135
|
# File 'lib/brief/model.rb', line 133
def name
@name || to_s.split('::').last.underscore.gsub('_', ' ').titlecase
end
|
#type_alias ⇒ Object
139
140
141
|
# File 'lib/brief/model.rb', line 139
def type_alias
@type_alias || name.parameterize.gsub(/-/, '_')
end
|
Instance Method Details
#after_initialize(&block) ⇒ Object
127
128
129
|
# File 'lib/brief/model.rb', line 127
def after_initialize(&block)
(self.after_initialization_hooks ||= []).push(block)
end
|
#attribute_names ⇒ Object
171
172
173
|
# File 'lib/brief/model.rb', line 171
def attribute_names
attribute_set.map(&:name)
end
|
#create_change_handler(_attribute, *_args, &block) ⇒ Object
197
198
199
|
# File 'lib/brief/model.rb', line 197
def create_change_handler(_attribute, *_args, &block)
block.call(self)
end
|
#defined_actions ⇒ Object
96
97
98
|
# File 'lib/brief/model.rb', line 96
def defined_actions
definition.defined_actions ||= []
end
|
#definition ⇒ Object
143
144
145
|
# File 'lib/brief/model.rb', line 143
def definition
@definition ||= Brief::Model::Definition.new(name, type_alias: type_alias, model_class: self)
end
|
#definition=(_value) ⇒ Object
147
148
149
|
# File 'lib/brief/model.rb', line 147
def definition=(_value)
@definition
end
|
#each(*args, &block) ⇒ Object
123
124
125
|
# File 'lib/brief/model.rb', line 123
def each(*args, &block)
Array(models).send(:each, *args, &block)
end
|
#example_body(*args) ⇒ Object
180
181
182
|
# File 'lib/brief/model.rb', line 180
def example_body(*args)
definition.send(:example_body, *args).to_s.strip
end
|
#finalize ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
# File 'lib/brief/model.rb', line 100
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
159
160
161
162
163
164
165
166
167
168
169
|
# File 'lib/brief/model.rb', line 159
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
92
93
94
|
# File 'lib/brief/model.rb', line 92
def has_actions?
definition.has_actions?
end
|
#section_mapping(*args) ⇒ Object
151
152
153
|
# File 'lib/brief/model.rb', line 151
def section_mapping(*args)
definition.send(:section_mapping, *args)
end
|
#section_mappings(*args) ⇒ Object
155
156
157
|
# File 'lib/brief/model.rb', line 155
def section_mappings(*args)
definition.send(:section_mappings, *args)
end
|
#template_body(*args) ⇒ Object
175
176
177
178
|
# File 'lib/brief/model.rb', line 175
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
119
120
121
|
# File 'lib/brief/model.rb', line 119
def where(*args, &_block)
Brief::DocumentMapper::Query.new(self).send(:where, *args)
end
|