Class: GeojsonDiff::PropertyDiff

Inherits:
Object
  • Object
show all
Defined in:
lib/geojson-diff/property-diff.rb

Instance Method Summary collapse

Constructor Details

#initialize(before, after) ⇒ PropertyDiff

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



10
11
12
13
14
15
# File 'lib/geojson-diff/property-diff.rb', line 10

def initialize(before,after)
  @before = before
  @after = after
  @meta = { :added => [], :removed => [], :changed => [] }
  diff
end

Instance Method Details

#addedObject

Returns an array of all keys added to the resulting geometry



27
28
29
# File 'lib/geojson-diff/property-diff.rb', line 27

def added
  @meta[:added]
end

#added?(key) ⇒ Boolean

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

Returns:

  • (Boolean)


22
23
24
# File 'lib/geojson-diff/property-diff.rb', line 22

def added?(key)
  !@before.key?(key) && @after.key?(key)
end

#changedObject

Returns an array of all keys modified in the resulting geometry



55
56
57
# File 'lib/geojson-diff/property-diff.rb', line 55

def changed
  @meta[:changed]
end

#changed?(key) ⇒ Boolean

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

Returns:

  • (Boolean)


50
51
52
# File 'lib/geojson-diff/property-diff.rb', line 50

def changed?(key)
  !added?(key) && !removed?(key) && @before[key] != @after[key]
end

#diffObject Also known as: properties

Returns a hash of the diffed properties, including metadata



73
74
75
76
77
78
79
# 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

#inspectObject



68
69
70
# 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

#removedObject

Returns an array of all keys removed from the resulting geometry



41
42
43
# File 'lib/geojson-diff/property-diff.rb', line 41

def removed
  @meta[:removed]
end

#removed?(key) ⇒ Boolean

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

Returns:

  • (Boolean)


36
37
38
# File 'lib/geojson-diff/property-diff.rb', line 36

def removed?(key)
  @before.key?(key) && !@after.key?(key)
end

#to_jsonObject

Returns the JSON representation of the diffed properties, including metadata



60
61
62
# File 'lib/geojson-diff/property-diff.rb', line 60

def to_json
  diff.to_json
end

#to_sObject



64
65
66
# File 'lib/geojson-diff/property-diff.rb', line 64

def to_s
  diff.to_s
end