Class: YARD::RegistryStore

Inherits:
Object
  • Object
show all
Defined in:
lib/yard/registry_store.rb

Overview

The data store for the Registry.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRegistryStore

Returns a new instance of RegistryStore.



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/yard/registry_store.rb', line 13

def initialize
  @file = nil
  @checksums = {}
  @store = {}
  @proxy_types = {}
  @object_types = {:root => [:root]}
  @notfound = {}
  @loaded_objects = 0
  @available_objects = 0
  @store[:root] = CodeObjects::RootObject.allocate
  @store[:root].send(:initialize, nil, :root)
end

Instance Attribute Details

#checksumsObject (readonly)

Returns the value of attribute checksums.



11
12
13
# File 'lib/yard/registry_store.rb', line 11

def checksums
  @checksums
end

#fileObject (readonly)

Returns the value of attribute file.



11
12
13
# File 'lib/yard/registry_store.rb', line 11

def file
  @file
end

#proxy_typesObject (readonly)

Deprecated.

The registry no longer tracks proxy types



10
11
12
# File 'lib/yard/registry_store.rb', line 10

def proxy_types
  @proxy_types
end

Instance Method Details

#checksums_pathObject (protected)



217
218
219
# File 'lib/yard/registry_store.rb', line 217

def checksums_path
  @serializer.checksums_path
end

#delete(key) ⇒ void

This method returns an undefined value.

Deletes an object at a given path

Parameters:

  • key (#to_sym)

    the key to delete



72
# File 'lib/yard/registry_store.rb', line 72

def delete(key) @store.delete(key.to_sym) end

#destroy(force = false) ⇒ Boolean

Deletes the .yardoc database on disk

Parameters:

  • force (Boolean) (defaults to: false)

    if force is not set to true, the file/directory will only be removed if it ends with .yardoc. This helps with cases where the directory might have been named incorrectly.

Returns:

  • (Boolean)

    true if the .yardoc database was deleted, false otherwise.



192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/yard/registry_store.rb', line 192

def destroy(force = false)
  if (!force && file =~ /\.yardoc$/) || force
    if File.file?(@file)
      # Handle silent upgrade of old .yardoc format
      File.unlink(@file)
    elsif File.directory?(@file)
      FileUtils.rm_rf(@file)
    end
    true
  else
    false
  end
end

#get(key) ⇒ CodeObjects::Base? Also known as: []

Gets a CodeObjects::Base from the store

Parameters:

  • key (String, Symbol)

    the path name of the object to look for. If it is empty or :root, returns the #root object.

Returns:



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/yard/registry_store.rb', line 31

def get(key)
  key = :root if key == ''
  key = key.to_sym
  return @store[key] if @store[key]
  return if @loaded_objects >= @available_objects

  # check disk
  return if @notfound[key]
  if obj = @serializer.deserialize(key)
    @loaded_objects += 1
    put(key, obj)
  else
    @notfound[key] = true
    nil
  end
end

#keys(reload = false) ⇒ Array<Symbol>

Gets all path names from the store. Loads the entire database if reload is true

Parameters:

  • reload (Boolean) (defaults to: false)

    if false, does not load the entire database before a lookup.

Returns:

  • (Array<Symbol>)

    the path names of all the code objects



80
# File 'lib/yard/registry_store.rb', line 80

def keys(reload = false) load_all if reload; @store.keys end

#load(file = nil) ⇒ Boolean

Returns whether the database was loaded.

Parameters:

  • file (String, nil) (defaults to: nil)

    the name of the yardoc db to load

Returns:

  • (Boolean)

    whether the database was loaded



113
114
115
116
117
118
119
120
121
# File 'lib/yard/registry_store.rb', line 113

def load(file = nil)
  @file = file
  @store = {}
  @proxy_types = {}
  @object_types = {}
  @notfound = {}
  @serializer = Serializers::YardocSerializer.new(@file)
  load_yardoc
end

#load!(file = nil) ⇒ Boolean

Loads the .yardoc file and loads all cached objects into memory automatically.

Parameters:

  • file (String, nil) (defaults to: nil)

    the name of the yardoc db to load

Returns:

  • (Boolean)

    whether the database was loaded

See Also:

Since:

  • 0.5.1



130
131
132
133
134
135
136
137
# File 'lib/yard/registry_store.rb', line 130

def load!(file = nil)
  if load(file)
    load_all
    true
  else
    false
  end
end

#load_allvoid

This method returns an undefined value.

Loads all cached objects into memory



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/yard/registry_store.rb', line 141

def load_all
  return unless @file
  return if @loaded_objects >= @available_objects
  log.debug "Loading entire database: #{@file} ..."
  objects = []

  all_disk_objects.sort_by {|x| x.size }.each do |path|
    if obj = @serializer.deserialize(path, true)
      objects << obj
    end
  end
  objects.each do |obj|
    put(obj.path, obj)
  end
  @loaded_objects += objects.size
  log.debug "Loaded database (file='#{@file}' count=#{objects.size} total=#{@available_objects})"
end

#load_yardocObject (protected)



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# File 'lib/yard/registry_store.rb', line 225

def load_yardoc
  return false unless @file
  if File.directory?(@file) # new format
    @loaded_objects = 0
    @available_objects = all_disk_objects.size
    load_proxy_types
    load_checksums
    load_root
    load_object_types
    true
  elsif File.file?(@file) # old format
    load_yardoc_old
    true
  else
    false
  end
end

#object_types_pathObject (protected)



221
222
223
# File 'lib/yard/registry_store.rb', line 221

def object_types_path
  @serializer.object_types_path
end

#objects_pathObject (protected)



208
209
210
# File 'lib/yard/registry_store.rb', line 208

def objects_path
  @serializer.objects_path
end

#paths_for_type(type, reload = false) ⇒ Array<String>

Returns a list of object paths with a given CodeObjects::Base#type.

Parameters:

  • type (Symbol)

    the type to look for

Returns:

Since:

  • 0.8.0



94
95
96
97
# File 'lib/yard/registry_store.rb', line 94

def paths_for_type(type, reload = false)
  load_all if reload
  @object_types[type] || []
end

#proxy_types_pathObject (protected)

Deprecated.

The registry no longer tracks proxy types



213
214
215
# File 'lib/yard/registry_store.rb', line 213

def proxy_types_path
  @serializer.proxy_types_path
end

#put(key, value) ⇒ CodeObjects::Base Also known as: []=

Associates an object with a path

Parameters:

  • key (String, Symbol)

    the path name (:root or ” for root object)

  • value (CodeObjects::Base)

    the object to store

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/yard/registry_store.rb', line 52

def put(key, value)
  if key == ''
    @object_types[:root] = [:root]
    @store[:root] = value
  else
    @notfound.delete(key.to_sym)
    (@object_types[value.type] ||= []) << key.to_s
    if @store[key.to_sym]
      @object_types[@store[key.to_sym].type].delete(key.to_s)
    end
    @store[key.to_sym] = value
  end
end

#rootCodeObjects::RootObject

Returns the root object.

Returns:



109
# File 'lib/yard/registry_store.rb', line 109

def root; @store[:root] end

#save(merge = true, file = nil) ⇒ Boolean

Saves the database to disk

Parameters:

  • merge (Boolean) (defaults to: true)

    if true, merges the data in memory with the data on disk, otherwise the data on disk is deleted.

  • file (String, nil) (defaults to: nil)

    if supplied, the name of the file to save to

Returns:

  • (Boolean)

    whether the database was saved



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/yard/registry_store.rb', line 164

def save(merge = true, file = nil)
  if file && file != @file
    @file = file
    @serializer = Serializers::YardocSerializer.new(@file)
  end
  destroy unless merge

  sdb = Registry.single_object_db
  if sdb == true || sdb == nil
    @serializer.serialize(@store)
  else
    values(false).each do |object|
      @serializer.serialize(object)
    end
  end
  write_proxy_types
  write_object_types
  write_checksums
  true
end

#values(reload = false) ⇒ Array<CodeObjects::Base>

Gets all code objects from the store. Loads the entire database if reload is true

Parameters:

  • reload (Boolean) (defaults to: false)

    if false, does not load the entire database before a lookup.

Returns:



88
# File 'lib/yard/registry_store.rb', line 88

def values(reload = false) load_all if reload; @store.values end

#values_for_type(type, reload = false) ⇒ Array<CodeObjects::Base>

Returns a list of objects with a given CodeObjects::Base#type.

Parameters:

  • type (Symbol)

    the type to look for

Returns:

Since:

  • 0.8.0



103
104
105
106
# File 'lib/yard/registry_store.rb', line 103

def values_for_type(type, reload = false)
  load_all if reload
  paths_for_type(type).map {|t| @store[t.to_sym] }
end