Module: IMW::Schemes::Local::LocalFile

Defined in:
lib/imw/schemes/local.rb

Overview

Defines methods for appropriate for a local file.

Instance Method Summary collapse

Instance Method Details

#<<(text) ⇒ Object

Write the text with a trailing newline to this resource.

Parameters:



130
131
132
# File 'lib/imw/schemes/local.rb', line 130

def << text
  io.write text.to_s + "\n"
end

#closeObject

Close this resource’s file handle if it exists.



97
98
99
100
101
102
# File 'lib/imw/schemes/local.rb', line 97

def close
  # explicitly check the @io instance variable b/c self.io
  # will open up a new handle by default
  io.close if @io
  super()
end

#emit(data, options = {}) ⇒ Object

Emit data into this file.

Parameters:



160
161
162
163
164
# File 'lib/imw/schemes/local.rb', line 160

def emit data, options={}
  data.each do |element|  # works if data is an Array or a String
    io << (element.to_s)
  end
end

#ioFile

Return the IO object at this path.

Returns:

  • (File)


92
93
94
# File 'lib/imw/schemes/local.rb', line 92

def io
  @io ||= open(path, mode)
end

#is_file?true, false

Is this resource a regular file?

Returns:

  • (true, false)


77
78
79
# File 'lib/imw/schemes/local.rb', line 77

def is_file?
  true
end

#load {|String| ... } ⇒ Array

Return the lines in this file.

If passed a block, yield each line of the file to the block.

Yields:

  • (String)

    each line of the file

Returns:

  • (Array)

    the lines in the file



140
141
142
143
144
145
146
147
148
# File 'lib/imw/schemes/local.rb', line 140

def load &block
  if block_given?
    io.each do |line|
      yield line
    end
  else
    read.split("\n")
  end
end

#map {|String| ... } ⇒ Object

Map over the lines in this file.

Yields:

  • (String)

    each line of the file



153
154
155
# File 'lib/imw/schemes/local.rb', line 153

def map &block
  io.map(&block)
end

#num_charsInteger

Return the number of characters in this file.

Returns:

  • (Integer)


199
200
201
# File 'lib/imw/schemes/local.rb', line 199

def num_chars
  wc[2]
end

#num_linesInteger

Return the number of lines in this file.

Returns:

  • (Integer)


185
186
187
# File 'lib/imw/schemes/local.rb', line 185

def num_lines
  wc[0]
end

#num_wordsInteger

Return the number of words in this file.

Returns:

  • (Integer)


192
193
194
# File 'lib/imw/schemes/local.rb', line 192

def num_words
  wc[1]
end

#read(length = nil) ⇒ String

Read from this file.

Parameters:

  • length (Fixnum) (defaults to: nil)

    bytes to read

Returns:



108
109
110
# File 'lib/imw/schemes/local.rb', line 108

def read length=nil
  io.read(length)
end

#readlineString

Read a line from this file.

Returns:



115
116
117
# File 'lib/imw/schemes/local.rb', line 115

def readline
  io.readline
end

#rmObject Also known as: rm!

Delete this resource.



82
83
84
85
86
# File 'lib/imw/schemes/local.rb', line 82

def rm
  should_exist!("Cannot delete")
  FileUtils.rm path
  self
end

#snippetString

Return a snippet of text from this resource.

Will read the first 1024 bytes and strip non-ASCII characters from them. For more control, redefine this method in another module.

Returns:



173
174
175
176
177
178
179
180
# File 'lib/imw/schemes/local.rb', line 173

def snippet
  returning([]) do |snip|
    io.read(1024).bytes.each do |byte|
                                # CR            LF          SPACE            ~
      snip << byte.chr if byte == 13 || byte == 10 || byte >= 32 && byte <= 126
    end
  end.join
end

#summaryObject

Return a summary of properties of this local file.

Returned properties include

  • basename

  • size

  • extension

  • snippet



210
211
212
213
214
215
216
217
218
219
220
# File 'lib/imw/schemes/local.rb', line 210

def summary
  data = {
    :basename  => basename,
    :size      => size,
    :extension => extension,
    :num_lines => num_lines
  }
  data[:snippet] = snippet if respond_to?(:snippet)
  data[:schema]  = schema  if respond_to?(:schema)
  data
end

#write(text) ⇒ Fixnum

Write to this file

Parameters:

  • text (String, #to_s)

    text to write

Returns:

  • (Fixnum)

    bytes written



123
124
125
# File 'lib/imw/schemes/local.rb', line 123

def write text
  io.write text
end