Sha256: 8f410c590000f063670065c111ed7237c30d59e3ced4d7c1e9a48386469e1005

Contents?: true

Size: 997 Bytes

Versions: 4

Compression:

Stored size: 997 Bytes

Contents

# frozen_string_literal: true

module PubSubModelSync
  class Publisher
    attr_accessor :attrs, :actions, :klass, :as_klass

    def initialize(attrs, klass, actions = nil, as_klass = nil)
      @attrs = attrs
      @klass = klass
      @actions = actions || %i[create update destroy]
      @as_klass = as_klass || klass
    end

    def payload(model, action)
      { data: payload_data(model), attrs: payload_attrs(model, action) }
    end

    private

    def payload_data(model)
      source_props = @attrs.map { |prop| prop.to_s.split(':').first }
      data = model.as_json(only: source_props, methods: source_props)
      aliased_props = @attrs.select { |prop| prop.to_s.include?(':') }
      aliased_props.each do |prop|
        source, target = prop.to_s.split(':')
        data[target] = data.delete(source)
      end
      data.symbolize_keys
    end

    def payload_attrs(model, action)
      { klass: (as_klass || model.class.name).to_s, action: action.to_sym }
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pub_sub_model_sync-0.5.1.1 lib/pub_sub_model_sync/publisher.rb
pub_sub_model_sync-0.5.1 lib/pub_sub_model_sync/publisher.rb
pub_sub_model_sync-0.5.0.1 lib/pub_sub_model_sync/publisher.rb
pub_sub_model_sync-0.5.0 lib/pub_sub_model_sync/publisher.rb