Sha256: 33ec22740951c264cdcb166ed6ddc620f1185b6d17e84ee90c5a42a7f86ca41a

Contents?: true

Size: 1.89 KB

Versions: 1

Compression:

Stored size: 1.89 KB

Contents

module NxtSchema
  module Node
    module HasSubNodes
      def collection(name, type = NxtSchema::Node::Collection::DEFAULT_TYPE, **options, &block)
        node = NxtSchema::Node::Collection.new(
          name: name,
          type: type,
          parent_node: self,
          **options,
          &block
        )

        add_sub_node(node)
      end

      alias nodes collection

      def schema(name, type = NxtSchema::Node::Schema::DEFAULT_TYPE, **options, &block)
        node = NxtSchema::Node::Schema.new(
          name: name,
          type: type,
          parent_node: self,
          **options,
          &block
        )

        add_sub_node(node)
      end

      def any_of(name, **options, &block)
        node = NxtSchema::Node::AnyOf.new(
          name: name,
          parent_node: self,
          **options,
          &block
        )

        add_sub_node(node)
      end

      def node(name, node_or_type_of_node, **options, &block)
        node = if node_or_type_of_node.is_a?(NxtSchema::Node::Base)
          raise ArgumentError, "Can't provide a block along with a node" if block.present?

          node_or_type_of_node.class.new(
            name: name,
            type: node_or_type_of_node.type,
            parent_node: self,
            **node_or_type_of_node.options.merge(options), # Does this make sense to merge options here?
            &node_or_type_of_node.configuration
          )
        else
          NxtSchema::Node::Leaf.new(
            name: name,
            type: node_or_type_of_node,
            parent_node: self,
            **options,
            &block
          )
        end

        add_sub_node(node)
      end

      alias required node

      def add_sub_node(node)
        sub_nodes.add(node)
        node
      end

      def sub_nodes
        @sub_nodes ||= Node::SubNodes.new
      end

      def [](key)
        sub_nodes[key]
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nxt_schema-1.0.0 lib/nxt_schema/node/has_sub_nodes.rb