Class: XMP2Assert::Quasifile

Inherits:
Object
  • Object
show all
Includes:
PrettierInspect
Defined in:
lib/xmp2assert/quasifile.rb

Overview

XMP2Assert converts a ruby script into a test file but we want to hold original path name / line number for diagnostic purposes. So this class.

Instance Attribute Summary collapse

Inspection collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PrettierInspect

#inspect, #pretty_print

Constructor Details

#initialize(content, file, line) ⇒ Quasifile

Returns a new instance of Quasifile.

Parameters:

  • content (String)

    a content of a ruby script.

  • file (String)

    file path.

  • line (Integer)

    line offset.



124
125
126
127
128
129
# File 'lib/xmp2assert/quasifile.rb', line 124

def initialize(content, file, line)
  @__FILE__     = file
  @__LINE__     = line
  @__ENCODING__ = content.encoding
  @read         = content
end

Instance Attribute Details

#__ENCODING__Encoding (readonly)

Returns script encoding.

Returns:

  • (Encoding)

    script encoding.



118
119
120
# File 'lib/xmp2assert/quasifile.rb', line 118

def __ENCODING__
  @__ENCODING__
end

#__FILE__String (readonly)

Returns file name of this script.

Returns:

  • (String)

    file name of this script.



116
117
118
# File 'lib/xmp2assert/quasifile.rb', line 116

def __FILE__
  @__FILE__
end

#__LINE__Integer (readonly)

Returns line offset.

Returns:

  • (Integer)

    line offset.



117
118
119
# File 'lib/xmp2assert/quasifile.rb', line 117

def __LINE__
  @__LINE__
end

#readString (readonly)

Returns content of the ruby script.

Returns:

  • (String)

    content of the ruby script.



119
120
121
# File 'lib/xmp2assert/quasifile.rb', line 119

def read
  @read
end

Class Method Details

.new(qfile) ⇒ Quasifile .new(uri, file = uri.to_s, line = 1) ⇒ Quasifile .new(path, file = path.to_path, line = 1) ⇒ Quasifile .new(io, file = '(eval)', line = io.lineno+1) ⇒ Quasifile .new(str, file = '(eval)', line = 1) ⇒ Quasifile

Returns a new quasifile.

Overloads:

  • .new(qfile) ⇒ Quasifile

    Just return the given object (for possible recursive calls).

    Parameters:

    • qfile (Quasifile)

      an instance of this class.

    Returns:

  • .new(uri, file = uri.to_s, line = 1) ⇒ Quasifile

    Obtains the resource pointed by the URI, parses the resource as a ruby script, and constructs a quasifile according to that.

    Parameters:

    • uri (URI)

      a URI of a ruby script.

    • file (String) (defaults to: uri.to_s)

      file path.

    • line (Integer) (defaults to: 1)

      line offset.

    Returns:

    Raises:

    • (OpenURI::HTTPError)

      404 and such.

  • .new(path, file = path.to_path, line = 1) ⇒ Quasifile

    Same as uri version, but accepts a pathname instead.

    Parameters:

    • path (#to_path)

      a pathname that points to a ruby script.

    • file (String) (defaults to: path.to_path)

      file path.

    • line (Integer) (defaults to: 1)

      line offset.

    Returns:

    Raises:

    • (Errno::ENOENT)

      not found.

    • (Errno::EISDIR)

      path is directory.

    • (Errno::EACCESS)

      permission denied.

    • (Errno::ELOOP)

      infinite symlink.

  • .new(io, file = '(eval)', line = io.lineno+1) ⇒ Quasifile

    Same as pathname version, but it also directly accepts arbitrary IO instances to read ruby scripts from. It migth be handy for you to pass a pipe here. The script filename may or may not be inferred depending on the IO (Files might be able to, Sockets hardly likely). Failures in filename resolution do not render exceptions. Rather the info lacks silently.

    Parameters:

    • io (#to_io)

      an IO that can be read.

    • file (String) (defaults to: '(eval)')

      file path.

    • line (Integer) (defaults to: io.lineno+1)

      line offset.

    Returns:

    Raises:

    • (IOError)

      io not open for read, already closed, etc.

  • .new(str, file = '(eval)', line = 1) ⇒ Quasifile

    Same as io version, but it also directly accepts a ruby script as a string. Obviously in this case, you cannot infer its filename.

    Parameters:

    • str (#to_io)

      a content of a ruby script.

    • file (String) (defaults to: '(eval)')

      file path.

    • line (Integer) (defaults to: 1)

      line offset.

    Returns:

Parameters:

  • obj (Quasifile, URI, Pathname, String, File, IO)

    a file-ish.

  • file (String) (defaults to: nil)

    path of the file (optional).

  • line (Integer) (defaults to: nil)

    line offset (optional).

Returns:



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/xmp2assert/quasifile.rb', line 91

def self.new(obj, file = nil, line = nil)
  case
  when src  = switch { obj.to_str  } then # LIKELY
    return allocate.tap do |ret|
      ret.send(:initialize, src, file||'(eval)', line||1)
    end
  when self              === obj     then return obj
  when OpenURI::OpenRead === obj     then src, path = obj.read, obj.to_s
  when path = switch { obj.to_path } then src       = obj.read
  when io   = switch { obj.to_io   } then off, src  = io.lineno+1, io.read
  when src  = switch { obj.read    } then # unknown class but works
  else
    raise TypeError, "something readable expected but given: #{obj.class}"
  end

  return new(src, file || path, line || off) # recur
end

Instance Method Details

#eval(b = TOPLEVEL_BINDING) ⇒ Object

Eavluate the content script

Parameters:

  • b (Binding) (defaults to: TOPLEVEL_BINDING)

    target binding (default toplevel).

Returns:

  • anything that the content evaluates.



134
135
136
# File 'lib/xmp2assert/quasifile.rb', line 134

def eval b = TOPLEVEL_BINDING
  Kernel.eval @read, b, @__FILE__, @__LINE__
end

#pretty_print_instance_variablesObject

For pretty print



142
143
144
# File 'lib/xmp2assert/quasifile.rb', line 142

def pretty_print_instance_variables
  return %w'@__FILE__ @__LINE__'
end