module JsonapiSerializer::DSL::Common
Attributes
meta_attributes[R]
meta_id[R]
meta_relationships[R]
meta_type[R]
Public Instance Methods
attribute(attr, &block)
click to toggle source
# File lib/jsonapi_serializer/dsl/common.rb, line 50 def attribute(attr, &block) @meta_attributes[attr.to_sym] = block_given? ? block : lambda { |obj| obj.public_send(attr) } end
attributes(*attrs)
click to toggle source
# File lib/jsonapi_serializer/dsl/common.rb, line 37 def attributes(*attrs) attrs.each do |attr| case attr when Symbol, String @meta_attributes[attr.to_sym] = lambda { |obj| obj.public_send(attr) } when Hash attr.each do |key, val| @meta_attributes[key] = lambda { |obj| obj.public_send(val) } end end end end
id(attr = nil, &block)
click to toggle source
# File lib/jsonapi_serializer/dsl/common.rb, line 26 def id(attr = nil, &block) case when attr.nil? && block_given? @meta_id = block when attr.present? && !block_given? @meta_id = lambda { |obj| obj.public_send(attr) } else raise ArgumentError, "ID hook requires either attribute name or block" end end
inherited(subclass)
click to toggle source
# File lib/jsonapi_serializer/dsl/common.rb, line 64 def inherited(subclass) raise "You attempted to inherit from #{self.name}, if you want to create Polymorphic serializer, include JsonapiSerializer::Polymorphic" end
relationship(name, opts = {})
click to toggle source
# File lib/jsonapi_serializer/dsl/common.rb, line 54 def relationship(name, opts = {}) @meta_relationships[name.to_sym] = {}.tap do |relationship| from = opts.fetch(:from, name) relationship[:from] = from.respond_to?(:call) ? from : lambda { |r| r.public_send(from) } serializer = opts[:serializer] relationship[:serializer] = serializer ? serializer.to_s : "#{name.to_s.singularize.classify}Serializer" end end
type(name)
click to toggle source
# File lib/jsonapi_serializer/dsl/common.rb, line 22 def type(name) @meta_type = name.to_sym end