Sha256: 242c421c4717c5335ce533afe19d90601ad91d1c3a5d8f582b741c13116679bd

Contents?: true

Size: 1.7 KB

Versions: 21

Compression:

Stored size: 1.7 KB

Contents

module JSONAPIonify::Api
  module Resource::Documentation

    def self.extended(mod)
      mod.example_id_generator { |val| val }
    end

    def documented_actions_in_order
      indexes = %i{list create read update delete add replace remove}
      documented_actions.reject do |a, *|
        ['HEAD', 'OPTIONS'].include? a.request_method
      end.sort_by do |action, *|
        indexes.index(action.name) || indexes.length
      end
    end

    def documentation_object(base_url)
      OpenStruct.new(
        name:          type,
        description:   JSONAPIonify::Documentation.render_markdown(@description || ''),
        relationships: relationships.map { |r| r.documentation_object },
        attributes:    attributes.sort_by(&:name).map(&:documentation_object),
        actions:       documented_actions_in_order.map do |action, base, args|
          action.documentation_object File.join(base_url, base), *args
        end
      )
    end

    def example_id_generator(&block)
      index = 0
      define_singleton_method(:generate_id) do
        instance_exec index += 1, &block
      end
      context :example_id do
        self.class.generate_id
      end
    end

    def example_instance_for_action(action, context)
      id = generate_id
      OpenStruct.new.tap do |instance|
        instance.send "#{id_attribute}=", id.to_s
        actionable_attributes = attributes.select do |attr|
          attr.supports_read_for_action?(action, context)
        end
        actionable_attributes.each do |attribute|
          instance.send "#{attribute.name}=", attribute.example(id)
        end

        instance.define_singleton_method :method_missing do |*args|
          self
        end
      end
    end

  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
jsonapionify-0.12.8 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.12.7 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.12.5 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.12.4 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.12.3 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.12.2 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.12.1 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.12.0 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.11.11 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.11.10 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.11.9 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.11.8 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.11.7 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.11.6 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.11.5 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.11.0 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.10.2 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.10.1 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.10.0 lib/jsonapionify/api/resource/documentation.rb
jsonapionify-0.9.3 lib/jsonapionify/api/resource/documentation.rb