Sha256: 9433ba56577e90570e62b457a4dfbbed4a9104d9ecd49731dcfc62600404b02d

Contents?: true

Size: 1.31 KB

Versions: 7

Compression:

Stored size: 1.31 KB

Contents

module Restspec
  module Schema
    # A schema is a collection of attributes that defines how the data passed through the API
    # should be formed. In REST, they are the representation of the resources the REST API
    # returns.
    class Schema
      # The schema identifier.
      attr_reader :name

      # The set of attributes that conforms the schema.
      attr_reader :attributes

      # TODO: Document
      attr_accessor :intention
      attr_accessor :original_schema

      # @param name [Symbol] The name of the schema
      # @return a new {Restspec::Schema::Schema Schema} object
      def initialize(name)
        self.name = name
        self.attributes = {}
      end

      # @param without [Array] An array of attributes that should be removed from the schema.
      #   This shouldn't be used without cloning first, to avoid modifying a schema
      #   used elsewhere.
      def extend_with(without: [])
        without.each { |attribute_name| attributes.delete(attribute_name.to_s) }
        self
      end

      def attributes_for_intention
        return attributes if intention.blank?

        attributes.inject({}) do |hash, (name, attribute)|
          attribute.can?(intention) ? hash.merge(name => attribute) : hash
        end
      end

      private

      attr_writer :name, :attributes
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
restspec-0.2.6 lib/restspec/schema/schema.rb
restspec-0.2.5 lib/restspec/schema/schema.rb
restspec-0.2.4 lib/restspec/schema/schema.rb
restspec-0.2.3 lib/restspec/schema/schema.rb
restspec-0.2.2 lib/restspec/schema/schema.rb
restspec-0.2.1 lib/restspec/schema/schema.rb
restspec-0.2 lib/restspec/schema/schema.rb