Class: Openapi3Parser::SourceInput::File

Inherits:
Openapi3Parser::SourceInput show all
Defined in:
lib/openapi3_parser/source_input/file.rb

Overview

An input of a file on the file system

Instance Attribute Summary collapse

Attributes inherited from Openapi3Parser::SourceInput

#access_error, #parse_error

Instance Method Summary collapse

Methods inherited from Openapi3Parser::SourceInput

#available?, #contents

Constructor Details

#initialize(path, working_directory: nil) ⇒ File

Returns a new instance of File.

Parameters:

  • path (String)

    The path to the file to open as this source

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

    The path to the working directory to use, will be calculated from path if not provided



23
24
25
26
27
28
# File 'lib/openapi3_parser/source_input/file.rb', line 23

def initialize(path, working_directory: nil)
  @path = ::File.absolute_path(path)
  working_directory ||= resolve_working_directory
  @working_directory = ::File.absolute_path(working_directory)
  initialize_contents
end

Instance Attribute Details

#pathString (readonly)

The absolute path to this file

Returns:

  • (String)

    the current value of path



14
15
16
# File 'lib/openapi3_parser/source_input/file.rb', line 14

def path
  @path
end

#working_directoryString (readonly)

The abolsute path of the working directory to use when opening relative references to this file

Returns:

  • (String)

    the current value of working_directory



14
15
16
# File 'lib/openapi3_parser/source_input/file.rb', line 14

def working_directory
  @working_directory
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

Returns:

  • (Boolean)

See Also:

  • SourceInput#other


40
41
42
43
44
# File 'lib/openapi3_parser/source_input/file.rb', line 40

def ==(other)
  return false unless other.instance_of?(self.class)
  path == other.path &&
    working_directory == other.working_directory
end

#inspectObject

return [String]



47
48
49
50
# File 'lib/openapi3_parser/source_input/file.rb', line 47

def inspect
  %{#{self.class.name}(path: #{path}, working_directory: } +
    %{#{working_directory})}
end

#relative_to(source_input) ⇒ String

Attempt to return a shorter relative path to the other source input so we can produce succinct output

Returns:

  • (String)


61
62
63
64
65
66
67
68
69
70
# File 'lib/openapi3_parser/source_input/file.rb', line 61

def relative_to(source_input)
  other_path = if source_input.respond_to?(:path)
                 ::File.dirname(source_input.path)
               elsif source_input.respond_to?(:working_directory)
                 source_input.working_directory
               end

  return path unless other_path
  other_path ? relative_path(other_path, path) : path
end

#resolve_next(reference) ⇒ SourceInput

Parameters:

Returns:

See Also:



33
34
35
# File 'lib/openapi3_parser/source_input/file.rb', line 33

def resolve_next(reference)
  ResolveNext.call(reference, self, working_directory: working_directory)
end

#to_sString

Returns:

  • (String)


53
54
55
# File 'lib/openapi3_parser/source_input/file.rb', line 53

def to_s
  path
end