Sha256: c5060186b4a84483309b6494e3e6402e46d6fba914516f74330b062f5bbe5af7

Contents?: true

Size: 1.41 KB

Versions: 6

Compression:

Stored size: 1.41 KB

Contents

require 'set'
require 'betterdocs/representer'

module Betterdocs::ResultRepresenter
  extend ActiveSupport::Concern
  include Betterdocs::Dsl::Common
  include Betterdocs::Representer
  include Betterdocs::Responding

  module ClassMethods
    def hashify(object)
      super do |result|
        assign_properties result, object
        assign_links      result, object
      end
    end

    def doc(type, name, **options, &block)
      docs.add_element(self, type, name, **options, &block)
    end

    def docs
      @docs ||= Betterdocs::ResultRepresenterCollector.new
    end

    def assign_links(result, object)
      result['links'] = []
      for link in links
        link.assign(result, object)
      end
    end
    private :assign_links

    def assign_properties(result, object)
      for property in properties
        property.assign(result, object)
      end
    end
    private :assign_properties

    def properties
      @properties ||= Set.new
    end

    def property(name, **options, &block)
      d = doc(:property, name, **options, &block) and
        properties << d
      self
    end

    def collection(name, **options, &block)
      d = doc(:collection_property, name, **options, &block) and
        properties << d
      self
    end

    def links
      @links ||= Set.new
    end

    def link(name, **options, &block)
      d = doc(:link, name, **options, &block) and links << d
      self
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
betterdocs-0.13.0 lib/betterdocs/result_representer.rb
betterdocs-0.12.5 lib/betterdocs/result_representer.rb
betterdocs-0.12.4 lib/betterdocs/result_representer.rb
betterdocs-0.12.3 lib/betterdocs/result_representer.rb
betterdocs-0.12.2 lib/betterdocs/result_representer.rb
betterdocs-0.12.1 lib/betterdocs/result_representer.rb