Class: PolyBelongsTo::SingletonSet

Inherits:
Object
  • Object
show all
Defined in:
lib/poly_belongs_to/singleton_set.rb

Overview

PolyBelongsTo::SingletonSet is a modified instance of Set for maintaining singletons of ActiveRecord objects during any recursive flow.

Instance Method Summary collapse

Constructor Details

#initializeself

Creates two internal Sets



8
9
10
11
12
# File 'lib/poly_belongs_to/singleton_set.rb', line 8

def initialize
  @set = Set.new
  @flagged = Set.new
  self
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(mthd, *args, &block) ⇒ Object

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.



56
57
58
59
60
61
62
63
64
65
66
67
# 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

Instance Method Details

#add?(record) ⇒ Object?

Add record to set. Flag if covered already.

Parameters:

  • record (Object)

    ActiveRecord object instance

Returns:

  • (Object, nil)

    Object if added safely, nil otherwise



24
25
26
27
28
29
# 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

#flag(record) ⇒ Set

Add record to flagged Set list

Parameters:

  • record (Object)

    ActiveRecord object instance

Returns:

  • (Set)


42
43
44
# File 'lib/poly_belongs_to/singleton_set.rb', line 42

def flag(record)
  @flagged << formatted_name(record)
end

#flagged?(record) ⇒ true, false

Boolean of record inclusion in flagged Set

Parameters:

  • record (Object)

    ActiveRecord object instance

Returns:

  • (true, false)


49
50
51
52
# File 'lib/poly_belongs_to/singleton_set.rb', line 49

def flagged?(record)
  return nil unless record
  @flagged.include?(formatted_name(record))
end

#formatted_name(record) ⇒ String

Takes ActiveRecord object and returns string of class name and object id

Parameters:

  • record (Object)

    ActiveRecord object instance

Returns:

  • (String)


17
18
19
# File 'lib/poly_belongs_to/singleton_set.rb', line 17

def formatted_name(record)
  "#{record.class.name}-#{record.id}"
end

#include?(record) ⇒ true, false

Boolean of record inclusion

Parameters:

  • record (Object)

    ActiveRecord object instance

Returns:

  • (true, false)


34
35
36
37
# File 'lib/poly_belongs_to/singleton_set.rb', line 34

def include?(record)
  return nil unless record
  @set.include?(formatted_name(record))
end