lib/kouba/simple_factory.rb in kouba-0.1.1 vs lib/kouba/simple_factory.rb in kouba-0.2.0
- old
+ new
@@ -1,22 +1,25 @@
require "dry/inflector"
module Kouba
module SimpleFactory
def self.included(mod)
- mod.send(:module_function, :create)
- mod.send(:module_function, :find)
+ mod.send(:module_function, :create, :find, :name_key)
end
+ def name_key(key)
+ @name_key = key
+ end
+
def find(name)
inflector = Dry::Inflector.new
children = constants.collect { |c| const_get(c) }.compact
children.find do |child|
inflector.underscore(child.name.split('::').last) == name
end
end
def create(config)
- (find(config[:name])).new(config[:options])
+ (find(config[@name_key ? @name_key.to_sym : :name])).new(config[:options])
end
end
end