Sha256: 7750449bf024dc317cc69a8b623e394664a5889b8121b8cb15babbffa53825f0

Contents?: true

Size: 1.29 KB

Versions: 2

Compression:

Stored size: 1.29 KB

Contents

module Schemacop
  class NodeSupportingField < NodeWithBlock
    block_method :req?
    block_method :req!
    block_method :req
    block_method :opt?
    block_method :opt!
    block_method :opt

    attr_reader :fields

    def initialize(options = {}, &block)
      @fields = {}
      super
      exec_block(&block)
    end

    def req?(*args, &block)
      field(*args, required: true, allow_nil: true, &block)
    end

    def req!(*args, &block)
      field(*args, required: true, allow_nil: false, &block)
    end

    alias_method :req, :req!

    def opt?(*args, &block)
      field(*args, required: false, allow_nil: true, &block)
    end

    def opt!(*args, &block)
      field(*args, required: false, allow_nil: false, &block)
    end

    alias_method :opt, :opt?

    protected

    def field(*args, required:, allow_nil:, &block)
      # name = args.shift
      # options = args.last.is_a?(Hash) ? args.pop : {}
      name = args.shift

      if @fields[name]
        @fields[name].type(*args, &block)
      else
        if args.any?
          @fields[name] = FieldNode.new(name, required) do
            type(*args, &block)
          end
        else
          @fields[name] = FieldNode.new(name, required, &block)
        end
      end

      @fields[name].type(:nil) if allow_nil
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
schemacop-2.2.0 lib/schemacop/node_supporting_field.rb
schemacop-2.1.0 lib/schemacop/node_supporting_field.rb