Sha256: 38e39bfb2daf13b2c335653b71769cfccc05210bc3ca80cebc13fb94f7dd6704

Contents?: true

Size: 1.77 KB

Versions: 9

Compression:

Stored size: 1.77 KB

Contents

require_relative "operation"

class Evil::Client
  #
  # Mutable container of definitions for sub-scopes and operations
  # with DSL to configure those definitions.
  #
  class Schema::Scope < Schema::Operation
    # Tells that this is a schema for a scope (not the final operation)
    #
    # @return [false]
    #
    def leaf?
      false
    end

    # The collection of named sub-scope schemas
    #
    # Every sub-scope schema refers to the current one as a [#parent]
    #
    # @return [Hash<Symbol, Class>]
    #
    def scopes
      @__children__.reject { |_, child| child.leaf? }
    end

    # The collection of named operation schemas
    #
    # @return [Hash<[Symbol, nil], Class>]
    #
    def operations
      @__children__.select { |_, child| child.leaf? }
    end

    # Creates or updates sub-scope definition
    #
    # @param  [#to_sym] name The unique name of subscope inside current scope
    # @param  [Proc] block The block containing definition for the subscope
    # @return [self]
    #
    def scope(name, &block)
      key = NameError.check!(name)
      TypeError.check! self, key, :scope
      @__children__[key] ||= self.class.new(self, key)
      @__children__[key].instance_exec(&block)
      self
    end

    # Creates or updates operation definition
    #
    # @param  [#to_sym] name The unique name of operation inside the scope
    # @param  [Proc] block The block containing definition for the operation
    # @return [self]
    #
    def operation(name, &block)
      key = NameError.check!(name)
      TypeError.check! self, key, :operation
      @__children__[key] ||= self.class.superclass.new(self, key)
      @__children__[key].instance_exec(&block)
      self
    end

    private

    def initialize(*)
      super
      @__children__ = {}
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
evil-client-3.0.4 lib/evil/client/schema/scope.rb
evil-client-3.0.3 lib/evil/client/schema/scope.rb
evil-client-3.0.2 lib/evil/client/schema/scope.rb
evil-client-3.0.1 lib/evil/client/schema/scope.rb
evil-client-3.0.0 lib/evil/client/schema/scope.rb
evil-client-2.1.1 lib/evil/client/schema/scope.rb
evil-client-2.1.0 lib/evil/client/schema/scope.rb
evil-client-2.0.0 lib/evil/client/schema/scope.rb
evil-client-1.1.0 lib/evil/client/schema/scope.rb