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

Includes:
Metadata::HasMetadata
Defined in:
lib/imw/schemes/local.rb

Overview

Defines methods for appropriate for a local file.

Instance Method Summary collapse

Methods included from Metadata::HasMetadata

#description, #description=, #fields, #fields=, #metadata, #schema

Instance Method Details

#<<(text) ⇒ Object

Write the text with a trailing newline to this resource.

Parameters:



132
133
134
# File 'lib/imw/schemes/local.rb', line 132

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

#closeObject

Close this resource’s file handle if it exists.



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

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:



162
163
164
165
166
# File 'lib/imw/schemes/local.rb', line 162

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

#external_summaryObject

Return a summary of properties of this local file.

Returned properties include

  • basename

  • size

  • extension

  • num_lines



212
213
214
215
216
217
# File 'lib/imw/schemes/local.rb', line 212

def external_summary
  super().merge({
                  :size      => size,
                  :num_lines => num_lines
                })
end

#ioFile

Return the IO object at this path.

Returns:

  • (File)


94
95
96
# File 'lib/imw/schemes/local.rb', line 94

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

#is_file?true, false

Is this resource a regular file?

Returns:

  • (true, false)


79
80
81
# File 'lib/imw/schemes/local.rb', line 79

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



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

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



155
156
157
# File 'lib/imw/schemes/local.rb', line 155

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

#num_charsInteger

Return the number of characters in this file.

Returns:

  • (Integer)


201
202
203
# File 'lib/imw/schemes/local.rb', line 201

def num_chars
  wc[2]
end

#num_linesInteger

Return the number of lines in this file.

Returns:

  • (Integer)


187
188
189
# File 'lib/imw/schemes/local.rb', line 187

def num_lines
  wc[0]
end

#num_wordsInteger

Return the number of words in this file.

Returns:

  • (Integer)


194
195
196
# File 'lib/imw/schemes/local.rb', line 194

def num_words
  wc[1]
end

#read(length = nil) ⇒ String

Read from this file.

Parameters:

  • length (Fixnum) (defaults to: nil)

    bytes to read

Returns:



110
111
112
# File 'lib/imw/schemes/local.rb', line 110

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

#readlineString

Read a line from this file.

Returns:



117
118
119
# File 'lib/imw/schemes/local.rb', line 117

def readline
  io.readline
end

#rmObject Also known as: rm!

Delete this resource.



84
85
86
87
88
# File 'lib/imw/schemes/local.rb', line 84

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:



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

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

#write(text) ⇒ Fixnum

Write to this file

Parameters:

  • text (String, #to_s)

    text to write

Returns:

  • (Fixnum)

    bytes written



125
126
127
# File 'lib/imw/schemes/local.rb', line 125

def write text
  io.write text
end