Sha256: 4ef6c655b6a939493c852e41d475b0288283da217eb77a7772f24e2ebde5e816

Contents?: true

Size: 1.18 KB

Versions: 7

Compression:

Stored size: 1.18 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

          resource.merge!({link: link}) if root and self.class.relations.any?
        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

7 entries across 7 versions & 1 rubygems

Version Path
transcriber-0.0.24 lib/transcriber/resource/response.rb
transcriber-0.0.23 lib/transcriber/resource/response.rb
transcriber-0.0.22 lib/transcriber/resource/response.rb
transcriber-0.0.21 lib/transcriber/resource/response.rb
transcriber-0.0.20 lib/transcriber/resource/response.rb
transcriber-0.0.19 lib/transcriber/resource/response.rb
transcriber-0.0.18 lib/transcriber/resource/response.rb