Sha256: d7b4b46de3f1b0b622dc433c60e2b1e9c6beb9c91c9972b1f0ca0fd0ff044564

Contents?: true

Size: 930 Bytes

Versions: 2

Compression:

Stored size: 930 Bytes

Contents

module JSONAPI
  # c.f. http://jsonapi.org/format/#document-resource-object-relationships
  class Relationships
    include Enumerable

    def initialize(relationships_hash, options = {})
      fail InvalidDocument,
           "the value of 'relationships' MUST be an object" unless
        relationships_hash.is_a?(Hash)

      @hash = relationships_hash
      @relationships = {}
      relationships_hash.each do |rel_name, rel_hash|
        @relationships[rel_name.to_s] = Relationship.new(rel_hash, options)
        define_singleton_method(rel_name) do
          @relationships[rel_name.to_s]
        end
      end
    end

    def to_hash
      @hash
    end

    def each(&block)
      @relationships.each(&block)
    end

    def [](rel_name)
      @relationships[rel_name.to_s]
    end

    def defined?(rel_name)
      @relationships.key?(rel_name.to_s)
    end

    def keys
      @relationships.keys
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
jsonapi-0.1.1.beta2 lib/jsonapi/relationships.rb
jsonapi-0.1.1.beta1 lib/jsonapi/relationships.rb