Sha256: 4f5bb08a7985dd1326a7472311ca3293be655b4884f03af64d637a97151bf53e

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

module Evil::Client::DSL
  require_relative "operation"

  # Container for operations definitions
  # Applies settings to definitions and returns a final schema
  class Operations
    # Adds block definition as a named operation
    #
    # @param [#to_sym] key
    # @param [Proc] block
    # @return [self]
    #
    def register(key, &block)
      @schema[key] = Operation.new(key, block)
      self
    end

    # Applies settings to all definitions and returns a final schema
    #
    # @param  [Object] settings
    # @return [Hash<Symbol, Object>]
    #
    def finalize(settings)
      default = @schema[nil].finalize(settings)
      custom  = @schema.select { |key| key }

      custom.each_with_object({}) do |(key, operation), hash|
        custom = operation.finalize(settings)
        hash[key] = default.merge(custom)
        hash[key][:format] ||= "json"
        hash[key][:responses] = default[:responses].merge(custom[:responses])
      end
    end

    private

    def initialize
      @schema = { nil => Operation.new(nil, nil) }
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
evil-client-0.3.1 lib/evil/client/dsl/operations.rb
evil-client-0.3.0 lib/evil/client/dsl/operations.rb
evil-client-0.2.3 lib/evil/client/dsl/operations.rb
evil-client-0.2.2 lib/evil/client/dsl/operations.rb
evil-client-0.2.1 lib/evil/client/dsl/operations.rb