lib/brief/model.rb in brief-1.0.0 vs lib/brief/model.rb in brief-1.1.0

- old
+ new

@@ -11,13 +11,14 @@ include Virtus.model(finalize: false) include Initializers include AccessorMethods include Persistence - class_attribute :models, :after_initialization_hooks, :defined_helpers + class_attribute :models, :after_initialization_hooks, :defined_actions self.models = Array(self.models).to_set + self.defined_actions = Array(self.defined_actions).to_set class << self include Enumerable end @@ -67,25 +68,11 @@ table[type_alias] end def self.finalize Virtus.finalize - - classes.each do |klass| - klass.name ||= klass.to_s.split('::').last.humanize - klass.type_alias ||= klass.name.parameterize.gsub(/-/,'_') - - klass.attribute_set.map(&:name).each do |attr| - klass.define_singleton_method("find_by_#{ attr }") do |value| - where(attr => value).first - end - end - - klass.definition.apply_config - end - - Brief::Repository.define_document_finder_methods + classes.each(&:finalize) end def ==(other) self.path == other.path end @@ -93,28 +80,39 @@ def extract_content(options={}) document.extract_content(options) end module ClassMethods - def where(*args, &block) - Brief::DocumentMapper::Query.new(self).send(:where, *args) + def has_actions? + definition.has_actions? end - def each(*args, &block) - Array(self.models).send(:each, *args, &block) - end + def finalize + klass = self - def meta(options={}, &block) - definition.send(:meta, options, &block) + 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 - def content(options={}, &block) - definition.send(:content, options, &block) + def where(*args, &block) + Brief::DocumentMapper::Query.new(self).send(:where, *args) end - def actions(&block) - definition.send(:helpers, &block) + def each(*args, &block) + Array(self.models).send(:each, *args, &block) end def after_initialize(&block) (self.after_initialization_hooks ||= []).push(block) end @@ -142,10 +140,13 @@ def definition=(value) @definition end def method_missing(meth, *args, &block) - if meth.to_s.match(/^on_(.*)_change$/) + if %w(meta content actions helpers).include?(meth.to_s) + definition.send(meth, &block) + finalize + elsif meth.to_s.match(/^on_(.*)_change$/) create_change_handler($1, *args, &block) else super end end