lib/brief/dsl.rb in brief-1.9.4 vs lib/brief/dsl.rb in brief-1.9.6

- old
+ new

@@ -8,29 +8,42 @@ def view(name, &block) Brief.views[name.to_sym] = block end - def define(*args, &block) + def extend(*args, &block) options = args.dup.extract_options! name = args.first type_alias = options.fetch(:type_alias) { name.downcase.parameterize.gsub(/-/, '_') } namespace = Brief.configuration.model_namespace || Brief::Model klass = namespace.const_get(type_alias.camelize) rescue nil - if klass - # raise class already defined + if !klass + return define(*args, &block) end - klass ||= namespace.const_set(type_alias.camelize, Class.new).tap do |k| + klass.definition.instance_eval(&block) if block_given? + klass.definition.validate! + end + + def define(*args, &block) + options = args.dup.extract_options! + name = args.first + type_alias = options.fetch(:type_alias) { name.downcase.parameterize.gsub(/-/, '_') } + + namespace = Brief.configuration.model_namespace || Brief::Model + + klass = namespace.const_get(type_alias.camelize) rescue nil + + klass = namespace.const_set(type_alias.camelize, Class.new).tap do |k| k.send(:include, Brief::Model) k.definition ||= Brief::Model::Definition.new(args.first, args.extract_options!) k.name ||= name k.type_alias ||= type_alias Brief::Model.classes << k - end + end if klass.nil? klass.definition.instance_eval(&block) if block_given? klass.definition.validate! end