Sha256: e882e9ec495778bcfc62a702cca1d009ef491f4f22fd7616becb69c7eac3eebe

Contents?: true

Size: 811 Bytes

Versions: 12

Compression:

Stored size: 811 Bytes

Contents

require 'action_controller'

module Betterdocs::Representer
  extend ActiveSupport::Concern

  def as_json(*)
    singleton_class.ancestors.find do |c|
      c != singleton_class && c < Betterdocs::Representer
    end.hashify(self)
  end

  def to_json(*a)
    JSON::generate(as_json, *a)
  end

  module ClassMethods
    def apply(object)
      object.extend self
    end

    def build_result_object
      {}
    end

    def hashify(object, &block)
      apply(object)
      result = build_result_object
      instance_exec(result, &block)
      result
    end

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

    def object_name(*) end

    def docs
      raise NotImplementedError, 'has to be implemented in including module'
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
betterdocs-0.6.7 lib/betterdocs/representer.rb
betterdocs-0.6.6 lib/betterdocs/representer.rb
betterdocs-0.6.5 lib/betterdocs/representer.rb
betterdocs-0.6.4 lib/betterdocs/representer.rb
betterdocs-0.6.3 lib/betterdocs/representer.rb
betterdocs-0.6.2 lib/betterdocs/representer.rb
betterdocs-0.6.1 lib/betterdocs/representer.rb
betterdocs-0.6.0 lib/betterdocs/representer.rb
betterdocs-0.5.0 lib/betterdocs/representer.rb
betterdocs-0.4.0 lib/betterdocs/representer.rb
betterdocs-0.3.0 lib/betterdocs/representer.rb
betterdocs-0.2.0 lib/betterdocs/representer.rb