Sha256: 4e3152f8d2c81369874c3af902cee7139bf20fc3f3c79fddafa4dc84788c2a7e

Contents?: true

Size: 1.47 KB

Versions: 3

Compression:

Stored size: 1.47 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, {:class => Feature::Hypermedia::Hyperlink, :extend => HyperlinkRepresenter, :collection => true}]
        end
      end
      
      
      # Represents a hyperlink in standard roar+json. 
      module HyperlinkRepresenter
        include JSON
        Feature::Hypermedia::Hyperlink.params.each do |attr|
          property attr
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
roar-0.10.2 lib/roar/representer/json.rb
roar-0.10.1 lib/roar/representer/json.rb
roar-0.10.0 lib/roar/representer/json.rb