Sha256: 6a14a7a9196e779d46e2f53ac6cfbcbeea037cd5176ebb1d37e9a73e80e8c403

Contents?: true

Size: 1.97 KB

Versions: 23

Compression:

Stored size: 1.97 KB

Contents

# -*- encoding : utf-8 -*-
require 'reactor/link/temporary_link'

module Reactor
  module Attributes
    module LinkListExtender
      def self.extend_linklist!
        # this will trigger rails autoload
        extend_given_linklist!(RailsConnector::LinkList)
      end

      def self.extend_given_linklist!(klass)
        unless klass.instance_methods(false).include?("changed?")
          klass.class_eval do
            def <<(link_data)
              super(transform_into_link(link_data))
            end

            def []=(idx, value)
              super(idx, transform_into_link(value))
            end

            # install #size_changed callback
            Array.instance_methods(false).each do |meth|
              old = instance_method(meth)
              define_method(meth) do |*args, &block|
                detect_modification do
                  old.bind(self).call(*args, &block)
                end
              end if meth.to_sym != :map
            end

            def changed?
              @changed == true || temporary_links_present?
            end

            def change!
              @changed = true
            end

            def original_link_ids
              @original_link_ids ||= link_ids
            end

            protected
            def link_ids
              self.map(&:id).compact
            end

            def temporary_links_present?
              self.any? {|l| l.kind_of? Reactor::Link::TemporaryLink }
            end

            def detect_modification(&block)
              original_link_ids
              yield.tap do
                @changed = @changed || original_link_ids != link_ids
              end
            end

            def transform_into_link(link_data)
              if (link_data.respond_to?(:external?) && link_data.respond_to?(:internal?))
                link_data
              else
                Reactor::Link::TemporaryLink.new(link_data)
              end
            end
          end
        end
      end
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
infopark_reactor-1.26.1 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.26.0 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.25.2 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.25.1 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.25.0 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.24.1 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.24.0 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.23.1 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.23.0 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.23.0.beta2 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.22.6 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.22.5 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.22.4 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.22.3 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.22.2 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.22.1 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.22.0 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.21.0 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.20.4 lib/reactor/attributes/link_list_extender.rb
infopark_reactor-1.20.3 lib/reactor/attributes/link_list_extender.rb