Class: CaseCheck::ColdfusionSource

Inherits:
Object
  • Object
show all
Defined in:
lib/case_check/coldfusion_source.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename, content = nil) ⇒ ColdfusionSource

Returns a new instance of ColdfusionSource.



13
14
15
16
# File 'lib/case_check/coldfusion_source.rb', line 13

def initialize(filename, content = nil)
  @filename = filename
  self.content = content
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



6
7
8
# File 'lib/case_check/coldfusion_source.rb', line 6

def content
  @content
end

#filenameObject

Returns the value of attribute filename.



6
7
8
# File 'lib/case_check/coldfusion_source.rb', line 6

def filename
  @filename
end

#internal_referencesObject

Returns the value of attribute internal_references.



6
7
8
# File 'lib/case_check/coldfusion_source.rb', line 6

def internal_references
  @internal_references
end

Class Method Details

.create(filename) ⇒ Object



8
9
10
11
# File 'lib/case_check/coldfusion_source.rb', line 8

def self.create(filename)
  f = File.expand_path(filename)
  new(f, File.read(f))
end

Instance Method Details

#analyzeObject



18
19
20
21
22
# File 'lib/case_check/coldfusion_source.rb', line 18

def analyze
  [CustomTag, Cfmodule, Cfinclude, Cfc].each do |reftype|
    internal_references.concat reftype.search(self)
  end
end

#inexact_internal_referencesObject



28
29
30
# File 'lib/case_check/coldfusion_source.rb', line 28

def inexact_internal_references
  internal_references.reject { |ir| ir.resolution == :exact }
end

#line_of(i) ⇒ Object

returns the line number (1-based) on which the given character index lies



33
34
35
36
37
38
39
40
41
42
# File 'lib/case_check/coldfusion_source.rb', line 33

def line_of(i)
  return nil if i >= content.size
  char_ct = 0
  l = 0
  while char_ct <= i
    char_ct += lines[l].size
    l += 1
  end
  l
end

#linesObject



49
50
51
52
53
54
# File 'lib/case_check/coldfusion_source.rb', line 49

def lines
  return @lines if @lines
  @lines = []
  content.split(/(\r\n|\r|\n)/).each_slice(2) { |line_and_br| @lines << line_and_br.join('') }
  @lines
end

#scan(re, &block) ⇒ Object

Scans the content for the given RE, yielding the MatchData for each match and the line number on which it occurred



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/case_check/coldfusion_source.rb', line 58

def scan(re, &block)
  results = []
  char_offset = 0
  re.scan(content) do |md|
    results << (yield [md, line_of(char_offset + md.begin(0))])
    
    char_offset += md[0].size + md.pre_match.size
    remaining = md.post_match
  end
  results
end

#scan_for_tag(tag, &block) ⇒ Object

Scans the content for opening and/or self-closing tags with the given name. Yields the full text of the tag, a parsed representation of the attributes, and the line number back to the provided block.



73
74
75
76
77
78
79
80
81
82
83
# File 'lib/case_check/coldfusion_source.rb', line 73

def scan_for_tag(tag, &block)
  scan(/<#{tag}(.*?)>/mi) do |md, l|
    attributes = %w(' ").collect do |q|
      /(\w+)\s*=\s*#{q}([^#{q}]*?)#{q}/.scan(md[1].gsub(%r(/$), ''))
    end.flatten.inject({}) do |attrs, amd|
      attrs[normalize_attribute_key(amd[1])] = amd[2]
      attrs
    end
    yield md[0], attributes, l
  end
end