Sha256: aaa34033d05418b0472846dc99c7d7f7b9c133f8da9357e3de2eb12ca5a94e5e

Contents?: true

Size: 1.46 KB

Versions: 1

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Skemata
  class DSL
    # Make a new type
    class NodeMethodChain < Array; end

    class << self
      #
      # Draw a schema.org node. Creates a new DSL class for each child node.
      # @param opts = {} [Hash] Options hash
      #   root_object (required): Object you wish to serialize
      #   type        (required): schema.org type
      #   is_root     (optional): Is this the top level object
      # @param &block [Block] DSL schema definition
      #
      # @return [String] schema.org JSON structure
      def draw(opts = {}, &block)
        dsl = new(Node.new(opts))
        dsl.instance_eval(&block)
        opts.fetch(:is_root, true) ? dsl.node.data.to_json : dsl.node.data
      end
    end

    #
    # Prepares DSL instance by assigning Node
    # @param node [Node] Data object
    #
    # @return [DSL] A DSL class.
    def initialize(node)
      raise(
        ArgumentError, 'DSL must be provided with a Skemata::Node type!'
      ) unless node.is_a?(Node)

      @node = node
    end

    # rubocop:disable Style/MethodMissing
    # TODO: special token so we can define respond_to_missing?
    def method_missing(name, *args, &block)
      node.decorate(name, *args, &block)
    end
    # rubocop:enable Style/MethodMissing

    #
    # Delegator to NodeMethodChain.new
    # @param *args [varargs]
    #
    # @return [NodeMethodChain]
    def nested(*args)
      NodeMethodChain.new(args)
    end

    attr_reader :node
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
skemata-0.0.1 lib/skemata/dsl.rb