Class: Brakeman::Constant

Inherits:
Object
  • Object
show all
Defined in:
lib/brakeman/tracker/constants.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, value = nil, context = nil) ⇒ Constant

Returns a new instance of Constant.



7
8
9
10
11
12
13
14
15
# File 'lib/brakeman/tracker/constants.rb', line 7

def initialize name, value = nil, context = nil 
  set_name name, context
  @value = value
  @context = context

  if @context
    @file = @context[:file]
  end
end

Instance Attribute Details

#fileObject (readonly)

Returns the value of attribute file.



5
6
7
# File 'lib/brakeman/tracker/constants.rb', line 5

def file
  @file
end

#nameObject (readonly)

Returns the value of attribute name.



5
6
7
# File 'lib/brakeman/tracker/constants.rb', line 5

def name
  @name
end

#name_arrayObject (readonly)

Returns the value of attribute name_array.



5
6
7
# File 'lib/brakeman/tracker/constants.rb', line 5

def name_array
  @name_array
end

#valueObject (readonly)

Returns the value of attribute value.



5
6
7
# File 'lib/brakeman/tracker/constants.rb', line 5

def value
  @value
end

Instance Method Details

#lineObject



17
18
19
20
21
# File 'lib/brakeman/tracker/constants.rb', line 17

def line
  if @value.is_a? Sexp
    @value.line
  end
end

#match?(name) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/brakeman/tracker/constants.rb', line 28

def match? name
  if name == @name
    return true
  elsif name.is_a? Sexp and name.node_type == :const and name.value == @name
    return true
  elsif name.is_a? Symbol and name.value == @name
    return true
  elsif name.class == Array
    name == @name_array or
      @name_array.reverse.zip(name.reverse).reduce(true) { |m, a| a[1] ? a[0] == a[1] && m : m }
  else
    false
  end
end

#set_name(name, context) ⇒ Object



23
24
25
26
# File 'lib/brakeman/tracker/constants.rb', line 23

def set_name name, context
  @name = name
  @name_array = Constants.constant_as_array(name)
end