Class: Jei::Serializer

Inherits:
Object
  • Object
show all
Defined in:
lib/jei/serializer.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(resource) ⇒ Serializer

Returns a new instance of Serializer.

Parameters:

  • resource (#id)


46
47
48
# File 'lib/jei/serializer.rb', line 46

def initialize(resource)
  @resource = resource
end

Instance Attribute Details

#resource#id (readonly)

Returns:



4
5
6
# File 'lib/jei/serializer.rb', line 4

def resource
  @resource
end

Class Method Details

.attribute(name, &blk) ⇒ Object

Parameters:

  • name (Symbol)


19
20
21
22
# File 'lib/jei/serializer.rb', line 19

def self.attribute(name, &blk)
  value = block_given? ? blk : name
  serialization_map[:attributes][name] = Attribute.new(name, value)
end

.attributes(name, ...) ⇒ Object

Parameters:

  • name (Symbol)
  • ... (Symbol)


14
15
16
# File 'lib/jei/serializer.rb', line 14

def self.attributes(*names)
  names.each { |name| attribute(name) }
end

.belongs_to(name, options = {}, &blk) ⇒ Object

Parameters:

  • name (Symbol)


25
26
27
28
29
# File 'lib/jei/serializer.rb', line 25

def self.belongs_to(name, options = {}, &blk)
  value = block_given? ? blk : name
  serialization_map[:relationships][name] =
    BelongsToRelationship.new(name, value, options)
end

.factory(resource) ⇒ Serializer

Returns:



39
40
41
42
43
# File 'lib/jei/serializer.rb', line 39

def self.factory(resource)
  name = resource.class.name
  klass = const_get("#{name}Serializer")
  klass.new(resource)
end

.has_many(name, options = {}, &blk) ⇒ Object

Parameters:

  • name (Symbol)


32
33
34
35
36
# File 'lib/jei/serializer.rb', line 32

def self.has_many(name, options = {}, &blk)
  value = block_given? ? blk : name
  serialization_map[:relationships][name] =
    HasManyRelationship.new(name, value, options)
end

.serialization_mapHash<Symbol, Attribute>

Returns:



7
8
9
# File 'lib/jei/serializer.rb', line 7

def self.serialization_map
  @serialization_map ||= Hash.new { |h, k| h[k] = {} }
end

Instance Method Details

#attributesHash<Symbol, Attribute>

Returns:



61
62
63
# File 'lib/jei/serializer.rb', line 61

def attributes
  self.class.serialization_map[:attributes]
end

#idString

Returns:

  • (String)


51
52
53
# File 'lib/jei/serializer.rb', line 51

def id
  resource.id.to_s
end

Returns:

  • (Array<Link>, nil)


71
72
73
# File 'lib/jei/serializer.rb', line 71

def links
  nil
end

#relationshipsHash<Symbol, Relationship>

Returns:



66
67
68
# File 'lib/jei/serializer.rb', line 66

def relationships
  self.class.serialization_map[:relationships]
end

#typeString

Returns:

  • (String)


56
57
58
# File 'lib/jei/serializer.rb', line 56

def type
  "#{resource.class.name.downcase}s"
end