Sha256: b449c84850beb977def1a21e10af505793b59a8fd461a08b6dad6ad8eb8ca440

Contents?: true

Size: 574 Bytes

Versions: 1

Compression:

Stored size: 574 Bytes

Contents

require "dry/inflector"

module Kouba
  module SimpleFactory
    def self.included(mod)
      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_key ? @name_key.to_sym : :name])).new(config[:options] || {})
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
kouba-0.2.1 lib/kouba/simple_factory.rb