Class: Bluepine::Serializer
- Inherits:
-
Attributes::Visitor
- Object
- Attributes::Visitor
- Bluepine::Serializer
- Includes:
- Resolvable
- Defined in:
- lib/bluepine/serializer.rb
Overview
user_schema = Attributes.create(:object) do
string :username
string :email
end
user = <any object>
resolver = Resolver.new(schemas: [user_schema, …]) serializer.serialize(user_schema, user)
Constant Summary collapse
- InvalidPredicate =
Bluepine::Error.create("Invalid predicate value (must be either Symbol or Proc)")
Constants included from Resolvable
Constants inherited from Attributes::Visitor
Attributes::Visitor::MethodNotFound
Instance Method Summary collapse
-
#initialize(resolver = nil) ⇒ Serializer
constructor
A new instance of Serializer.
-
#normalize_attribute(attribute, object, options = {}) ⇒ Object
Override to make it accepts 3 arguments.
- #visit_array(attribute, object, options = {}) ⇒ Object
-
#visit_attribute(attribute, object, options = {}) ⇒ Object
catch all.
- #visit_object(attribute, object, options = {}) ⇒ Object
- #visit_schema(attribute, object, options = {}) ⇒ Object
Methods included from Resolvable
Methods inherited from Attributes::Visitor
Methods included from Functions
#compose, #compose_result, #curry, #result
Constructor Details
#initialize(resolver = nil) ⇒ Serializer
Returns a new instance of Serializer.
16 17 18 |
# File 'lib/bluepine/serializer.rb', line 16 def initialize(resolver = nil) @resolver = resolver end |
Instance Method Details
#normalize_attribute(attribute, object, options = {}) ⇒ Object
Override to make it accepts 3 arguments
21 22 23 |
# File 'lib/bluepine/serializer.rb', line 21 def normalize_attribute(attribute, object, = {}) super(attribute, ) end |
#visit_array(attribute, object, options = {}) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/bluepine/serializer.rb', line 66 def visit_array(attribute, object, = {}) as = attribute.of Array(object).map do |item| # item#serialize_as will be used when of: option is not specified. # e.g. ListSerializer.schema has `array :data` # as = attribute.of || (item.serialize_as if item.respond_to?(:serialize_as)) unless as.kind_of?(Symbol) item else visit(as, item, ) end end end |
#visit_attribute(attribute, object, options = {}) ⇒ Object
catch all
26 27 28 |
# File 'lib/bluepine/serializer.rb', line 26 def visit_attribute(attribute, object, = {}) attribute.serialize(attribute.value(object)) end |
#visit_object(attribute, object, options = {}) ⇒ Object
60 61 62 63 64 |
# File 'lib/bluepine/serializer.rb', line 60 def visit_object(attribute, object, = {}) visit_object_handler(attribute, object) do |attr, value, attrs| attrs[attr.name] = visit(attr, value, ) end end |
#visit_schema(attribute, object, options = {}) ⇒ Object
81 82 83 84 85 |
# File 'lib/bluepine/serializer.rb', line 81 def visit_schema(attribute, object, = {}) attribute = resolver.schema(attribute.of) visit_object(attribute, object, ) end |