Sha256: 8fcd2d5a9d38f9ee6eed88aa217bea8df47eee3e6da39665b7331f10ad836305
Contents?: true
Size: 1.12 KB
Versions: 1
Compression:
Stored size: 1.12 KB
Contents
# frozen_string_literal: true # # Copyright (c) 2019-present, Blue Marble Payroll, LLC # # This source code is licensed under the MIT license found in the # LICENSE file in the root directory of this source tree. # module Proforma # A TypeFactory object understands how to build objects using a special designated '$type' key. class TypeFactory DEFAULT_TYPE_KEY = :type attr_reader :registry, :type_key def initialize(registry = {}, type_key = DEFAULT_TYPE_KEY) @registry = registry.symbolize_keys @type_key = type_key freeze end def array(objects = []) objects = objects.is_a?(Hash) ? [objects] : Array(objects) objects.map do |object| object.is_a?(Hash) ? make(object) : object end end def make(config = {}) config = (config || {}).symbolize_keys type = config[type_key].to_s.to_sym object_class = registry[type] raise ArgumentError, "cannot find section from type: #{type}" unless object_class config_without_type = config.reject { |k| k == type_key } object_class.new(config_without_type) end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
proforma-1.0.0.pre.alpha | lib/proforma/type_factory.rb |