Sha256: e628bdcbeee6f1e059b6b54fb6673224228bb19df6a011539581e02b3e299d45

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

require 'roar/representer'
require 'roar/hypermedia'
require 'representable/json'

module Roar
  module JSON
    def self.included(base)
      base.class_eval do
        include Representer
        include Representable::JSON

        extend ClassMethods
        include InstanceMethods # otherwise Representable overrides our #to_json.
      end
    end

    module InstanceMethods
      def from_json(document, options={})
        document = '{}' if document.nil? or document.empty?

        super
      end

      # Generic entry-point for rendering.
      def serialize(*args)
        to_json(*args)
      end

      def deserialize(*args)
        from_json(*args)
      end
    end


    module ClassMethods
      # TODO: move to instance method, or remove?
      def links_definition_options
        # FIXME: this doesn't belong into the generic JSON representer.
        {
          :collection     => true,
          :class          => Hypermedia::Hyperlink,
          :extend         => HyperlinkRepresenter,
          :exec_context   => :decorator,
        }
      end
    end


    require "representable/json/hash"
    # Represents a hyperlink in standard roar+json hash representation.
    module HyperlinkRepresenter
      include Representable::JSON::Hash
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
roar-1.0.4 lib/roar/json.rb
roar-1.0.3 lib/roar/json.rb
roar-1.0.2 lib/roar/json.rb
roar-1.0.1 lib/roar/json.rb
roar-1.0.0 lib/roar/json.rb
roar-1.0.0.beta2 lib/roar/json.rb
roar-1.0.0.beta1 lib/roar/json.rb