Last active
November 23, 2020 19:17
-
-
Save joshmn/dc2c2f7e5440a7a71574f97c8b95cc4b to your computer and use it in GitHub Desktop.
simple serializer pattern
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ApplicationSerializer | |
attr_reader :object | |
def initialize(object, options = {}) | |
@object = object | |
@options = options | |
end | |
def to_json | |
JSON.generate(as_json) | |
end | |
def as_json | |
raise NotImplementedError | |
end | |
end | |
module FancyAttributeDSL | |
def self.included(klass) | |
klass.extend ClassMethods | |
end | |
def as_json | |
hash = {} | |
self.class._serialized_attributes.each do |attr, block| | |
hash[attr] = read_attribute_for_serialization(attr, &block) | |
end | |
hash | |
end | |
private | |
def read_attribute_for_serialization(attr, &block) | |
if block | |
instance_eval(&block) | |
elsif respond_to?(attr) | |
send(attr) | |
else | |
object.send(attr) | |
end | |
end | |
module ClassMethods | |
def _serialized_attributes | |
@_serialized_attributes ||= {} | |
end | |
def attributes(*keys) | |
keys.each { |key| _serialized_attributes[key] = nil } | |
end | |
def attribute(key, &block) | |
_serialized_attributes[key] = block | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: