Class: Gloo::Objs::FileHandle

Inherits:
Core::Obj show all
Defined in:
lib/gloo/objs/system/file_handle.rb

Constant Summary collapse

KEYWORD =
'file'.freeze
KEYWORD_SHORT =
'dir'.freeze
FILE_NAME_ERR =
'file and path name expected'.freeze
FILE_MISSING_ERR =
'file not found'.freeze

Constants inherited from Core::Baseo

Core::Baseo::NOT_IMPLEMENTED_ERR

Instance Attribute Summary

Attributes inherited from Core::Obj

#children, #parent, #value

Attributes inherited from Core::Baseo

#name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Obj

#add_child, #add_children_on_create?, #add_default_children, can_create?, #can_receive_message?, #child_count, #child_index, #contains_child?, #delete_children, #dispatch, #display_value, #find_add_child, #find_child, #find_child_resolve_alias, #find_child_value, help, inherited, #initialize, #is_alias?, #is_container?, #is_function?, #msg_blank?, #msg_contains?, #msg_reload, #msg_responds_to?, #msg_unload, #multiline_value?, #pn, #remove_child, #render, #root?, #send_message, #set_parent, #set_value, #sql_value, #type_display, #value_display, #value_is_array?, #value_is_blank?, #value_string?

Methods inherited from Core::Baseo

#initialize, #type_display

Constructor Details

This class inherits a constructor from Gloo::Core::Obj

Class Method Details

.doc_dataObject

Get the object's documentation data.



249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
# File 'lib/gloo/objs/system/file_handle.rb', line 249

def self.doc_data
  {
    :name => KEYWORD,
    :shortcut => KEYWORD_SHORT,
    :description => 'Reference to a file or folder (directory) ' \
      'on disk. The string value of the file object is the path ' \
      'and name of the file.',
    :messages => [
      'read ({into.obj}) — Read the file and put the data in the specified object. If {into.obj} is not specified, the data will be in it.',
      'write ({from.obj}) — Write the data in {from.obj} into the file.',
      'append ({data}) — Append the given data to the file as a new line. Adds a leading newline first if the file doesn\'t already end with one.',
      'delete — Delete the file.',
      'get_name — Get the name of the file without path or extension. Put the name into it.',
      'get_ext — Get the file extension. Put the extension into it.',
      'get_parent — Get the file or folder path parent. Put the parent into it.',
      'get_sha256 — Get the SHA256 hash of the contents of the file. Put the hash value into it.',
      'show — Show the contents of the file.',
      'page — Show the contents of the file, paginated.',
      'open — Open the file with the default application for the type.',
      'exists? — Check to see if the file exists. It will be true or false.',
      'is_file? — Check to see if the file specified is a regular file. It will be true or false.',
      'is_dir? — Check to see if the file specified is a directory. It will be true or false.',
      'find_match — Look for the existence of a file matching the file\'s pattern. It will be true or false.',
      'mkdir — Create the file\'s path as a directory, if it doesn\'t already exist.'
    ],
    :examples => <<~EXAMPLES.strip
      #
      # Get elements of a file name.
      #
      file_name [can] :

        # The full file path, name and extension.
        f [file] : /Users/me/dev/gloo/gloo.gemspec

        on_load [script] :
          show "Full file name:  " + ^.f

          check ^.f for get_name
          show "The file name alone: " and it

          check ^.f for get_ext
          show "The file extension: " and it

          check ^.f for get_parent
          show "The file path: " and it
    EXAMPLES
  }
end

.hash_for_file(file_path) ⇒ Object

Get the SHA256 hash of the file contents.



217
218
219
220
221
222
# File 'lib/gloo/objs/system/file_handle.rb', line 217

def self.hash_for_file( file_path )
  require 'digest'
  file_data = File.read( file_path )
  file_hash = Digest::SHA256.hexdigest( file_data )
  return file_hash
end

.messagesObject

Get a list of message names that this object receives.



39
40
41
42
43
44
45
# File 'lib/gloo/objs/system/file_handle.rb', line 39

def self.messages
  basic = %w[read write append delete get_name get_ext get_parent get_sha256]
  checks = %w[exists? is_file? is_dir? mkdir]
  search = %w[find_match]
  show = %w[show page open]
  return super + basic + show + checks + search
end

.short_typenameObject

The short name of the object type.



28
29
30
# File 'lib/gloo/objs/system/file_handle.rb', line 28

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



21
22
23
# File 'lib/gloo/objs/system/file_handle.rb', line 21

def self.typename
  return KEYWORD
end

Instance Method Details

#check_file_exists?Boolean

Check to see if the file exists. Show error if not.

Returns:



228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/gloo/objs/system/file_handle.rb', line 228

def check_file_exists?
  if value.blank?
    @engine.log.error FILE_NAME_ERR
    return false
  end

  unless File.exist?( value )
    @engine.log.error FILE_MISSING_ERR
    return false
  end

  return true
end

#msg_appendObject

Append the given data to the file as a new line. Adds a leading newline if the file does not already end with one.



96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/gloo/objs/system/file_handle.rb', line 96

def msg_append
  data = ''
  return unless value

  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    data = expr.evaluate
  end
  existing = File.exist?( value ) ? File.read( value ) : ''
  prefix = existing.empty? || existing.end_with?( "\n" ) ? '' : "\n"
  File.open( value, 'a' ) { |f| f.puts "#{prefix}#{data}" }
end

#msg_deleteObject

Delete the file.



126
127
128
129
# File 'lib/gloo/objs/system/file_handle.rb', line 126

def msg_delete
  return unless value
  File.delete( value )
end

#msg_exists?Boolean

Check to see if the file exists.

Returns:



134
135
136
137
# File 'lib/gloo/objs/system/file_handle.rb', line 134

def msg_exists?
  result = File.exist? value
  @engine.heap.it.set_to result
end

#msg_find_matchObject

Look for any file matching pattern.



165
166
167
168
# File 'lib/gloo/objs/system/file_handle.rb', line 165

def msg_find_match
  result = !Dir.glob( value ).empty?
  @engine.heap.it.set_to result
end

#msg_get_extObject

Get the file's extension.



185
186
187
188
189
190
191
# File 'lib/gloo/objs/system/file_handle.rb', line 185

def msg_get_ext
  if value.blank?
    @engine.heap.it.set_to ''
  else
    @engine.heap.it.set_to File.extname( value )
  end
end

#msg_get_nameObject

Get the name of the file.



173
174
175
176
177
178
179
180
# File 'lib/gloo/objs/system/file_handle.rb', line 173

def msg_get_name
  if value.blank?
    @engine.heap.it.set_to ''
  else
    file_name = File.basename( value, File.extname( value ) )
    @engine.heap.it.set_to file_name
  end
end

#msg_get_parentObject

Get the parent directory of the file.



196
197
198
199
200
201
202
# File 'lib/gloo/objs/system/file_handle.rb', line 196

def msg_get_parent
  if value.blank?
    @engine.heap.it.set_to ''
  else
    @engine.heap.it.set_to File.dirname( value )
  end
end

#msg_get_sha256Object

Get the SHA256 hash of the file contents.



207
208
209
210
211
212
# File 'lib/gloo/objs/system/file_handle.rb', line 207

def msg_get_sha256
  return unless check_file_exists?

  file_hash = FileHandle.hash_for_file( value )
  @engine.heap.it.set_to file_hash
end

#msg_is_dir?Boolean

Check to see if the file is a directory.

Returns:



150
151
152
153
# File 'lib/gloo/objs/system/file_handle.rb', line 150

def msg_is_dir?
  result = File.directory? value
  @engine.heap.it.set_to result
end

#msg_is_file?Boolean

Check to see if the file is a file.

Returns:



142
143
144
145
# File 'lib/gloo/objs/system/file_handle.rb', line 142

def msg_is_file?
  result = File.file? value
  @engine.heap.it.set_to result
end

#msg_mkdirObject

Create a directory.



158
159
160
# File 'lib/gloo/objs/system/file_handle.rb', line 158

def msg_mkdir
  FileUtils.mkdir_p(value) unless Dir.exist?(value)
end

#msg_openObject

Open the file in the default application for the file type.



50
51
52
53
54
55
56
# File 'lib/gloo/objs/system/file_handle.rb', line 50

def msg_open
  return unless value && File.exist?( value )

  cmd = Gloo::Core::GlooSystem.open_for_platform
  cmd_with_param = "#{cmd} \"#{value}\""
  `#{cmd_with_param}`
end

#msg_pageObject

Show the contents of the file, paginated.



61
62
63
64
65
# File 'lib/gloo/objs/system/file_handle.rb', line 61

def msg_page
  return unless value && File.file?( value )

  system "less #{value}"
end

#msg_readObject

Read the contents of the file into the object.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/gloo/objs/system/file_handle.rb', line 79

def msg_read
  return unless check_file_exists?

  data = File.read( value )
  if @params&.token_count&.positive?
    pn = Gloo::Core::Pn.new( @engine, @params.first )
    o = pn.resolve
    o.set_value data
  else
    @engine.heap.it.set_to data
  end
end

#msg_showObject

Show the contents of the file.



70
71
72
73
74
# File 'lib/gloo/objs/system/file_handle.rb', line 70

def msg_show
  return unless value && File.file?( value )

  puts File.read( value )
end

#msg_writeObject

Write the given data out to the file.



112
113
114
115
116
117
118
119
120
121
# File 'lib/gloo/objs/system/file_handle.rb', line 112

def msg_write
  data = ''
  return unless value

  if @params&.token_count&.positive?
    expr = Gloo::Expr::Expression.new( @engine, @params.tokens )
    data = expr.evaluate
  end
  File.write( value, data )
end