Class: RGen::ModelComparatorBase
- Inherits:
-
Object
- Object
- RGen::ModelComparatorBase
- Defined in:
- lib/rgen/model_comparator_base.rb
Defined Under Namespace
Classes: CompareSpec
Constant Summary collapse
- INDENT =
" "
Class Attribute Summary collapse
-
.compareSpecs ⇒ Object
readonly
Returns the value of attribute compareSpecs.
Class Method Summary collapse
Instance Method Summary collapse
-
#compare(as, bs, recursive = true) ⇒ Object
compares two sets of elements.
- #compareElements(a, b) ⇒ Object
- #compareSpec(element) ⇒ Object
- #compareValues(name, val1, val2) ⇒ Object
- #elementDisplayName(e) ⇒ Object
- #elementIdentifier(element) ⇒ Object
- #sortElementPairs(pairs) ⇒ Object
- #sortElements(elements) ⇒ Object
- #useElement?(element) ⇒ Boolean
Class Attribute Details
.compareSpecs ⇒ Object (readonly)
Returns the value of attribute compareSpecs.
11 12 13 |
# File 'lib/rgen/model_comparator_base.rb', line 11 def compareSpecs @compareSpecs end |
Class Method Details
.compare_spec(clazz, hash) ⇒ Object
13 14 15 16 17 18 19 20 21 |
# File 'lib/rgen/model_comparator_base.rb', line 13 def compare_spec(clazz, hash) @compareSpecs ||= {} raise "Compare spec already defined for #{clazz}" if @compareSpecs[clazz] spec = CompareSpec.new hash.each_pair do |k,v| spec.send("#{k}=",v) end @compareSpecs[clazz] = spec end |
Instance Method Details
#compare(as, bs, recursive = true) ⇒ Object
compares two sets of elements
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rgen/model_comparator_base.rb', line 25 def compare(as, bs, recursive=true) result = [] aById = as.select{|e| useElement?(e)}.inject({}){|r, e| r[elementIdentifier(e)] = e; r} bById = bs.select{|e| useElement?(e)}.inject({}){|r, e| r[elementIdentifier(e)] = e; r} onlyA = sortElements((aById.keys - bById.keys).collect{|id| aById[id]}) onlyB = sortElements((bById.keys - aById.keys).collect{|id| bById[id]}) aAndB = sortElementPairs((aById.keys & bById.keys).collect{|id| [aById[id], bById[id]]}) onlyA.each do |e| result << "- #{elementDisplayName(e)}" end onlyB.each do |e| result << "+ #{elementDisplayName(e)}" end if recursive aAndB.each do |ab| a, b = *ab r = compareElements(a, b) if r.size > 0 result << "#{elementDisplayName(a)}" result += r.collect{|l| INDENT+l} end end end result end |
#compareElements(a, b) ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
# File 'lib/rgen/model_comparator_base.rb', line 76 def compareElements(a, b) result = [] if a.class != b.class result << "Class: #{a.class} -> #{b.class}" else a.class.ecore.eAllStructuralFeatures.reject{|f| f.derived || compareSpec(a).andand.ignore_features.andand.include?(f.name.to_sym)}.each do |f| va, vb = a.getGeneric(f.name), b.getGeneric(f.name) if f.is_a?(RGen::ECore::EAttribute) r = compareValues(f.name, va, vb) result << r if r else va, vb = [va].compact, [vb].compact unless f.many r = compare(va, vb, f.containment || compareSpec(a).andand.recurse.andand.include?(f.name.to_sym)) if r.size > 0 result << "[#{f.name}]" result += r.collect{|l| INDENT+l} end end end end result end |
#compareSpec(element) ⇒ Object
127 128 129 130 131 132 133 |
# File 'lib/rgen/model_comparator_base.rb', line 127 def compareSpec(element) @compareSpec ||= {} return @compareSpec[element.class] if @compareSpec[element.class] return nil unless self.class.compareSpecs key = self.class.compareSpecs.keys.find{|k| element.is_a?(k)} @compareSpec[element.class] = self.class.compareSpecs[key] end |
#compareValues(name, val1, val2) ⇒ Object
99 100 101 102 103 |
# File 'lib/rgen/model_comparator_base.rb', line 99 def compareValues(name, val1, val2) result = nil result = "[#{name}] #{val1} -> #{val2}" if val1 != val2 result end |
#elementDisplayName(e) ⇒ Object
68 69 70 71 72 73 74 |
# File 'lib/rgen/model_comparator_base.rb', line 68 def elementDisplayName(e) if compareSpec(e) && compareSpec(e).display_name compareSpec(e).display_name.call(e) else elementIdentifier(e) end end |
#elementIdentifier(element) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/rgen/model_comparator_base.rb', line 105 def elementIdentifier(element) cs = compareSpec(element) if cs && cs.identifier if cs.identifier.is_a?(Proc) cs.identifier.call(element) else cs.identifier end else if element.respond_to?(:name) element.name else element.object_id end end end |
#sortElementPairs(pairs) ⇒ Object
51 52 53 54 55 56 57 58 |
# File 'lib/rgen/model_comparator_base.rb', line 51 def sortElementPairs(pairs) pairs.sort do |x,y| a, b = x[0], y[0] r = a.class.name <=> b.class.name r = compareSpec(a).sort.call(a,b) if r == 0 && compareSpec(a) && compareSpec(a).sort r end end |
#sortElements(elements) ⇒ Object
60 61 62 63 64 65 66 |
# File 'lib/rgen/model_comparator_base.rb', line 60 def sortElements(elements) elements.sort do |a,b| r = a.class.name <=> b.class.name r = compareSpec(a).sort.call(a,b) if r == 0 && compareSpec(a) && compareSpec(a).sort r end end |
#useElement?(element) ⇒ Boolean
122 123 124 125 |
# File 'lib/rgen/model_comparator_base.rb', line 122 def useElement?(element) cs = compareSpec(element) !(cs && cs.filter) || cs.filter.call(element) end |