Sha256: 57574dac02c5242cc9cd8e18ba2287b98364daa6a80d8b04343b59c6bc6cfefe

Contents?: true

Size: 1.23 KB

Versions: 2

Compression:

Stored size: 1.23 KB

Contents

module Transcriber
  class Resource
    module Response
      extend ActiveSupport::Concern

      def resource(options = {})
        root = options.fetch(:root, true)

        {}.tap do |resource|
          resource_keys(options).inject(resource) do |buffer, key|
            buffer.merge!(key.to_resource(self))
          end

          if root and self.class.relations.any? and !options[:summarize]
            resource.merge!({link: link})
          end
        end
      end

      def link
        self.class.relations.map {|key| key.to_relation(self)}
      end

      def resource_keys(options)
        summarize?(options) ? self.class.summarized_keys + [Resource::Href.new]
                            : self.class.keys
      end

      def summarize?(options)
        options[:summarize] and self.class.summarized_keys.any?
      end

      module ClassMethods
        def normalize(model, options = {})
          model.kind_of?(Enumerable) ?
            {entries: model.map {|m| m.resource(options.merge(summarize: true))}}
           : model.resource(options)
        end

        alias :resources :normalize

        def transcribe(input, options = {})
          normalize(parse(input, options), options)
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
transcriber-0.0.26 lib/transcriber/resource/response.rb
transcriber-0.0.25 lib/transcriber/resource/response.rb