Sha256: 7513532bf7fdbfe571b1dce7d36bf28a1e7c735c6b2175d3340693df2ac02a15

Contents?: true

Size: 1.97 KB

Versions: 60

Compression:

Stored size: 1.97 KB

Contents

class ForestLiana::SchemaSerializer
  def initialize collections, meta
    @collections = collections
    @meta = meta
    @data = []
    @included = []
  end

  def serialize
    populate_data_and_included
    {
      data: @data,
      included: @included,
      meta: @meta
    }
  end

  private

  def populate_data_and_included
    @collections.each do |collection|
      serialize_collection(collection)
    end
  end

  def serialize_collection collection
    collection_serialized = {
      id: collection['name'],
      type: 'collections',
      attributes: {},
      relationships: {
        actions: {
          data: []
        },
        segments: {
          data: []
        }
      }
    }

    collection.each do |attribute, value|
      if attribute == 'actions'
        value.each do |action|
          action_id = define_child_id(collection_serialized[:id], action['name'])
          collection_serialized[:relationships][:actions][:data] << format_child_pointer('actions', action_id)
          @included << format_child_content('actions', action_id, action)
        end
      elsif attribute == 'segments'
        value.each do |segment|
          segment_id = define_child_id(collection_serialized[:id], segment['name'])
          collection_serialized[:relationships][:segments][:data] << format_child_pointer('segments', segment_id)
          @included << format_child_content('segments', segment_id, segment)
        end
      else
        collection_serialized[:attributes][attribute.to_sym] = value;
      end
    end

    @data << collection_serialized
  end

  def define_child_id collection_id, object_id
    "#{collection_id}.#{object_id}"
  end

  def format_child_pointer type, id
    { id: id, type: type }
  end

  def format_child_content type, id, object
    child_serialized = {
      id: id,
      type: type,
      attributes: {}
    }

    object.each do |attribute, value|
      child_serialized[:attributes][attribute.to_sym] = value;
    end

    child_serialized
  end
end

Version data entries

60 entries across 60 versions & 1 rubygems

Version Path
forest_liana-5.4.4 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.4.3 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.4.2 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.4.1 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.4.0 app/serializers/forest_liana/schema_serializer.rb
forest_liana-6.0.0.pre.beta.1 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.3.3 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.3.2 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.3.1 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.3.0 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.2.3 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.2.2 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.2.1 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.2.0 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.1.3 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.1.2 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.1.1 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.1.0 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.0.0 app/serializers/forest_liana/schema_serializer.rb
forest_liana-5.0.0.pre.beta.0 app/serializers/forest_liana/schema_serializer.rb