Sha256: ca7c058f3eb8bbdeeeafaf1b0c4429b6096b49b4b850e3adf320599fa6df288a
Contents?: true
Size: 1.14 KB
Versions: 1
Compression:
Stored size: 1.14 KB
Contents
require 'simple_factory/definition' require 'simple_factory/dsl' module SimpleFactory class Factory class << self def create(params = {}) definitions = (@definition_samples.sample || []) default_params = definitions.map {|d| [d.name, d.value] }.to_h merged_params = default_params.merge(params) self.new.create(merged_params).tap do |model| @after_create_hooks.each {|hook| hook.call(model, merged_params) } end end private def define(path = nil, &block) if path yaml = YAML.load_file(File.expand_path(path, SimpleFactory.definitions_dir)) hash_def = HashDefinitions.new(yaml) @definition_samples = hash_def.samples else dsl = DSL.new dsl.instance_eval(&block) @definition_samples = [dsl.definitions] end end def after_create(&block) @after_create_hooks << block end def inherited(subclass) subclass.class_eval do @definition_samples = [] @after_create_hooks = [] end end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
simple_factory-0.1.0 | lib/simple_factory/factory.rb |