class Seri::Serializer
Constants
- Attribute
- VERSION
Attributes
object[RW]
scope[RW]
Public Class Methods
attribute(key, condition: nil, from: nil, serializer: nil, **options)
click to toggle source
# File lib/serializer.rb, line 14 def self.attribute(key, condition: nil, from: nil, serializer: nil, **options) attributes.push(Attribute.new(key, condition, from, serializer, options)) end
attributes()
click to toggle source
# File lib/serializer.rb, line 10 def self.attributes @attributes ||= [] end
new(object, scope: {})
click to toggle source
# File lib/serializer.rb, line 20 def initialize(object, scope: {}) @object = object @scope = scope end
Public Instance Methods
to_h()
click to toggle source
Loops over all attributes and skips if a condition is defined and falsey
# File lib/serializer.rb, line 26 def to_h self.class.attributes.each_with_object({}) do |attribute, obj| next if attribute.condition && !public_send(attribute.condition) obj[attribute.key] = ValueFetcher.fetch(attribute, object, self) end end
to_json(*)
click to toggle source
# File lib/serializer.rb, line 34 def to_json(*) Oj.dump(to_h, mode: :json) end