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

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_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

.messagesObject

Get a list of message names that this object receives.



35
36
37
38
39
40
41
# File 'lib/gloo/objs/system/file_handle.rb', line 35

def self.messages
  basic = %w[read write get_name get_ext get_parent]
  checks = %w[exists? is_file? is_dir?]
  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.



24
25
26
# File 'lib/gloo/objs/system/file_handle.rb', line 24

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



17
18
19
# File 'lib/gloo/objs/system/file_handle.rb', line 17

def self.typename
  return KEYWORD
end

Instance Method Details

#msg_exists?Boolean

Check to see if the file exists.

Returns:



105
106
107
108
# File 'lib/gloo/objs/system/file_handle.rb', line 105

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

#msg_find_matchObject

Look for any file matching pattern.



129
130
131
132
# File 'lib/gloo/objs/system/file_handle.rb', line 129

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

#msg_get_extObject

Get the file’s extension.



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

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.



137
138
139
140
141
142
143
144
# File 'lib/gloo/objs/system/file_handle.rb', line 137

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.



160
161
162
163
164
165
166
# File 'lib/gloo/objs/system/file_handle.rb', line 160

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

#msg_is_dir?Boolean

Check to see if the file is a directory.

Returns:



121
122
123
124
# File 'lib/gloo/objs/system/file_handle.rb', line 121

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:



113
114
115
116
# File 'lib/gloo/objs/system/file_handle.rb', line 113

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

#msg_openObject

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



46
47
48
49
50
51
52
# File 'lib/gloo/objs/system/file_handle.rb', line 46

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.



57
58
59
60
61
# File 'lib/gloo/objs/system/file_handle.rb', line 57

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

  system "less #{value}"
end

#msg_readObject

Read the contents of the file into the object.



75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/gloo/objs/system/file_handle.rb', line 75

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

  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.



66
67
68
69
70
# File 'lib/gloo/objs/system/file_handle.rb', line 66

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

  puts File.read( value )
end

#msg_writeObject

Write the given data out to the file.



91
92
93
94
95
96
97
98
99
100
# File 'lib/gloo/objs/system/file_handle.rb', line 91

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