Class: Card::Diff::DiffBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/card/diff.rb

Defined Under Namespace

Classes: LCS

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(old_version, new_version, opts = {}) ⇒ DiffBuilder

diff options :format => :html|:text|:pointer|:raw

:html    = maintain html structure, but compare only content
:text    = remove all html tags; compare plain text
:pointer = remove all double square brackets
:raw     = escape html tags and compare everything

summary: {length: <number> , joint: <string> }



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/card/diff.rb', line 42

def initialize old_version, new_version, opts={}
  @new_version = new_version
  @old_version = old_version
  @lcs_opts = lcs_opts_for_format opts[:format]
  @lcs_opts[:summary] = opts[:summary]
  @dels_cnt = 0
  @adds_cnt = 0

  if !@new_version
    @complete = ''
    @summary  = ''
  else
    lcs_diff
  end
end

Instance Attribute Details

#completeObject (readonly)

Returns the value of attribute complete.



31
32
33
# File 'lib/card/diff.rb', line 31

def complete
  @complete
end

#summaryObject (readonly)

Returns the value of attribute summary.



31
32
33
# File 'lib/card/diff.rb', line 31

def summary
  @summary
end

Instance Method Details

#green?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'lib/card/diff.rb', line 62

def green?
  @adds_cnt > 0
end

#lcs_diffObject



82
83
84
85
86
87
88
# File 'lib/card/diff.rb', line 82

def lcs_diff
  @lcs = LCS.new(@old_version, @new_version, @lcs_opts)
  @summary  = @lcs.summary
  @complete = @lcs.complete
  @dels_cnt = @lcs.dels_cnt
  @adds_cnt = @lcs.adds_cnt
end

#lcs_opts_for_format(format) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/card/diff.rb', line 66

def lcs_opts_for_format format
  opts = {}
  case format
  when :html
    opts[:exclude] = /^</
  when :text
    opts[:reject] =  /^</
    opts[:postprocess] = proc { |word| word.gsub("\n", '<br>') }
  when :pointer
    opts[:preprocess] = proc { |word| word.gsub('[[', '').gsub(']]', '') }
  else # :raw
    opts[:preprocess] = proc { |word| CGI.escapeHTML(word) }
  end
  opts
end

#red?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/card/diff.rb', line 58

def red?
  @dels_cnt > 0
end