Sha256: 73c5526603650571b5b0ba30769929eea10c73194aeb6b637c7fddbdaa5f3f06

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 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
  module Modeling
    # This can be used as a super-class for models which are basically containers for
    # simple arrays of elements
    class GenericContainer
      def initialize(child_key, child_class, children = [])
        raise ArgumentError, 'child_key is required'   unless child_key
        raise ArgumentError, 'child_class is required' unless child_class

        @child_class = child_class
        @child_key   = child_key.to_s.to_sym
        @children    = child_class.array(children)
      end

      def respond_to_missing?(method_name, include_private = false)
        method_name.to_s == child_key.to_s ||
          method_name.to_s == child_setter_name ||
          super
      end

      def method_missing(method_name, *args, &block)
        if method_name.to_s == child_key.to_s
          Array(children)
        elsif method_name.to_s == child_setter_name
          @children = args
        else
          super
        end
      end

      def compile(data, evaluator)
        compiled_children = children.map { |child| child.compile(data, evaluator) }

        args = {}.tap { |hash| hash[child_key] = compiled_children }

        self.class.new(args)
      end

      private

      attr_reader :child_class, :child_key, :children

      def child_setter_name
        "#{child_key}="
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
proforma-1.0.0.pre.alpha lib/proforma/modeling/generic_container.rb