Sha256: b8f9942f8934e73b759fca8c86d0a55b3ae1fbd450a434a3b92eb37e608d5380

Contents?: true

Size: 1.4 KB

Versions: 9

Compression:

Stored size: 1.4 KB

Contents

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

module Roar
  module Representer
    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 to_hash(*args)
          before_serialize(*args)
          super
        end

        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
        def deserialize(*args)
          from_json(*args)
        end

        # TODO: move to instance method, or remove?
        def links_definition_options
          [:links_array, {:from => :links, :class => Feature::Hypermedia::Hyperlink, :extend => HyperlinkRepresenter, :collection => true}]
        end
      end


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

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
roar-0.11.15 lib/roar/representer/json.rb
roar-0.11.14 lib/roar/representer/json.rb
roar-0.11.13 lib/roar/representer/json.rb
roar-0.11.12 lib/roar/representer/json.rb
roar-0.11.11 lib/roar/representer/json.rb
roar-0.11.10 lib/roar/representer/json.rb
roar-0.11.9 lib/roar/representer/json.rb
roar-0.11.8 lib/roar/representer/json.rb
roar-0.11.7 lib/roar/representer/json.rb