Class: Amp::DelayedOpener

Inherits:
Object show all
Defined in:
lib/amp/revlogs/changelog.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(realopener, owner) ⇒ DelayedOpener

Initializes the delayed opener, needing the real deal, and the owner of the delayed opener.

Parameters:

  • realopener (Opener)

    the actual opener. Since the DelayedOpener doesn’t know how to open files, we’ll need this!

  • owner (ChangeLog)

    the owner of the delayed opener. This reference is needed because we need to check on the state of the delayed_buffer and what not.



116
117
118
# File 'lib/amp/revlogs/changelog.rb', line 116

def initialize(realopener, owner)
  @real_opener, @owner = realopener, owner
end

Instance Attribute Details

#defaultObject

Returns the value of attribute default.



105
106
107
# File 'lib/amp/revlogs/changelog.rb', line 105

def default
  @default
end

Instance Method Details

#open(name, mode = "r") ⇒ Object

Specialized opener that will fake writing when asked to. We need to know who our owner is and we need a real opener so we can actually open files. The DelayedOpener doesn’t know how to do real IO.



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/amp/revlogs/changelog.rb', line 124

def open(name, mode="r")
  fp = @real_opener.open(name, mode)
  return fp if name != @owner.index_file
  
  # Are we holding off on writing? if so, set up the file where we
  # writing pending data.
  
  if @owner.delay_count == 0
    @owner.delay_name = File.amp_name(fp)
    mode.gsub!(/a/, 'w') if @owner.empty?
    return @real_opener.open(name+".a", mode) # See what I did there?
  end
  
  # FakeFileAppender = fake file so we don't do any real writing yet
  size = File.stat(@real_opener.join(name)).size
  ffa = FakeFileAppender.new(fp, @owner.delay_buffer, size)
  
  return ffa
end