Module: LintFu::Mixins::SexpInstanceMethods

Defined in:
lib/lint_fu/mixins/sexp_instance_methods.rb

Instance Method Summary collapse

Instance Method Details

#constant?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/lint_fu/mixins/sexp_instance_methods.rb', line 55

def constant?
  typ = self[0]

  case typ
    when :true, :false, :lit, :str
      return true
    when :array, :arglist
      self[1..-1].each { |sexp| return false unless sexp.constant? }
      return true
    when :hash
      result = {}
      key, value = nil, nil
      flipflop = false
      self[1..-1].each do |token|
        if flipflop
          value = token
          return false unless key.constant? && value.constant?
        else
          key = token
        end
        flipflop = !flipflop
      end
      return true
    else
      return false
  end
end

#deep_cloneObject



4
5
6
# File 'lib/lint_fu/mixins/sexp_instance_methods.rb', line 4

def deep_clone
  Marshal.load(Marshal.dump(self))
end

#find_all_recursively(results = nil, &test) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/lint_fu/mixins/sexp_instance_methods.rb', line 94

def find_all_recursively(results=nil, &test)
  results ||= []
  results << self if test.call(self)

  self.each do |child|
    child.find_all_recursively(results, &test) if (Sexp === child)
  end

  return nil if results.empty?
  return results
end

#find_recursively(&test) ⇒ Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/lint_fu/mixins/sexp_instance_methods.rb', line 83

def find_recursively(&test)
  return self if test.call(self)

  self.each do |child|
    found = child.find_recursively(&test) if (Sexp === child)
    return found if found
  end

  return nil
end

#fingerprintObject

Return a version of this Sexp that preserves the structure of the original, but with any specific names, quantities or other values replaced with nil. The fingerprint of a given chunk of code will tend to remain the same over time, even if variable names or other inconsequential details are changed. TODO actually implement this method



13
14
15
# File 'lib/lint_fu/mixins/sexp_instance_methods.rb', line 13

def fingerprint
  self
end

#to_ruby(options = {}) ⇒ Object

Translate a sexp containing a Ruby data literal (string, int, array, hash, etc) into the equivalent Ruby object.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/lint_fu/mixins/sexp_instance_methods.rb', line 23

def to_ruby(options={})
  typ = self[0]

  case typ
    when :true
      return true
    when :false
      return false
    when :lit, :str
      return self[1]
    when :array, :arglist
      return self[1..-1].collect { |x| x.to_ruby(options) }
    when :hash
      result = {}
      key, value = nil, nil
      flipflop = false
      self[1..-1].each do |token|
        if flipflop
          value = token
          result[key.to_ruby(options)] = value.to_ruby(options)
        else
          key = token
        end
        flipflop = !flipflop
      end
      return result
    else
      return options[:partial] if options.has_key?(:partial)
      raise StandardError.new("Cannot convert Sexp to Ruby object: " + self.to_s)
  end
end

#to_ruby_stringObject

Generate a human-readable description for this sexp that is similar to source code.



18
19
20
# File 'lib/lint_fu/mixins/sexp_instance_methods.rb', line 18

def to_ruby_string
  Ruby2Ruby.new.process(self.deep_clone)
end