Module: Open

Defined in:
lib/open/base/base.rb,
lib/open/open.rb,
lib/open/last/last.rb,
lib/open/project/project.rb,
lib/open/version/version.rb,
lib/open/last_url/last_url.rb,
lib/open/constants/constants.rb,
lib/open/in_editor/in_editor.rb,
lib/open/nano_open/nano_open.rb,
lib/open/in_browser/in_browser.rb,
lib/open/with_delay/with_delay.rb,
lib/open/these_files/these_files.rb,
lib/open/toplevel_code/toplevel_code.rb

Overview

#

require 'open/toplevel_code/toplevel_code.rb'

#

Defined Under Namespace

Classes: Base, InBrowser, InEditor, Last, LastUrl, NanoOpen, Open, TheseFiles, WithDelay

Constant Summary collapse

PROJECT_BASE_DIRECTORY =
#

Open::PROJECT_BASE_DIRECTORY

#
File.absolute_path("#{__dir__}/..")+'/'
PROJECT_YAML_DIRECTORY =
#

Open::PROJECT_YAML_DIRECTORY

#
"#{Open.project_base_dir?}yaml/"
VERSION =
#

VERSION

#
'0.1.22'
LAST_UPDATE =
#

LAST_UPDATE

#
'17.07.2022'
IN_BACKGROUND =
#

IN_BACKGROUND

#
' &'
HOME_DIRECTORY_OF_USER_X =
#

HOME_DIRECTORY_OF_USER_X

This constant is only useful on my home system.

#
'/home/x/'
RUBY_SRC =
#

RUBY_SRC

#
"#{HOME_DIRECTORY_OF_USER_X}programming/ruby/src/"
PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME =
#

PROGRAMMING_LANGUAGES_DIRECTORY_AT_HOME

#
"#{HOME_DIRECTORY_OF_USER_X}programming/"
USERFIND =
#

USERFIND

#
"find #{HOME_DIRECTORY_OF_USER_X}data -name"
MY_DATA =
#

MY_DATA

This is functionality equivalent to the String '/home/x/data/'.

#
"#{HOME_DIRECTORY_OF_USER_X}data/"
DATA_DIRECTORY_AT_HOME =

DATA_DIRECTORY_AT_HOME

MY_DATA
N_DELAY =
#

N_DELAY

This delay is specifically used for delaying before batch-opening files via the Open.in_editor() functionality.

#
0.48
LOCATION_OF_BROWSER_YAML_FILE =
#

Open::LOCATION_OF_BROWSER_YAML_FILE

Here we must define where we store the location for our browser.

That file will tell us which browser to use.

Most users will not have this file, so the code will have to remain flexible in this regard.

On my home system this will point towards the following file:

/home/Programs/Ruby/3.1.2/lib/ruby/site_ruby/3.1.0/open/yaml/use_this_browser.yml
#
"#{project_yaml_directory?}use_this_browser.yml"
LOCATION_OF_EDITOR_YAML_FILE =
#

LOCATION_OF_EDITOR_YAML_FILE

#
"#{project_yaml_directory?}use_this_editor.yml"
USE_THIS_EDITOR =

else use a hardcoded default

'bluefish'
USE_THIS_BROWSER =

else use a hardcoded default

'firefox'

Class Method Summary collapse

Class Method Details

.[](i = ARGV, &block) ⇒ Object

#

Open[]

This method defaults to Open.in_editor(), but if the input starts with “http” then we will use Open.in_browser() instead.

Usage example:

Open[TEST_THIS_FILE]
#

133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/open/toplevel_code/toplevel_code.rb', line 133

def self.[](i = ARGV, &block)
  unless Open.respond_to? :in_browser
    require 'open/in_browser/in_browser.rb'
  end
  unless Open.respond_to? :in_editor
    require 'open/in_editor/in_editor.rb'
  end
  if i.is_a? Array
    i = i.join(' ').strip
  end
  if i.start_with? 'http'
    ::Open.in_browser(i, &block)
  else # this is the default
    ::Open.in_editor(i, &block)
  end
end

.e(i = '') ⇒ Object

#

Open.e

#

14
15
16
# File 'lib/open/toplevel_code/toplevel_code.rb', line 14

def self.e(i = '')
  puts i
end

.host_os?Boolean

#

Open.host_os?

Return the host-operating system via this method.

#

Returns:

  • (Boolean)

23
24
25
# File 'lib/open/toplevel_code/toplevel_code.rb', line 23

def self.host_os?
  ::RbConfig::CONFIG['host_os']
end

.in_editor(i = ARGV, &block) ⇒ Object

#

Open.in_editor

Usage examples:

Open.in_editor('/home/x/programming/ruby/src/open/open.gemspec')

Or theb lock variant:

Open.in_editor {{
  this_file:       first_argument?,
  use_this_editor: :nano
}}
#

299
300
301
302
303
# File 'lib/open/in_editor/in_editor.rb', line 299

def self.in_editor(
    i = ARGV, &block
  )
  ::Open::InEditor.new(i, &block)
end

.is_on_roebe?Boolean

#

Roebe.is_on_roebe?

#

Returns:

  • (Boolean)

153
154
155
# File 'lib/open/toplevel_code/toplevel_code.rb', line 153

def self.is_on_roebe?
  ENV['IS_ROEBE'].to_s == '1'
end

.is_on_windows?(i = host_os?) ) ⇒ Boolean

#

Open.is_on_windows?

#

Returns:

  • (Boolean)

30
31
32
33
34
35
36
37
38
# File 'lib/open/toplevel_code/toplevel_code.rb', line 30

def self.is_on_windows?(i = host_os?)
  case i # or: RUBY_PLATFORM
  when /win/,
       /mingw/
    true
  else
    false
  end
end

.last_url(optional_arguments = ARGV) ⇒ Object

#

Open.last_url

#

141
142
143
# File 'lib/open/last_url/last_url.rb', line 141

def self.last_url(optional_arguments = ARGV)
  ::Open::LastUrl[optional_arguments]
end

.open(i = ARGV, &block) ⇒ Object

#

Open.open

#

991
992
993
# File 'lib/open/open.rb', line 991

def self.open(i = ARGV, &block)
  ::Open::Open.new(i, &block)
end

.open_in_browser(i = ARGV, &block) ⇒ Object

#

Open.open_in_browser

Note that this method needs to be somewhat flexible, as we can provide varied input such as:

Open.open_in_browser(port: 8080)
Open.in_browser(remote_URL) {{ use_this_browser: :firefox }}
#

505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
# File 'lib/open/in_browser/in_browser.rb', line 505

def self.open_in_browser(
    i = ARGV, &block
  )
  # ======================================================================= #
  # === Handle blocks next
  # ======================================================================= #
  if block_given?
    yielded = yield
    # ===================================================================== #
    # === Handle Hashes next
    # ===================================================================== #
    if yielded.is_a? Hash
      # =================================================================== #
      # === Handle Hashes past this point
      # =================================================================== #
      # =================================================================== #
      # === :delay
      # =================================================================== #
      if yielded.has_key? :delay
        _ = yielded.delete(:delay)
        if _.is_a? String
          _ = _.sub(/ seconds/,'')
          _ = _.sub(/ second/,'').to_f if _.include? ' second'
          _ = _.to_f # Need a Float.
        end
        sleep(_)
      end
    end
  # else
  end
  ::Open::InBrowser.new(i, &block)
end

.open_last(i = ARGV) ⇒ Object

#

Open.open_last

#

135
136
137
# File 'lib/open/last/last.rb', line 135

def self.open_last(i = ARGV)
  ::Open::Last.new(i)
end

.permanently_use_this_browser(this_browser = 'thorium', store_into_this_file = ::Open::LOCATION_OF_BROWSER_YAML_FILE) ⇒ Object

#

Open.permanently_use_this_browser

This method can be used to permanently store a new browser, for the open gem.

#

99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/open/toplevel_code/toplevel_code.rb', line 99

def self.permanently_use_this_browser(
    this_browser         = 'thorium',
    store_into_this_file = ::Open::LOCATION_OF_BROWSER_YAML_FILE
  )
  this_browser = this_browser.to_s
  e 'Storing into `'+store_into_this_file+'`.'
  write_what_into(this_browser, store_into_this_file)
  if is_on_roebe?
    store_into_this_file = '/home/x/programming/ruby/src/open/lib/open/yaml/use_this_browser.yml'
    e 'Storing into `'+store_into_this_file+'`.'
    write_what_into(this_browser, store_into_this_file)
  end 
end

.project_base_directory?Boolean

#

Open.project_base_directory?

#

Returns:

  • (Boolean)

18
19
20
# File 'lib/open/project/project.rb', line 18

def self.project_base_directory?
  PROJECT_BASE_DIRECTORY
end

.project_yaml_dir?Boolean

#

Open.project_yaml_dir?

This is a query-method for the constant PROJECT_YAML_DIRECTORY.

#

Returns:

  • (Boolean)

35
36
37
# File 'lib/open/project/project.rb', line 35

def self.project_yaml_dir?
  PROJECT_YAML_DIRECTORY
end

.set_use_this_browser(i = USE_THIS_BROWSER) ⇒ Object

#

Open.set_use_this_browser

This method can be used to assign a different browser.

#

75
76
77
78
79
# File 'lib/open/toplevel_code/toplevel_code.rb', line 75

def self.set_use_this_browser(
    i = USE_THIS_BROWSER
  )
  @use_this_browser = i
end

.set_use_this_editor(i = USE_THIS_EDITOR) ⇒ Object

#

Open.set_use_this_editor

Modify the toplevel variable @use_this_editor through this method.

#

50
51
52
53
54
# File 'lib/open/toplevel_code/toplevel_code.rb', line 50

def self.set_use_this_editor(
    i = USE_THIS_EDITOR
  )
  @use_this_editor = i
end

.these_files(i = ARGV, &block) ⇒ Object

#

Open.these_files

#

99
100
101
102
103
# File 'lib/open/these_files/these_files.rb', line 99

def self.these_files(
    i = ARGV, &block
  )
  ::Open::TheseFiles.new(i, &block)
end

.use_which_browser?Boolean

#

Open.use_which_browser?

Query-method to determine which browser is currently designated to be the main browser, as far as the open-gem is concerned.

#

Returns:

  • (Boolean)

89
90
91
# File 'lib/open/toplevel_code/toplevel_code.rb', line 89

def self.use_which_browser?
  @use_this_browser
end

.use_which_editor?Boolean

#

Open.use_which_editor?

#

Returns:

  • (Boolean)

61
62
63
# File 'lib/open/toplevel_code/toplevel_code.rb', line 61

def self.use_which_editor?
  @use_this_editor
end

.with_delay(i = ARGV) ⇒ Object

#

Open.with_delay

#

253
254
255
# File 'lib/open/with_delay/with_delay.rb', line 253

def self.with_delay(i = ARGV)
  Open::WithDelay.new(i)
end

.write_what_into(what, into) ⇒ Object

#

Open.write_what_into

Delegate towards SaveFile.

#

118
119
120
# File 'lib/open/toplevel_code/toplevel_code.rb', line 118

def self.write_what_into(what, into)
  ::SaveFile.write_what_into(what, into)
end