class Apiarist::Serializer
Attributes
context[R]
Public Class Methods
association(name, opts = {})
click to toggle source
# File lib/apiarist/serializer.rb, line 54 def association(name, opts = {}) attribute(name, opts.merge(type: :association)) end
associations(*associations)
click to toggle source
# File lib/apiarist/serializer.rb, line 58 def associations(*associations) opts = associations.extract_options! associations.each { |name| association(name, opts) } end
attribute(name, opts = {})
click to toggle source
# File lib/apiarist/serializer.rb, line 44 def attribute(name, opts = {}) self._keys = _keys.dup _keys[name] = opts end
attributes(*names)
click to toggle source
# File lib/apiarist/serializer.rb, line 49 def attributes(*names) opts = names.extract_options! names.each { |name| attribute(name, opts) } end
new(resource, context = self, opts = {})
click to toggle source
Calls superclass method
# File lib/apiarist/serializer.rb, line 10 def initialize(resource, context = self, opts = {}) @resource = resource, @context = context @options = opts super(resource) end
scope(name, &block)
click to toggle source
# File lib/apiarist/serializer.rb, line 63 def scope(name, &block) with_options(if: name) do |scope| scope.instance_eval(&block) end end
Public Instance Methods
as_json(_ = {})
click to toggle source
# File lib/apiarist/serializer.rb, line 18 def as_json(_ = {}) json = {} _keys.each_with_object(json) do |(name, opts), hash| serialization_opts = opts.dup key = serialization_opts.delete(:as) || name if serialization_scope_include?(serialization_opts.delete(:if)) case serialization_opts.delete(:type) when :association value = serialize(send(name), serialization_opts) else value = send(name) end hash[key] = value end end json end
serialization_context()
click to toggle source
# File lib/apiarist/serializer.rb, line 39 def serialization_context context.serialization_context end
Private Instance Methods
serialization_scope_include?(scope)
click to toggle source
# File lib/apiarist/serializer.rb, line 75 def serialization_scope_include?(scope) [@options[:scope], nil].flatten.include?(scope) end