Module: Jinx::Inversible

Included in:
Resource
Defined in:
lib/jinx/resource/inversible.rb

Overview

Resource inverse integrity aspect mix-in. The Inversible methods are intended for the sole use of the Inverse class mix-in.

Instance Method Summary collapse

Instance Method Details

#add_to_inverse_collection(newval, accessors, inverse) { ... } ⇒ Object

Sets a collection attribute value in a way which enforces inverse integrity. The inverse of the attribute is a collection accessed by calling inverse on newval.

Parameters:

  • newval (Resource)

    the new attribute reference value

  • accessors ((Symbol, Symbol))

    the reader and writer to use in setting the attribute

  • inverse (Symbol)

    the inverse collection attribute to which this domain object will be added

Yields:

  • a factory to create a new collection on demand (default is an Array)



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/jinx/resource/inversible.rb', line 60

def add_to_inverse_collection(newval, accessors, inverse)
  rdr, wtr = accessors
  # the current inverse
  oldval = send(rdr)
  # no-op if no change
  return newval if newval == oldval

  # delete self from the current inverse reference collection
  if oldval then
    coll = oldval.send(inverse)
    coll.delete(self) if coll
  end
  
  # call the writer on this object
  send(wtr, newval)
  
  # add self to the inverse collection
  if newval then
    coll = newval.send(inverse)
    if coll.nil? then
      coll = block_given? ? yield : Array.new
      newval.set_property_value(inverse, coll)
    end
    coll << self
    if oldval then
      logger.debug { "Moved #{qp} from #{rdr} #{oldval.qp} #{inverse} to #{newval.qp}." }
    else
      logger.debug { "Added #{qp} to #{rdr} #{newval.qp} #{inverse}." }
    end
  end
  
  newval
end

#set_inverse(other, writer, inv_writer) ⇒ Object

Sets an attribute inverse by calling the attribute writer method with the other argument. If other is non-nil, then the inverse writer method is called on self.

Parameters:

  • other (Resource)

    the attribute value to set

  • writer (Symbol)

    the attribute writer method

  • inv_writer (Symbol)

    the attribute inverse writer method defined for the other object



12
13
14
15
# File 'lib/jinx/resource/inversible.rb', line 12

def set_inverse(other, writer, inv_writer)
  other.send(inv_writer, self) if other
  send(writer, other)
end

#set_inversible_noncollection_attribute(newval, accessors, inverse_writer) ⇒ Object

Sets a non-collection attribute value in a way which enforces inverse integrity.

Parameters:

  • newval (Object)

    the value to set

  • accessors ((Symbol, Symbol))

    the reader and writer methods to use in setting the attribute

  • inverse_writer (Symbol)

    the inverse attribute writer method



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/jinx/resource/inversible.rb', line 24

def set_inversible_noncollection_attribute(newval, accessors, inverse_writer)
  rdr, wtr = accessors
  # the previous value
  oldval = send(rdr)
  # bail if no change
  return newval if newval.equal?(oldval)

  # clear the previous inverse
  logger.debug { "Moving #{qp} from #{oldval.qp} to #{newval.qp}..." } if oldval and newval
  if oldval then
    clr_wtr = self.class === oldval && oldval.send(rdr).equal?(self) ? wtr : inverse_writer
    oldval.send(clr_wtr, nil)
  end
  
  # call the writer
  send(wtr, newval)
  
  # call the inverse writer on self
  if newval then
    newval.send(inverse_writer, self)
    logger.debug { "Moved #{qp} from #{oldval.qp} to #{newval.qp}." } if oldval
  end
  
  newval
end