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.



120
121
122
123
124
125
# File 'lib/xmp2assert/quasifile.rb', line 120

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.



114
115
116
# File 'lib/xmp2assert/quasifile.rb', line 114

def __ENCODING__
  @__ENCODING__
end

#__FILE__String (readonly)

Returns file name of this script.

Returns:

  • (String)

    file name of this script.



112
113
114
# File 'lib/xmp2assert/quasifile.rb', line 112

def __FILE__
  @__FILE__
end

#__LINE__Integer (readonly)

Returns line offset.

Returns:

  • (Integer)

    line offset.



113
114
115
# File 'lib/xmp2assert/quasifile.rb', line 113

def __LINE__
  @__LINE__
end

#readString (readonly)

Returns content of the ruby script.

Returns:

  • (String)

    content of the ruby script.



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

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:

Returns:



87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/xmp2assert/quasifile.rb', line 87

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.



130
131
132
# File 'lib/xmp2assert/quasifile.rb', line 130

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

#pretty_print_instance_variablesObject

For pretty print



138
139
140
# File 'lib/xmp2assert/quasifile.rb', line 138

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