class Yaks::Format

Attributes

format_name[R]
media_type[R]
serializer[R]
env[R]
options[R]

@!attribute [r] options

@return [Hash]

Public Class Methods

all() click to toggle source
# File lib/yaks/format.rb, line 42
def all
  @formats ||= []
end
by_accept_header(accept_header) { || ... } click to toggle source
# File lib/yaks/format.rb, line 73
def by_accept_header(accept_header)
  media_type = Rack::Accept::Charset.new(accept_header).best_of(media_types.values)
  if media_type
    by_media_type(media_type)
  else
    yield if block_given?
  end
end
by_media_type(media_type) click to toggle source

@param [Symbol] media_type @return [Constant] @raise [KeyError]

# File lib/yaks/format.rb, line 68
def by_media_type(media_type)
  find(:media_type, media_type)
end
by_name(format_name) click to toggle source

@param [Symbol] format_name @return [Constant] @raise [KeyError]

# File lib/yaks/format.rb, line 61
def by_name(format_name)
  find(:format_name, format_name)
end
media_types() click to toggle source
# File lib/yaks/format.rb, line 82
def media_types
  Format.all.each_with_object({}) do |format, memo|
    memo[format.format_name] = format.media_type
  end
end
names() click to toggle source
# File lib/yaks/format.rb, line 89
def names
  media_types.keys
end
new(options = {}) click to toggle source

@param [Hash] options @return [Hash]

# File lib/yaks/format.rb, line 19
def initialize(options = {})
  @options = options
end
register(format_name, serializer, media_type) click to toggle source

@param [Constant] klass @param [Symbol] format_name @param [String] media_type @return [Array]

# File lib/yaks/format.rb, line 50
def register(format_name, serializer, media_type)
  @format_name = format_name
  @serializer = serializer
  @media_type = media_type

  Format.all << self
end

Private Class Methods

find(key, cond) click to toggle source
# File lib/yaks/format.rb, line 95
def find(key, cond)
  Format.all.detect {|format| format.send(key) == cond }
end

Public Instance Methods

call(resource, env = {}) click to toggle source

@param [Yaks::Resource] resource @return [Hash]

# File lib/yaks/format.rb, line 25
def call(resource, env = {})
  @env = env
  serialize_resource(resource)
end
Also aliased as: serialize
serialize(resource, env = {})
Alias for: call
serialize_resource(_resource) click to toggle source

@abstract

# File lib/yaks/format.rb, line 32
def serialize_resource(_resource)
end