Class: Jei::Serializer
- Inherits:
-
Object
- Object
- Jei::Serializer
- Defined in:
- lib/jei/serializer.rb
Instance Attribute Summary collapse
- #resource ⇒ Object readonly
Class Method Summary collapse
- .attribute(name, &blk) ⇒ Object
- .attributes(name, ...) ⇒ Object
- .belongs_to(name, options = {}, &blk) ⇒ Object
-
.factory(resource, klass = nil) ⇒ Serializer
Instantiates a new serializer based on the type of the given resource.
- .fields ⇒ Hash<Symbol, Attribute>
- .has_many(name, options = {}, &blk) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Boolean (also: #eql?)
- #attributes(fieldset = nil) ⇒ Hash<Symbol, Attribute>
-
#hash ⇒ Fixnum
Returns a digest for this serializer.
- #id ⇒ String
-
#initialize(resource, options = nil) ⇒ Serializer
constructor
A new instance of Serializer.
- #key ⇒ Array<String>
- #links ⇒ Array<Link>?
- #options ⇒ Hash<Symbol, Object>
- #relationships(fieldset = nil) ⇒ Hash<Symbol, Relationship>
- #type ⇒ String
Constructor Details
#initialize(resource, options = nil) ⇒ Serializer
Returns a new instance of Serializer.
66 67 68 69 |
# File 'lib/jei/serializer.rb', line 66 def initialize(resource, = nil) @resource = resource = end |
Instance Attribute Details
#resource ⇒ Object (readonly)
4 5 6 |
# File 'lib/jei/serializer.rb', line 4 def resource @resource end |
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 fields[: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
27 28 29 30 31 |
# File 'lib/jei/serializer.rb', line 27 def self.belongs_to(name, = {}, &blk) value = block_given? ? blk : name fields[:relationships][name] = BelongsToRelationship.new(name, value, ) end |
.factory(resource, klass = nil) ⇒ Serializer
Instantiates a new serializer based on the type of the given resource.
This assumes serializer classes are defined in the global namespace. If not, a serializer class can be passed to override the lookup.
59 60 61 62 |
# File 'lib/jei/serializer.rb', line 59 def self.factory(resource, klass = nil) klass ||= const_get("#{resource.class.name}Serializer") klass.new(resource) end |
.fields ⇒ Hash<Symbol, Attribute>
7 8 9 |
# File 'lib/jei/serializer.rb', line 7 def self.fields @fields ||= Hash.new { |h, k| h[k] = {} } end |
.has_many(name, options = {}, &blk) ⇒ Object
36 37 38 39 40 |
# File 'lib/jei/serializer.rb', line 36 def self.has_many(name, = {}, &blk) value = block_given? ? blk : name fields[:relationships][name] = HasManyRelationship.new(name, value, ) end |
Instance Method Details
#==(other) ⇒ Boolean Also known as: eql?
109 110 111 |
# File 'lib/jei/serializer.rb', line 109 def ==(other) hash == other.hash end |
#attributes(fieldset = nil) ⇒ Hash<Symbol, Attribute>
83 84 85 |
# File 'lib/jei/serializer.rb', line 83 def attributes(fieldset = nil) fields(:attributes, fieldset) end |
#hash ⇒ Fixnum
Returns a digest for this serializer.
The second element is shifted to preserve order.
119 120 121 |
# File 'lib/jei/serializer.rb', line 119 def hash type.hash ^ (id.hash >> 1) end |
#id ⇒ String
72 73 74 |
# File 'lib/jei/serializer.rb', line 72 def id resource.id.to_s end |
#key ⇒ Array<String>
104 105 106 |
# File 'lib/jei/serializer.rb', line 104 def key [type, id] end |
#options ⇒ Hash<Symbol, Object>
99 100 101 |
# File 'lib/jei/serializer.rb', line 99 def ||= {} end |
#relationships(fieldset = nil) ⇒ Hash<Symbol, Relationship>
89 90 91 |
# File 'lib/jei/serializer.rb', line 89 def relationships(fieldset = nil) fields(:relationships, fieldset) end |
#type ⇒ String
77 78 79 |
# File 'lib/jei/serializer.rb', line 77 def type "#{resource.class.name.downcase}s" end |