Sha256: cfed86342918a7d194ee46be6ea788f92ed36f92374eeaf887eaa928600d4a33

Contents?: true

Size: 737 Bytes

Versions: 2

Compression:

Stored size: 737 Bytes

Contents

require 'active_support/concern'

module HalApi::Representer::LinkSerialize

  extend ActiveSupport::Concern

  module ClassMethods

    def link(options, &block)
      set_link_property(options)
      super(options, &block)
    end

    def set_link_property(options)
      if options.is_a?(Hash) && (options.delete(:writeable) || options[:reader])
        name = options[:rel].to_s.split(':').last.split('/').last
        pname = "set_#{name}_uri"
        reader = options.delete(:reader) || ->(obj) do
          doc = obj[:doc]
          try("#{name}_id=", id_from_url(doc[pname])) if doc[pname]
          Representable::Pipeline::Stop
        end

        property(pname, readable: false, reader: reader)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
hal_api-rails-1.2.2 lib/hal_api/representer/link_serialize.rb
hal_api-rails-1.2.1 lib/hal_api/representer/link_serialize.rb