Sha256: e91ab61c11225e2ef5fa4c36ea960ab5d86be079a721cf20c95166e8a9d4f629

Contents?: true

Size: 1.23 KB

Versions: 20

Compression:

Stored size: 1.23 KB

Contents

module Gris
  module PresenterLinkHelpers
    def self.included(base)
      base.class_eval do
        def self.resource_links(name, args = [], resource_uri_template = '/{id}')
          args += %w(page size)
          endpoint_link(
            name.to_s.pluralize,
            template_options: args, templated: true
          )
          endpoint_link(
            name,
            namespace: name.to_s.pluralize,
            uri_template: resource_uri_template,
            templated: true
          )
        end

        def self.endpoint_link(name, options = {})
          namespace = options[:namespace] || name
          template_options = options[:template_options] || []
          uri_template = options[:uri_template] || format_template_options(template_options)
          link name do
            link = {
              href: "#{Gris::Identity.base_url}/#{namespace}#{uri_template}"
            }
            link[:templated] = true if !!options[:templated] || !template_options.blank?
            link
          end
        end

        private

        def self.format_template_options(template_options = [])
          return unless template_options.any?
          "{?#{template_options.join(',')}}"
        end
      end
    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
gris-0.6.7 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.6.6 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.6.5 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.6.2 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.6.1 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.6.0 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.5.9 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.5.8 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.5.7 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.5.6 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.5.5 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.5.4 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.5.3 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.5.2 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.5.1 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.5.0 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.4.9 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.4.8 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.4.7 lib/gris/output_formatters/presenter_link_helpers.rb
gris-0.4.6 lib/gris/output_formatters/presenter_link_helpers.rb