rails-erd 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,118 +0,0 @@
1
- module RailsERD
2
- class Relationship
3
- class Cardinality
4
- N = Infinity = 1.0/0 # And beyond.
5
-
6
- CLASSES = {
7
- [1, 1] => :one_to_one,
8
- [1, N] => :one_to_many,
9
- [N, 1] => :many_to_one,
10
- [N, N] => :many_to_many
11
- } # @private :nodoc:
12
-
13
- # Returns a range that indicates the source (left) cardinality.
14
- attr_reader :source_range
15
-
16
- # Returns a range that indicates the destination (right) cardinality.
17
- attr_reader :destination_range
18
-
19
- # Create a new cardinality based on a source range and a destination
20
- # range. These ranges describe which number of values are valid.
21
- def initialize(source_range, destination_range) # @private :nodoc:
22
- @source_range = compose_range(source_range)
23
- @destination_range = compose_range(destination_range)
24
- end
25
-
26
- # Returns the name of this cardinality, based on its two cardinal
27
- # numbers (for source and destination). Can be any of
28
- # +:one_to_one:+, +:one_to_many+, or +:many_to_many+. The name
29
- # +:many_to_one+ also exists, but Rails ERD always normalises these
30
- # kinds of relationships by inversing them, so they become
31
- # +:one_to_many+ associations.
32
- #
33
- # You can also call the equivalent method with a question mark, which
34
- # will return true if the name corresponds to that method. For example:
35
- #
36
- # cardinality.one_to_one?
37
- # #=> true
38
- # cardinality.one_to_many?
39
- # #=> false
40
- def name
41
- CLASSES[cardinality_class]
42
- end
43
-
44
- # Returns +true+ if the source (left side) is not mandatory.
45
- def source_optional?
46
- source_range.first < 1
47
- end
48
-
49
- # Returns +true+ if the destination (right side) is not mandatory.
50
- def destination_optional?
51
- destination_range.first < 1
52
- end
53
-
54
- # Returns the inverse cardinality. Destination becomes source, source
55
- # becomes destination.
56
- def inverse
57
- self.class.new destination_range, source_range
58
- end
59
-
60
- CLASSES.each do |cardinality_class, name|
61
- class_eval <<-RUBY
62
- def #{name}?
63
- cardinality_class == #{cardinality_class.inspect}
64
- end
65
- RUBY
66
- end
67
-
68
- def ==(other) # @private :nodoc:
69
- source_range == other.source_range and destination_range == other.destination_range
70
- end
71
-
72
- def <=>(other) # @private :nodoc:
73
- (cardinality_class <=> other.cardinality_class).nonzero? or
74
- compare_with(other) { |x| x.source_range.first + x.destination_range.first }.nonzero? or
75
- compare_with(other) { |x| x.source_range.last + x.destination_range.last }.nonzero? or
76
- compare_with(other) { |x| x.source_range.last }.nonzero? or
77
- compare_with(other) { |x| x.destination_range.last }
78
- end
79
-
80
- def inspect # @private :nodoc:
81
- "#<#{self.class}:0x%.14x (%s,%s) => (%s,%s)>" %
82
- [object_id << 1, source_range.first, source_range.last, destination_range.first, destination_range.last]
83
- end
84
-
85
- # Returns an array with the cardinality classes for the source and
86
- # destination of this cardinality. Possible return values are:
87
- # <tt>[1, 1]</tt>, <tt>[1, N]</tt>, <tt>[N, N]</tt>, and (in theory)
88
- # <tt>[N, 1]</tt>.
89
- def cardinality_class
90
- [source_cardinality_class, destination_cardinality_class]
91
- end
92
-
93
- protected
94
-
95
- # The cardinality class of the source (left side). Either +1+ or +Infinity+.
96
- def source_cardinality_class
97
- source_range.last == 1 ? 1 : N
98
- end
99
-
100
- # The cardinality class of the destination (right side). Either +1+ or +Infinity+.
101
- def destination_cardinality_class
102
- destination_range.last == 1 ? 1 : N
103
- end
104
-
105
- private
106
-
107
- def compose_range(r)
108
- return r..r if r.kind_of?(Integer) && r > 0
109
- return (r.begin)..(r.end - 1) if r.exclude_end?
110
- r
111
- end
112
-
113
- def compare_with(other, &block)
114
- yield(self) <=> yield(other)
115
- end
116
- end
117
- end
118
- end