Class: Yaks::Format

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, Util::Deprecated
Includes:
Yaks::FP::Callable, Util
Defined in:
lib/yaks/format.rb,
lib/yaks/format/hal.rb,
lib/yaks/format/halo.rb,
lib/yaks/format/json_api.rb,
lib/yaks/format/collection_json.rb

Direct Known Subclasses

CollectionJson, Hal, JsonAPI

Defined Under Namespace

Classes: CollectionJson, Hal, Halo, JsonAPI, Reader

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Util::Deprecated

deprecated_alias

Methods included from Util

#Resolve, #camelize, #extract_options, #reject_keys, #slice_hash, #symbolize_keys, #underscore

Methods included from Yaks::FP::Callable

#to_proc

Constructor Details

#initialize(options = {}) ⇒ Hash

Parameters:

  • options (Hash) (defaults to: {})


19
20
21
# File 'lib/yaks/format.rb', line 19

def initialize(options = {})
  @options = options
end

Class Attribute Details

.format_nameObject (readonly)

Returns the value of attribute format_name.



38
39
40
# File 'lib/yaks/format.rb', line 38

def format_name
  @format_name
end

.media_typeObject (readonly)

Returns the value of attribute media_type.



38
39
40
# File 'lib/yaks/format.rb', line 38

def media_type
  @media_type
end

.serializerObject (readonly)

Returns the value of attribute serializer.



38
39
40
# File 'lib/yaks/format.rb', line 38

def serializer
  @serializer
end

Instance Attribute Details

#envObject (readonly)

Returns the value of attribute env.



11
12
13
# File 'lib/yaks/format.rb', line 11

def env
  @env
end

Class Method Details

.allObject



42
43
44
# File 'lib/yaks/format.rb', line 42

def all
  @formats ||= []
end

.by_accept_header(accept_header) ⇒ Object



73
74
75
76
77
78
79
80
# 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) ⇒ Constant

Parameters:

  • media_type (Symbol)

Returns:

  • (Constant)

Raises:

  • (KeyError)


68
69
70
# File 'lib/yaks/format.rb', line 68

def by_media_type(media_type)
  find(:media_type, media_type)
end

.by_name(format_name) ⇒ Constant

Parameters:

  • format_name (Symbol)

Returns:

  • (Constant)

Raises:

  • (KeyError)


61
62
63
# File 'lib/yaks/format.rb', line 61

def by_name(format_name)
  find(:format_name, format_name)
end

.media_typesObject



82
83
84
85
86
# 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

.namesObject



89
90
91
# File 'lib/yaks/format.rb', line 89

def names
  media_types.keys
end

.register(format_name, serializer, media_type) ⇒ Array

Parameters:

  • klass (Constant)
  • format_name (Symbol)
  • media_type (String)

Returns:

  • (Array)


50
51
52
53
54
55
56
# 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

Instance Method Details

#call(resource, env = {}) ⇒ Hash Also known as: serialize

Parameters:

Returns:

  • (Hash)


25
26
27
28
# File 'lib/yaks/format.rb', line 25

def call(resource, env = {})
  @env = env
  serialize_resource(resource)
end

#serialize_resource(_resource) ⇒ Object

This method is abstract.


32
33
# File 'lib/yaks/format.rb', line 32

def serialize_resource(_resource)
end