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> }



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

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 not @new_version
    @complete = ''
    @summary  = ''
  else
    lcs_diff
  end
end

Instance Attribute Details

#completeObject (readonly)

Returns the value of attribute complete.



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

def complete
  @complete
end

#summaryObject (readonly)

Returns the value of attribute summary.



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

def summary
  @summary
end

Instance Method Details

#green?Boolean

Returns:

  • (Boolean)


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

def green?
  @adds_cnt > 0 
end

#lcs_diffObject



89
90
91
92
93
94
95
# File 'lib/card/diff.rb', line 89

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



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/card/diff.rb', line 67

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

#red?Boolean

Returns:

  • (Boolean)


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

def red?
  @dels_cnt > 0 
end