Class: Jei::Serializer
- Inherits:
-
Object
- Object
- Jei::Serializer
- Defined in:
- lib/jei/serializer.rb
Instance Attribute Summary collapse
- #resource ⇒ #id readonly
Class Method Summary collapse
- .attribute(name, &blk) ⇒ Object
- .attributes(name, ...) ⇒ Object
- .belongs_to(name, options = {}, &blk) ⇒ Object
- .factory(resource) ⇒ Serializer
- .has_many(name, options = {}, &blk) ⇒ Object
- .serialization_map ⇒ Hash<Symbol, Attribute>
Instance Method Summary collapse
- #attributes ⇒ Hash<Symbol, Attribute>
- #id ⇒ String
-
#initialize(resource) ⇒ Serializer
constructor
A new instance of Serializer.
- #links ⇒ Array<Link>?
- #relationships ⇒ Hash<Symbol, Relationship>
- #type ⇒ String
Constructor Details
#initialize(resource) ⇒ Serializer
Returns a new instance of Serializer.
46 47 48 |
# File 'lib/jei/serializer.rb', line 46 def initialize(resource) @resource = resource end |
Instance Attribute Details
Class Method Details
.attribute(name, &blk) ⇒ Object
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
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
25 26 27 28 29 |
# File 'lib/jei/serializer.rb', line 25 def self.belongs_to(name, = {}, &blk) value = block_given? ? blk : name serialization_map[:relationships][name] = BelongsToRelationship.new(name, value, ) end |
.factory(resource) ⇒ Serializer
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
32 33 34 35 36 |
# File 'lib/jei/serializer.rb', line 32 def self.has_many(name, = {}, &blk) value = block_given? ? blk : name serialization_map[:relationships][name] = HasManyRelationship.new(name, value, ) end |
.serialization_map ⇒ Hash<Symbol, Attribute>
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
#attributes ⇒ Hash<Symbol, Attribute>
61 62 63 |
# File 'lib/jei/serializer.rb', line 61 def attributes self.class.serialization_map[:attributes] end |
#id ⇒ String
51 52 53 |
# File 'lib/jei/serializer.rb', line 51 def id resource.id.to_s end |
#relationships ⇒ Hash<Symbol, Relationship>
66 67 68 |
# File 'lib/jei/serializer.rb', line 66 def relationships self.class.serialization_map[:relationships] end |
#type ⇒ String
56 57 58 |
# File 'lib/jei/serializer.rb', line 56 def type "#{resource.class.name.downcase}s" end |