module Reactor::Attributes::LinkListExtender
Public Class Methods
extend_given_linklist!(klass)
click to toggle source
# File lib/reactor/attributes/link_list_extender.rb, line 11 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
extend_linklist!()
click to toggle source
# File lib/reactor/attributes/link_list_extender.rb, line 6 def self.extend_linklist! # this will trigger rails autoload extend_given_linklist!(RailsConnector::LinkList) end
Public Instance Methods
<<(link_data)
click to toggle source
Calls superclass method
# File lib/reactor/attributes/link_list_extender.rb, line 14 def <<(link_data) super(transform_into_link(link_data)) end
[]=(idx, value)
click to toggle source
Calls superclass method
# File lib/reactor/attributes/link_list_extender.rb, line 18 def []=(idx, value) super(idx, transform_into_link(value)) end
change!()
click to toggle source
# File lib/reactor/attributes/link_list_extender.rb, line 36 def change! @changed = true end
changed?()
click to toggle source
# File lib/reactor/attributes/link_list_extender.rb, line 32 def changed? @changed == true || temporary_links_present? end
detect_modification() { || ... }
click to toggle source
# File lib/reactor/attributes/link_list_extender.rb, line 53 def detect_modification(&block) original_link_ids yield.tap do @changed = @changed || original_link_ids != link_ids end
link_ids()
click to toggle source
# File lib/reactor/attributes/link_list_extender.rb, line 45 def link_ids self.map(&:id).compact end
original_link_ids()
click to toggle source
# File lib/reactor/attributes/link_list_extender.rb, line 40 def original_link_ids @original_link_ids ||= link_ids end
temporary_links_present?()
click to toggle source
# File lib/reactor/attributes/link_list_extender.rb, line 49 def temporary_links_present? self.any? {|l| l.kind_of? Reactor::Link::TemporaryLink } end
transform_into_link(link_data)
click to toggle source
# File lib/reactor/attributes/link_list_extender.rb, line 60 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