Class: YARD::Serializers::FileSystemSerializer

Inherits:
Base
  • Object
show all
Defined in:
lib/yard/serializers/file_system_serializer.rb

Overview

Implements a serializer that reads from and writes to the filesystem.

Instance Attribute Summary collapse

Attributes inherited from Base

#options

Instance Method Summary collapse

Methods inherited from Base

#after_serialize, #before_serialize

Constructor Details

#initialize(opts = {}) ⇒ FileSystemSerializer

Creates a new FileSystemSerializer with options

Parameters:

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :basepath (String) — default: 'doc'

    the base path to write data to

  • :extension (String) — default: 'html'

    the extension of the serialized path filename. If this is set to the empty string, no extension is used.



27
28
29
30
31
# File 'lib/yard/serializers/file_system_serializer.rb', line 27

def initialize(opts = {})
  super
  @basepath = (options[:basepath] || 'doc').to_s
  @extension = (options.has_key?(:extension) ? options[:extension] : 'html').to_s
end

Instance Attribute Details

#basepathString

The base path to write data to.

Returns:



7
8
9
# File 'lib/yard/serializers/file_system_serializer.rb', line 7

def basepath
  @basepath
end

#extensionString

The extension of the filename (defaults to html)

Returns:

  • (String)

    the extension of the file. Empty string for no extension.



16
17
18
# File 'lib/yard/serializers/file_system_serializer.rb', line 16

def extension
  @extension
end

Instance Method Details

#exists?(object) ⇒ Boolean

Checks the disk for an object and returns whether it was serialized.

Parameters:

Returns:

  • (Boolean)

    whether an object has been serialized to disk



83
84
85
# File 'lib/yard/serializers/file_system_serializer.rb', line 83

def exists?(object)
  File.exist?(File.join(basepath, serialized_path(object)))
end

#serialize(object, data) ⇒ String

Serializes object with data to its serialized path (prefixed by the #basepath).

Returns:

  • (String)

    the written data (for chaining)



36
37
38
39
40
# File 'lib/yard/serializers/file_system_serializer.rb', line 36

def serialize(object, data)
  path = File.join(basepath, *serialized_path(object))
  log.debug "Serializing to #{path}"
  File.open!(path, "wb") {|f| f.write data }
end

#serialized_path(object) ⇒ String

Implements the serialized path of a code object.

Parameters:

Returns:

  • (String)

    if object is a String, returns object, otherwise the path on disk (without the basepath).



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/yard/serializers/file_system_serializer.rb', line 48

def serialized_path(object)
  return object if object.is_a?(String)

  objname = object != YARD::Registry.root ? object.name.to_s : "top-level-namespace"
  objname += '_' + object.scope.to_s[0,1] if object.is_a?(CodeObjects::MethodObject)
  fspath = [objname + (extension.empty? ? '' : ".#{extension}")]
  if object.namespace && object.namespace.path != ""
    fspath.unshift(*object.namespace.path.split(CodeObjects::NSEP))
  end
  
  # Don't change the filenames, it just makes it more complicated
  # to figure out the original name.
  #fspath.map! do |p| 
  #  p.gsub(/([a-z])([A-Z])/, '\1_\2').downcase 
  #end
  
  # Remove special chars from filenames.
  # Windows disallows \ / : * ? " < > | but we will just remove any
  # non alphanumeric (plus period, underscore and dash).
  fspath.map! do |p|
    p.gsub(/[^\w\.-]/) do |x|
      encoded = '_'

      x.each_byte { |b| encoded << ("%X" % b) }
      encoded
    end
  end
  
  File.join(fspath)
end