Class: ShellHelpers::VirtualFile

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/shell_helpers/pathname.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, content) ⇒ VirtualFile

Returns a new instance of VirtualFile.



596
597
598
599
600
# File 'lib/shell_helpers/pathname.rb', line 596

def initialize(name, content)
  @content=content
  @tmpfile=nil
  @name=name
end

Instance Attribute Details

#contentObject

Returns the value of attribute content.



594
595
596
# File 'lib/shell_helpers/pathname.rb', line 594

def content
  @content
end

#nameObject

Returns the value of attribute name.



594
595
596
# File 'lib/shell_helpers/pathname.rb', line 594

def name
  @name
end

#tmpfileObject

Returns the value of attribute tmpfile.



594
595
596
# File 'lib/shell_helpers/pathname.rb', line 594

def tmpfile
  @tmpfile
end

Instance Method Details

#create(unlink = false) ⇒ Object



611
612
613
614
615
616
617
618
619
620
621
622
623
# File 'lib/shell_helpers/pathname.rb', line 611

def create(unlink=false)
  require 'tempfile'
  unless @tmpfile&.path
    @tmpfile = Tempfile.new(@name)
    @tmpfile.write(@content)
    @tmpfile.flush
  end
  if block_given?
    yield path
    @tmpfile.close(unlink)
  end
  path
end

#fileObject



606
607
608
609
# File 'lib/shell_helpers/pathname.rb', line 606

def file
  create
  path
end

#pathObject



602
603
604
# File 'lib/shell_helpers/pathname.rb', line 602

def path
  @tmpfile&.path && Pathname.new(@tmpfile.path)
end

#shellescapeObject



629
630
631
632
# File 'lib/shell_helpers/pathname.rb', line 629

def shellescape
  create
  to_s&.shellescape
end

#to_sObject



625
626
627
# File 'lib/shell_helpers/pathname.rb', line 625

def to_s
  @tmpfile&.path || "VirtualFile:#{@name}"
end