Class: Cloc::Database

Inherits:
Object
  • Object
show all
Defined in:
lib/cloc/database.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(version) ⇒ Database

Returns a new instance of Database.



20
21
22
23
24
# File 'lib/cloc/database.rb', line 20

def initialize(version)
  @version = version
  @table = {}
  load if File.exist? path
end

Class Method Details

.default_versionObject

Pick a default version.

We give preference to RUBY_VERSION.



16
17
18
# File 'lib/cloc/database.rb', line 16

def self.default_version
  versions.include?(RUBY_VERSION) ? RUBY_VERSION : (versions.first || RUBY_VERSION)
end

.versionsObject

Find all existing versions.



9
10
11
# File 'lib/cloc/database.rb', line 9

def self.versions
  @versions ||= Dir.entries(HOME).grep(/^.cloc-(.*)/) { $1 }.sort
end

Instance Method Details

#dataObject



34
35
36
# File 'lib/cloc/database.rb', line 34

def data
  @table
end

#loadObject



58
59
60
# File 'lib/cloc/database.rb', line 58

def load
  @table = File.open(path) { |f| Marshal.load(f) }
end

#lookup(object_name, method_name) ⇒ Object

Returns the source-code location where a method is defined.

Returned value is either [ path_to_file, line_number ], or nil if the method isn’t known.



42
43
44
45
46
47
48
# File 'lib/cloc/database.rb', line 42

def lookup(object_name, method_name)
  if @table[object_name] and (loc = @table[object_name][method_name])
    # Since the pathname is stored as a symbol, we convert
    # it back to a string.
    return [loc[0].to_s, loc[1]]
  end
end

#merge(hash) ⇒ Object



28
29
30
31
32
# File 'lib/cloc/database.rb', line 28

def merge(hash)
  @table.merge!(hash) do |key, oldval, newval|
    oldval.merge(newval)
  end
end

#pathObject



50
51
52
# File 'lib/cloc/database.rb', line 50

def path
  File.join(HOME, ".cloc-#{@version}")
end

#saveObject



54
55
56
# File 'lib/cloc/database.rb', line 54

def save
  File.open(path, 'w') { |f| Marshal.dump(@table, f) }
end

#versionObject



26
# File 'lib/cloc/database.rb', line 26

def version; @version; end