class GeojsonDiff::PropertyDiff
Public Class Methods
Creates a new PropertyDiff instance
before - the property element of the starting geometry (as parsed JSON) after - the property element of the resulting geometry (as parsed JSON)
returns the PropertyDiff instance
# File lib/geojson-diff/property-diff.rb, line 10 def initialize(before,after) @before = before @after = after @meta = { :added => [], :removed => [], :changed => [] } diff end
Public Instance Methods
Returns an array of all keys added to the resulting geometry
# File lib/geojson-diff/property-diff.rb, line 27 def added @meta[:added] end
Checks if the given key has been added to the resulting geometry
key - string the JSON property key to check
Returns bool true if added, otherwise false
# File lib/geojson-diff/property-diff.rb, line 22 def added?(key) !@before.key?(key) && @after.key?(key) end
Returns an array of all keys modified in the resulting geometry
# File lib/geojson-diff/property-diff.rb, line 55 def changed @meta[:changed] end
Checks if the given key has been modified in the resulting geometry
key - string the JSON property key to check
Returns bool true if modified, otherwise false
# File lib/geojson-diff/property-diff.rb, line 50 def changed?(key) !added?(key) && !removed?(key) && @before[key] != @after[key] end
Returns a hash of the diffed properties, including metadata
# File lib/geojson-diff/property-diff.rb, line 73 def diff @diff ||= begin properties = {} @before.merge(@after).each { |key,value| properties.merge! diffed_property(key) } properties.merge({ GeojsonDiff::META_KEY => @meta }) end end
# File lib/geojson-diff/property-diff.rb, line 68 def inspect "#<GeojsonDiff::PropertyDiff added=#{added.to_s} removed=#{removed.to_s} changed=#{changed.to_s}>" end
Returns an array of all keys removed from the resulting geometry
# File lib/geojson-diff/property-diff.rb, line 41 def removed @meta[:removed] end
Checks if the given key has been removed from the resulting geometry
key - string the JSON property key to check
Returns bool true if removed, otherwise false
# File lib/geojson-diff/property-diff.rb, line 36 def removed?(key) @before.key?(key) && !@after.key?(key) end
Returns the JSON representation of the diffed properties, including metadata
# File lib/geojson-diff/property-diff.rb, line 60 def to_json diff.to_json end
# File lib/geojson-diff/property-diff.rb, line 64 def to_s diff.to_s end
Private Instance Methods
Diffs an individual key/value pair Also propegates @meta arrays
key - the property key to diff
Returns the resulting key/value pair, either before (removed), after (added), or diffed (changed)
# File lib/geojson-diff/property-diff.rb, line 90 def diffed_property(key) if added? key @meta[:added].push key { key => @after[key] } elsif removed? key @meta[:removed].push key { key => @before[key] } elsif changed? key @meta[:changed].push key { key => diffed_value(key) } else # unchanged { key => @after[key] } end end
Diffs an indivual changed value
key - the property key to diff
Returns (string) the Diffy representation of the changed property
# File lib/geojson-diff/property-diff.rb, line 110 def diffed_value(key) Diffy::Diff.new(@before[key], @after[key]).to_s(:html) end