class GeojsonDiff::PropertyDiff

Public Class Methods

new(before,after) click to toggle source

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

added() click to toggle source

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
added?(key) click to toggle source

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
changed() click to toggle source

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
changed?(key) click to toggle source

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
diff() click to toggle source

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
Also aliased as: properties
inspect() click to toggle source
# 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
properties()
Alias for: diff
removed() click to toggle source

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
removed?(key) click to toggle source

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
to_json() click to toggle source

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
to_s() click to toggle source
# File lib/geojson-diff/property-diff.rb, line 64
def to_s
  diff.to_s
end

Private Instance Methods

diffed_property(key) click to toggle source

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
diffed_value(key) click to toggle source

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