Sha256: 65d7331e3e34ea55f93070ee1e2ffb6a7aa8288e5e6a10cbe43175d1b6f862db

Contents?: true

Size: 1.49 KB

Versions: 3

Compression:

Stored size: 1.49 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) }

        self.class.new(child_key => compiled_children)
      end

      private

      attr_reader :child_class, :child_key, :children

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

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
proforma-1.0.2 lib/proforma/modeling/generic_container.rb
proforma-1.0.1 lib/proforma/modeling/generic_container.rb
proforma-1.0.0 lib/proforma/modeling/generic_container.rb