Class: RevisionSan::Diff

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

Defined Under Namespace

Modules: SimpleHTMLFormatter

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(from, to) ⇒ Diff

Returns a new instance of Diff.



19
20
21
# File 'lib/revision_san/diff.rb', line 19

def initialize(from, to)
  @from, @to = from, to
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/revision_san/diff.rb', line 32

def method_missing(method, *args, &block)
  if @from.respond_to?(method) || @from.class.column_names.include?(method.to_s)
    define_and_call_singleton_method(method, &block)
  else
    super
  end
end

Instance Attribute Details

#fromObject (readonly)

Returns the value of attribute from.



17
18
19
# File 'lib/revision_san/diff.rb', line 17

def from
  @from
end

#toObject (readonly)

Returns the value of attribute to.



17
18
19
# File 'lib/revision_san/diff.rb', line 17

def to
  @to
end

Instance Method Details

#define_and_call_singleton_method(method, &block) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/revision_san/diff.rb', line 40

def define_and_call_singleton_method(method, &block)
  instance_eval %{
    def #{method}(&block)
      diff_for_column('#{method}', &block)
    end
    
    #{method}(&block)
  }
end

#diff_for_column(column) ⇒ Object



23
24
25
26
27
28
29
30
# File 'lib/revision_san/diff.rb', line 23

def diff_for_column(column)
  from_lines, to_lines = [@from.send(column), @to.send(column)].map do |res|
    text = res.blank? ? '' : res.to_s
    text = yield(text) if block_given?
    text.scan(/\n+|.+/)
  end
  SimpleHTMLFormatter.diff(from_lines, to_lines)
end