class PolyBelongsTo::SingletonSet
PolyBelongsTo::SingletonSet
is a modified instance of Set for maintaining singletons of ActiveRecord objects during any recursive flow.
Public Class Methods
Creates two internal Sets @return [self]
# File lib/poly_belongs_to/singleton_set.rb, line 8 def initialize @set = Set.new @flagged = Set.new self end
Public Instance Methods
Add record to set. Flag if covered already. @param record [Object] ActiveRecord object instance @return [Object, nil] Object if added safely, nil otherwise
# File lib/poly_belongs_to/singleton_set.rb, line 24 def add?(record) result = @set.add?( formatted_name( record ) ) return result if result flag(record) result end
Add record to flagged Set list @param record [Object] ActiveRecord object instance @return [Set]
# File lib/poly_belongs_to/singleton_set.rb, line 42 def flag(record) @flagged << formatted_name(record) end
Boolean of record inclusion in flagged Set @param record [Object] ActiveRecord object instance @return [true, false]
# File lib/poly_belongs_to/singleton_set.rb, line 49 def flagged?(record) return nil unless record @flagged.include?(formatted_name(record)) end
Takes ActiveRecord object and returns string of class name and object id @param record [Object] ActiveRecord object instance @return [String]
# File lib/poly_belongs_to/singleton_set.rb, line 17 def formatted_name(record) "#{record.class.name}-#{record.id}" end
Boolean of record inclusion @param record [Object] ActiveRecord object instance @return [true, false]
# File lib/poly_belongs_to/singleton_set.rb, line 34 def include?(record) return nil unless record @set.include?(formatted_name(record)) end
method_missing
will transform any record argument into a formatted string and pass the method and arguments on to the internal Set. Also will flag any existing records covered.
# File lib/poly_belongs_to/singleton_set.rb, line 56 def method_missing(mthd, *args, &block) new_recs = args.reduce([]) {|a, i| a.push(formatted_name(i)) if i.class.ancestors.include?(ActiveRecord::Base); a} result = @set.send(mthd, *(args.map do |arg| arg.class.ancestors.include?(ActiveRecord::Base) ? formatted_name(arg) : arg end ), &block ) @set.to_a.select {|i| new_recs.include? i }.each {|f| @flagged << f} result end