Class: Fars::BaseCollectionSerializer
- Inherits:
-
Object
- Object
- Fars::BaseCollectionSerializer
- Defined in:
- lib/fars/base_collection_serializer.rb
Overview
Class: BaseCollectionSerializer
It is used to represent collection
Instance Method Summary collapse
-
#as_json ⇒ Object
Returns: Hash.
-
#initialize(objects, opts = {}, &block) ⇒ BaseCollectionSerializer
constructor
Constructor.
- #to_json ⇒ Object
Constructor Details
#initialize(objects, opts = {}, &block) ⇒ BaseCollectionSerializer
Constructor
Params:
- objects {ActiveRecord::Relation} or {Array} collection to serialize
- opts {Hash} of options:
- fields {Array} of attributes to serialize. Can be {NilClass}.
If so - will use all available.
- scope {Object} context of request. Usually current user
or current ability. Can be passed as a {Proc}. If so -
evaluated only when actually called.
- :add_metadata {Boolean} if to add a node '_metadata'
- :root_key {Symbol} overwrites the default one from serializer's Class
- :api_version {String} namespace for serializers classes, e.g. "V1"
- :class_name {String} serialized model class name
- :serializer {String} model serializer class name
- :metadata {Hash} optional hash with metadata (root_key should not be false)
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/fars/base_collection_serializer.rb', line 25 def initialize(objects, opts = {}, &block) @objects = objects if !opts.has_key?(:root_key) && !opts[:class_name] && empty_array? raise ArgumentError, 'Specify :root_key or model :class_name for empty array.' end # Cann't use Hash#fetch here, becouse if root_key provided default_root_key method should not be called. @root_key = opts.has_key?(:root_key) ? opts[:root_key] : default_root_key if !@root_key && opts[:metadata] raise ArgumentError, 'Can not omit :root_key if provided :metadata' end # Serialized model class name. @class_name = opts[:class_name] if opts[:serializer] if opts[:serializer].is_a? Proc @item_serializer = opts[:serializer] else @item_serializer_class = opts[:serializer].constantize end elsif block_given? @item_serializer = block end @api_version = opts[:api_version] @params = opts[:params] || {} @metadata = opts[:metadata] # Do not need options if serialize items with proc. unless @item_serializer # Options for model serializer. @options = opts.slice(:scope, :fields, :add_metadata, :api_version, :params) # If root_key is false, do not transfer this option to the model serializer class. @options[:root_key] = item_root_key if @root_key end end |
Instance Method Details
#as_json ⇒ Object
Returns: Hash
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fars/base_collection_serializer.rb', line 61 def as_json items = [] unless empty_array? @item_serializer ||= item_serializer_class.new(nil, ) objects.each do |object| items << item_serializer.call(object) end end return items unless root_key hash = { root_key => items } hash[:_metadata] = if hash end |
#to_json ⇒ Object
79 80 81 |
# File 'lib/fars/base_collection_serializer.rb', line 79 def to_json MultiJson.dump(as_json) end |