Sha256: 4ea0d53043b875a3aeb8563c1e70e2321afeef23e35fac2040fe193f400d86ce

Contents?: true

Size: 1.13 KB

Versions: 36

Compression:

Stored size: 1.13 KB

Contents

module Schemacop
  module V3
    class DslScope
      EXP_NAME = /^dsl_([a-z_]+)([?!])?$/.freeze

      def initialize(node)
        @node = node
        @with_name = @node.class.supports_children_options[:name]
      end

      def method_missing(name, *args, **options, &block)
        match = EXP_NAME.match(name)
        return super unless match

        base_name, req_optional = match.captures

        if req_optional == '!'
          options[:required] = true
        elsif req_optional == '?'
          options[:required] = false
        end

        options[:parent] = @node

        if (klass = NodeRegistry.by_short_name(base_name))
          if @with_name
            options[:name] = args.shift
          end
          node = klass.create(*args, **options, &block)
          @node.add_child node
          return node
        else
          return super
        end
      end

      def respond_to_missing?(name, *args)
        match = EXP_NAME.match(name)
        return super unless match

        base_name, _req_optional = match.captures
        return NodeRegistry.by_short_name(base_name).present? || super
      end
    end
  end
end

Version data entries

36 entries across 36 versions & 1 rubygems

Version Path
schemacop-3.0.29 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.28 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.27 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.26 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.25 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.24 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.23 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.22 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.21 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.20 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.19 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.18 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.17 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.16 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.15 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.14 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.13 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.12 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.11 lib/schemacop/v3/dsl_scope.rb
schemacop-3.0.10 lib/schemacop/v3/dsl_scope.rb