Sha256: 18f45bfa21e53d75e9df1cc73dcf9de076dc9690863cb19ab8914e270f5d22e6

Contents?: true

Size: 1.51 KB

Versions: 19

Compression:

Stored size: 1.51 KB

Contents

module Jdoc
  class Resource
    attr_reader :schema

    # @param schema [JsonSchema::Schema]
    def initialize(schema)
      @schema = schema
    end

    # @return [String] Description for this schema, defined in description property
    # @example
    #   resource.description #=> "An app is a program to be deployed."
    def description
      @schema.description
    end

    # @return [String] Href anchor for putting link in ToC
    # @example
    #   resource.anchor #=> "#app"
    def anchor
      "#" + title.gsub(" ", "-").gsub(/[:\/]/, "").downcase
    end

    # @return [String] Markdown styled link text for this resource
    # @example
    #   resource.hyperlink #=> "[App](#apps)"
    def hyperlink
      "[#{title}](#{anchor})"
    end

    # @return [String] Title defined in title property
    # @example
    #   resource.title #=> "App"
    def title
      @title ||= @schema.title || @schema.pointer.split("/").last.camelize
    end

    # @return [Array<Jdoc::Property>]
    def properties
      @schema.properties.map do |name, schema|
        Property.new(name: name, schema: schema)
      end
    end

    # Defined to change uniqueness in Hash key
    def hash
      title.hash
    end

    # Defined to change uniqueness in Hash key
    def eql?(other)
      title == other.title
    end

    def <=>(other)
      title <=> other.title
    end

    def links
      @links ||= @schema.links.map do |link|
        if link.method && link.href
          Link.new(link)
        end
      end.compact
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
jdoc-0.4.4 lib/jdoc/resource.rb
jdoc-0.4.3 lib/jdoc/resource.rb
jdoc-0.4.2 lib/jdoc/resource.rb
jdoc-0.4.1 lib/jdoc/resource.rb
jdoc-0.4.0 lib/jdoc/resource.rb
jdoc-0.3.4 lib/jdoc/resource.rb
jdoc-0.3.3 lib/jdoc/resource.rb
jdoc-0.3.2 lib/jdoc/resource.rb
jdoc-0.3.1 lib/jdoc/resource.rb
jdoc-0.3.0 lib/jdoc/resource.rb
jdoc-0.2.1 lib/jdoc/resource.rb
jdoc-0.2.0 lib/jdoc/resource.rb
jdoc-0.1.9 lib/jdoc/resource.rb
jdoc-0.1.8 lib/jdoc/resource.rb
jdoc-0.1.7 lib/jdoc/resource.rb
jdoc-0.1.6 lib/jdoc/resource.rb
jdoc-0.1.5 lib/jdoc/resource.rb
jdoc-0.1.4 lib/jdoc/resource.rb
jdoc-0.1.3 lib/jdoc/resource.rb