Sha256: 4b4aa8338becb38cec318a80bf8af1c2c7319e49080e4bedec37e00b33583cce

Contents?: true

Size: 1.39 KB

Versions: 2

Compression:

Stored size: 1.39 KB

Contents

require 'roar/representer/base'
require 'representable/json'

module Roar
  module Representer
    module JSON
      def self.included(base)
        base.class_eval do
          include Base
          include Representable::JSON
          
          extend ClassMethods
          include InstanceMethods # otherwise Representable overrides our #to_json.
        end
      end
      
      module InstanceMethods
        def to_json(*args)
          before_serialize(*args)
          super
        end
        
        def from_json(document, options={})
          document ||= "{}" # DISCUSS: provide this for convenience, or better not?
          
          if block = deserialize_block_for_options(options) and 
            return super(document, &block)
          end
          
          super
        end
        
        # Generic entry-point for rendering.
        def serialize(*args)
          to_json(*args)
        end
      end
      
      
      module ClassMethods
        def deserialize(json)
          from_json(json)
        end
        
        # TODO: move to instance method, or remove?
        def links_definition_options
          {:as => [Hyperlink]}
        end
      end
      
      
      # Encapsulates a hypermedia link.
      class Hyperlink
        include JSON
        self.representation_name = :link
        
        property :rel
        property :href
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
roar-0.8.2 lib/roar/representer/json.rb
roar-0.8.1 lib/roar/representer/json.rb