class SimpleFactory::Factory

Public Class Methods

create(params = {}) click to toggle source
# File lib/simple_factory/factory.rb, line 7
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 Class Methods

after_create(&block) click to toggle source
# File lib/simple_factory/factory.rb, line 29
def after_create(&block)
  @after_create_hooks << block
end
define(path = nil, &block) click to toggle source
# File lib/simple_factory/factory.rb, line 17
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
inherited(subclass) click to toggle source
# File lib/simple_factory/factory.rb, line 33
def inherited(subclass)
  subclass.class_eval do
    @definition_samples = []
    @after_create_hooks = []
  end
end