Class: Xing::SpecDoc::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/xing/specdoc/document.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, contents) ⇒ Document

Returns a new instance of Document.



6
7
8
9
# File 'lib/xing/specdoc/document.rb', line 6

def initialize(path, contents)
  @path = path
  @contents = contents
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



10
11
12
# File 'lib/xing/specdoc/document.rb', line 10

def contents
  @contents
end

#pathObject (readonly)

Returns the value of attribute path.



10
11
12
# File 'lib/xing/specdoc/document.rb', line 10

def path
  @path
end

Instance Method Details

#diff(one, other) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/xing/specdoc/document.rb', line 34

def diff(one, other)
  diff = HashDiff.diff(one, other)


  by_change = diff.group_by {|change| change[0]}

  adds = by_change.delete("+") || []
  subs = by_change.delete("-") || []
  mods = by_change.values.inject do |all, a_kind|
    all + a_kind
  end || []

  arr_adds, other_adds = adds.partition do |add|
    add[1] =~ /\[\d+\]$/
  end

  mods += (other_adds || [])

  arr_adds.each do |add|
    sub = subs.find{|sub| sub[1] == add[1]}
    if sub.nil?
      mods << add
    else
      subs.delete(sub)
      new_diff = diff(add[2], sub[2])
      mods += new_diff.map do |change|
        [change[0], [add[1], change[1]].join(".")] + change[2..-1]
      end
    end
  end

  mods += subs

  mods
end

#difference_from(other) ⇒ Object



30
31
32
# File 'lib/xing/specdoc/document.rb', line 30

def difference_from(other)
  diff(parsed_body, other)
end

#different_from?(body) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/xing/specdoc/document.rb', line 76

def different_from?(body)
  difference_from(body).any? do |change|
    significant_diff_item?(change)
  end
end

#parsed_bodyObject



12
13
14
15
16
17
18
19
# File 'lib/xing/specdoc/document.rb', line 12

def parsed_body
  @parsed_body ||=
    if contents.empty?
      {}
    else
      JSON.parse(contents)
    end
end

#pretty_bodyObject



21
22
23
24
25
26
27
28
# File 'lib/xing/specdoc/document.rb', line 21

def pretty_body
  @pretty_body ||=
    if contents.empty?
      ""
    else
      JSON.pretty_generate(parsed_body)+"\n"
    end
end

#significant_diff_item?(change) ⇒ Boolean

Returns:

  • (Boolean)


70
71
72
73
74
# File 'lib/xing/specdoc/document.rb', line 70

def significant_diff_item?(change)
  return true if change[0] != "~"
  return true if change[2].class != change[3].class
  return false
end