Parent

Class/Module Index [+]

Quicksearch

RGen::Util::ModelComparatorBase

Constants

CompareSpec
INDENT

Attributes

compareSpecs[R]

Public Class Methods

compare_spec(clazz, hash) click to toggle source
# File lib/rgen/util/model_comparator_base.rb, line 15
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

Public Instance Methods

compare(as, bs, recursive=true) click to toggle source

compares two sets of elements

# File lib/rgen/util/model_comparator_base.rb, line 27
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) click to toggle source
# File lib/rgen/util/model_comparator_base.rb, line 78
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) click to toggle source
# File lib/rgen/util/model_comparator_base.rb, line 129
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) click to toggle source
# File lib/rgen/util/model_comparator_base.rb, line 101
def compareValues(name, val1, val2)
        result = nil
        result = "[#{name}] #{val1} -> #{val2}" if val1 != val2
        result
end
elementDisplayName(e) click to toggle source
# File lib/rgen/util/model_comparator_base.rb, line 70
def elementDisplayName(e)
        if compareSpec(e) && compareSpec(e).display_name
                compareSpec(e).display_name.call(e)
        else
                elementIdentifier(e)
        end
end
elementIdentifier(element) click to toggle source
# File lib/rgen/util/model_comparator_base.rb, line 107
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) click to toggle source
# File lib/rgen/util/model_comparator_base.rb, line 53
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) click to toggle source
# File lib/rgen/util/model_comparator_base.rb, line 62
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) click to toggle source
# File lib/rgen/util/model_comparator_base.rb, line 124
def useElement?(element)
        cs = compareSpec(element)
        !(cs && cs.filter) || cs.filter.call(element)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.