Sha256: 17962583349eaf48dffedec96b699a886ff4f77fde9f77c663bc7e5439077fab
Contents?: true
Size: 1.15 KB
Versions: 2
Compression:
Stored size: 1.15 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 using HashRefinements 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
2 entries across 2 versions & 1 rubygems
Version | Path |
---|---|
proforma-1.0.1 | lib/proforma/type_factory.rb |
proforma-1.0.0 | lib/proforma/type_factory.rb |