Class: Waveguide::Serializer
- Inherits:
-
Object
- Object
- Waveguide::Serializer
- Defined in:
- lib/waveguide/serializer.rb
Class Attribute Summary collapse
-
.attributes_to_serialize ⇒ Object
Returns the value of attribute attributes_to_serialize.
-
.conditional_attributes ⇒ Object
Returns the value of attribute conditional_attributes.
-
.serializer_methods ⇒ Object
Returns the value of attribute serializer_methods.
Instance Attribute Summary collapse
-
#object ⇒ Object
readonly
Returns the value of attribute object.
-
#scope ⇒ Object
readonly
Returns the value of attribute scope.
Class Method Summary collapse
- .add_attribute(attr_name) ⇒ Object
- .add_conditional_attribute(attr_name) ⇒ Object
- .attribute(attr_name, &block) ⇒ Object
- .attributes(*attr_names) ⇒ Object
- .define_serializer_method(name, &block) ⇒ Object
- .has_many(relation_name, as: nil, serializer: nil) ⇒ Object
- .has_one(relation_name, as: nil, serializer: nil) ⇒ Object
- .include?(attr_name, &block) ⇒ Boolean
- .inherited(base) ⇒ Object
Instance Method Summary collapse
- #as_json ⇒ Object
-
#initialize(object, scope = nil) ⇒ Serializer
constructor
A new instance of Serializer.
Constructor Details
#initialize(object, scope = nil) ⇒ Serializer
Returns a new instance of Serializer.
6 7 8 9 |
# File 'lib/waveguide/serializer.rb', line 6 def initialize(object, scope = nil) @object = object @scope = scope end |
Class Attribute Details
.attributes_to_serialize ⇒ Object
Returns the value of attribute attributes_to_serialize.
31 32 33 |
# File 'lib/waveguide/serializer.rb', line 31 def attributes_to_serialize @attributes_to_serialize end |
.conditional_attributes ⇒ Object
Returns the value of attribute conditional_attributes.
31 32 33 |
# File 'lib/waveguide/serializer.rb', line 31 def conditional_attributes @conditional_attributes end |
.serializer_methods ⇒ Object
Returns the value of attribute serializer_methods.
31 32 33 |
# File 'lib/waveguide/serializer.rb', line 31 def serializer_methods @serializer_methods end |
Instance Attribute Details
#object ⇒ Object (readonly)
Returns the value of attribute object.
4 5 6 |
# File 'lib/waveguide/serializer.rb', line 4 def object @object end |
#scope ⇒ Object (readonly)
Returns the value of attribute scope.
4 5 6 |
# File 'lib/waveguide/serializer.rb', line 4 def scope @scope end |
Class Method Details
.add_attribute(attr_name) ⇒ Object
46 47 48 49 50 51 52 |
# File 'lib/waveguide/serializer.rb', line 46 def add_attribute(attr_name) return if self.conditional_attributes.include?(attr_name) self.attributes_to_serialize ||= [] self.attributes_to_serialize << attr_name self.attributes_to_serialize.uniq! end |
.add_conditional_attribute(attr_name) ⇒ Object
54 55 56 57 58 59 60 |
# File 'lib/waveguide/serializer.rb', line 54 def add_conditional_attribute(attr_name) self.conditional_attributes ||= [] self.conditional_attributes << attr_name self.conditional_attributes.uniq! self.attributes_to_serialize.delete(attr_name) end |
.attribute(attr_name, &block) ⇒ Object
75 76 77 78 79 80 81 82 83 |
# File 'lib/waveguide/serializer.rb', line 75 def attribute(attr_name, &block) add_attribute(attr_name) if block.nil? define_serializer_method(attr_name) { object.send(attr_name) } else define_serializer_method(attr_name, &block) end end |
.attributes(*attr_names) ⇒ Object
69 70 71 72 73 |
# File 'lib/waveguide/serializer.rb', line 69 def attributes(*attr_names) attr_names.each do |attr_name| attribute(attr_name) end end |
.define_serializer_method(name, &block) ⇒ Object
62 63 64 65 66 67 |
# File 'lib/waveguide/serializer.rb', line 62 def define_serializer_method(name, &block) self.serializer_methods ||= {} self.serializer_methods[name] = block define_method(name, &block) end |
.has_many(relation_name, as: nil, serializer: nil) ⇒ Object
95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/waveguide/serializer.rb', line 95 def has_many(relation_name, as: nil, serializer: nil) key = as || relation_name add_attribute(key) if serializer define_serializer_method(key) do object.send(relation_name).map do |item| serializer.new(item, scope).as_json end end end end |
.has_one(relation_name, as: nil, serializer: nil) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'lib/waveguide/serializer.rb', line 85 def has_one(relation_name, as: nil, serializer: nil) key = as || relation_name add_attribute(key) if serializer define_serializer_method(key) do serializer.new(object.send(relation_name), scope).as_json end end end |
.include?(attr_name, &block) ⇒ Boolean
107 108 109 110 |
# File 'lib/waveguide/serializer.rb', line 107 def include?(attr_name, &block) add_conditional_attribute(attr_name) define_serializer_method("include_#{attr_name}?", &block) end |
.inherited(base) ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'lib/waveguide/serializer.rb', line 35 def inherited(base) super base.attributes_to_serialize = (attributes_to_serialize || []).dup base.conditional_attributes = (conditional_attributes || []).dup base.serializer_methods = (serializer_methods || {}).dup (self.serializer_methods || {}).each do |key, block| base.send(:define_method, key, &block) end end |
Instance Method Details
#as_json ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/waveguide/serializer.rb', line 11 def as_json self.class.attributes_to_serialize ||= [] self.class.conditional_attributes ||= [] self.class.serializer_methods ||= {} output = {} self.class.attributes_to_serialize.each do |key| output[key] = self.send(key) end self.class.conditional_attributes.each do |key| output[key] = self.send(key) if self.send("include_#{key}?") end output end |