Sha256: c9e7fc95e3f32126bdc43619e0c7d77f91a0291ff405617fc03acb881cff213c

Contents?: true

Size: 1.83 KB

Versions: 33

Compression:

Stored size: 1.83 KB

Contents

require 'active_support/concern'

module HalApi::Representer::Curies
  extend ActiveSupport::Concern

  included do
    class_eval do
      class_attribute :default_curie, instance_writer: false
    end
  end

  module ClassMethods

    LINK_RELATIONS = %w(about alternate appendix archives author bookmark
    canonical chapter collection contents copyright create-form curies current
    describedby describes disclosure duplicate edit edit-form edit-media
    enclosure first glossary help hosts hub icon index item last latest-version
    license lrdd memento monitor monitor-group next next-archive nofollow
    noreferrer original payment predecessor-version prefetch prev preview
    previous prev-archive privacy-policy profile related replies search section
    self service start stylesheet subsection successor-version tag
    terms-of-service timegate timemap type up version-history via working-copy
    working-copy-of)

    def use_curie(curie)
      self.default_curie = curie
    end

    def curies(curie, &block)
      use_curie(curie) unless default_curie
      link({rel: :curies, array: true}) do
        if represented.try(:show_curies)
          block.call
        end
      end
    end

    def curify(rel, curie=default_curie)
      if curie.blank? || rel.to_s =~ /:/ || LINK_RELATIONS.include?(rel.to_s)
        rel
      else
        "#{curie}:#{rel}".to_sym
      end
    end

    def link(options, &block)
      options = {:rel => options} unless options.is_a?(Hash)
      options[:rel] = curify(options[:rel])
      super(options, &block)
    end

    def property(name, options={})
      options[:as] = curify(options[:as] || name) if options[:embedded]
      super(name, options)
    end

    def collection(name, options={})
      options[:as] = curify(options[:as] || name) if options[:embedded]
      super(name, options)
    end
  end
end

Version data entries

33 entries across 33 versions & 1 rubygems

Version Path
hal_api-rails-1.2.2 lib/hal_api/representer/curies.rb
hal_api-rails-1.2.1 lib/hal_api/representer/curies.rb
hal_api-rails-1.2.0 lib/hal_api/representer/curies.rb
hal_api-rails-1.1.5 lib/hal_api/representer/curies.rb
hal_api-rails-1.1.4 lib/hal_api/representer/curies.rb
hal_api-rails-0.3.7 lib/hal_api/representer/curies.rb
hal_api-rails-0.3.6 lib/hal_api/representer/curies.rb
hal_api-rails-1.1.3 lib/hal_api/representer/curies.rb
hal_api-rails-0.3.5 lib/hal_api/representer/curies.rb
hal_api-rails-1.1.2 lib/hal_api/representer/curies.rb
hal_api-rails-1.1.1 lib/hal_api/representer/curies.rb
hal_api-rails-1.1.0 lib/hal_api/representer/curies.rb
hal_api-rails-1.0.0 lib/hal_api/representer/curies.rb
hal_api-rails-0.7.0 lib/hal_api/representer/curies.rb
hal_api-rails-0.6.0 lib/hal_api/representer/curies.rb
hal_api-rails-0.5.0 lib/hal_api/representer/curies.rb
hal_api-rails-0.4.1 lib/hal_api/representer/curies.rb
hal_api-rails-0.4.0 lib/hal_api/representer/curies.rb
hal_api-rails-0.3.4 lib/hal_api/representer/curies.rb
hal_api-rails-0.3.3 lib/hal_api/representer/curies.rb