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, context = {}) ⇒ Constant

Returns a new instance of Constant.



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

def initialize name, value, context = {}
  set_name name, context
  @value = value
  @context = context

  if @context
    if @context[:class].is_a? Brakeman::Controller
      @context[:class] = @context[:class].name
    end

    @file = @context[:file]
  end
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



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

def context
  @context
end

#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



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

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

#match?(name) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/brakeman/tracker/constants.rb', line 32

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



27
28
29
30
# File 'lib/brakeman/tracker/constants.rb', line 27

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