Sha256: bccd6dc5d5a7ba97122177119b5779f92a163b744c1ab67a3807e5566621562f

Contents?: true

Size: 1.61 KB

Versions: 31

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module FmRest
  module Spyke
    # Extend Spyke's HasMany association with custom options
    #
    class Portal < ::Spyke::Associations::HasMany
      def initialize(*args)
        super

        # Portals are always embedded, so no special URI
        @options[:uri] = ""
      end

      def portal_key
        (@options[:portal_key] || name).to_s
      end

      def attribute_prefix
        (@options[:attribute_prefix] || portal_key).to_s
      end

      # Callback method, not meant to be used directly
      #
      def parent_changes_applied
        each(&:changes_applied)
      end

      def <<(*records)
        records.flatten.each { |r| add_to_parent(r) }
        self
      end
      alias_method :push, :<<
      alias_method :concat, :<<

      def _remove_marked_for_destruction
        find_some.reject!(&:marked_for_destruction?)
      end

      private

      # Spyke::Associations::HasMany#initialize calls primary_key to build the
      # default URI, which causes a NameError, so this is here just to prevent
      # that. We don't care what it returns as we override the URI with nil
      # anyway
      def primary_key; end

      # Make sure the association doesn't try to fetch records through URI
      def uri; nil; end

      def embedded_data
        parent.attributes[portal_key]
      end

      # Spyke override
      #
      def add_to_parent(record)
        raise ArgumentError, "Expected an instance of #{klass}, got a #{record.class} instead" unless record.kind_of?(klass)
        find_some << record
        record.embedded_in_portal
        record
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
fmrest-spyke-0.26.2 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.26.1 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.26.1.rc1 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.26.0 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.26.0.rc1 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.25.0.rc1 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.24.0 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.24.0.rc1 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.23.1 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.23.0 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.22.0 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.21.0 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.21.0.rc1 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.20.0 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.20.0.rc1 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.19.0 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.19.0.rc1 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.18.0 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.18.0.rc3 lib/fmrest/spyke/portal.rb
fmrest-spyke-0.18.0.rc2 lib/fmrest/spyke/portal.rb