host-os 0.2.0 → 0.2.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8259f7f3965b419b1c41233b4e6a1a2726b78662f5506e3d397a0cab916ea5bc
4
- data.tar.gz: a0dabfba2f2ac721b422d62c0eb297ca42e0942215399974f43061dded676ba3
3
+ metadata.gz: 9a1718752411b34a816e31b58be6f1a3cd83aec89b851e74f6997998e173dcad
4
+ data.tar.gz: fc322f198a39c3b5b7792012b2206960e0889302a97716ee9bed733b485b642f
5
5
  SHA512:
6
- metadata.gz: f554e8e5562e7c1bc5d58bd45422529e3838bb6e21cfc132b3f3010c639ed78c9928f2433868091e540ac50553b578245afc77e52fa63463520f3dd81f33cb2b
7
- data.tar.gz: 2210f64c92376f4f498e4d3d3041bb36a1f5a2c2eb592c6686540f21c037d979e0a72251d973ad94e0c6995e54bb8d2a4cce9bbfa66dd0855e0aa567cc33d816
6
+ metadata.gz: 3652cc6e8ddbc5c5489bec318e48f7ce5c73f1c84aa4940887ba080767a8173835e24b2739db27fff6a16f4aaa9f980d9d59edfa66c1ade9e4a296357043bbe2
7
+ data.tar.gz: 3fd4535322bb9d0de53af3d2ae0695e4199e31976fb472f0d631c1c57750d5c1eac6174abcd16b8e52b4ffa01b3e5ef642dc027c71e4f913fe0412885aeb93cd
@@ -0,0 +1,137 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative('../host-os') unless defined?(HostOS.id)
4
+
5
+ module HostOS
6
+ #
7
+ # This module allows to identify the used Ruby interpreter.
8
+ #
9
+ # Besides here documented boolean attributes you can also check for any other
10
+ # boolean attribute or interpreter name:
11
+ #
12
+ # @example Query for the Opal interpreter
13
+ # HostOS.interpreter.opal?
14
+ # @example Query for TruffleRuby
15
+ # HostOS.interpreter.truffleruby?
16
+ #
17
+ module Interpreter
18
+ extend Helper
19
+
20
+ class << self
21
+ # @!attribute [r] mri?
22
+ # @return [true, false] whether the interpreter is the Yukihiro
23
+ # Matsumoto's C-based (default) Ruby Interpreter
24
+ def mri?
25
+ id == :mri
26
+ end
27
+ alias cruby? mri?
28
+ alias default? mri?
29
+
30
+ # @!attribute [r] cardinal?
31
+ # @return [true, false] whether the interpreter is the Parrot based
32
+ # Cardinal interpreter
33
+ def cardinal?
34
+ id == :cardinal
35
+ end
36
+ alias parrot? cardinal?
37
+
38
+ # @!attribute [r] jruby?
39
+ # @return [true, false] whether the interpreter is the Java based JRuby
40
+ # Interpreter
41
+ def jruby?
42
+ id == :jruby
43
+ end
44
+ alias java? jruby?
45
+
46
+ # @!attribute [r] rbx?
47
+ # @return [true, false] whether the interpreter is the Rubinius
48
+ # Interpreter
49
+ def rbx?
50
+ id == :rbx
51
+ end
52
+ alias rubinius? rbx?
53
+
54
+ # @!attribute [r] ree?
55
+ # @return [true, false] whether the interpreter is the Ruby Enterprise
56
+ # Edition
57
+ def ree?
58
+ id == :ree
59
+ end
60
+ alias enterprise? ree?
61
+
62
+ # @!attribute [r] jit_enabled?
63
+ # @return [true, false] whether the interpreter currently uses a JIT
64
+ # Compiler
65
+ def jit_enabled?
66
+ jit_type != :none
67
+ end
68
+
69
+ # @!attribute [r] jit_type
70
+ # @return [:mjit, :rjit, :yjit, :java, :none] type of currently used JIT
71
+ # Compiler
72
+ def jit_type
73
+ return :mjit if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled?
74
+ return :yjit if defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?
75
+ return :rjit if defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
76
+ jruby? ? :java : :none
77
+ end
78
+
79
+ # @!visibility private
80
+ def to_s
81
+ @to_s ||=
82
+ {
83
+ cardinal: 'Cardinal',
84
+ jruby: 'JRuby',
85
+ mri: 'CRuby',
86
+ rby: 'Rubinius',
87
+ ree: 'Enterprise Ruby',
88
+ truffleruby: 'TruffleRuby'
89
+ }.compare_by_identity[
90
+ id
91
+ ] || id.to_s.upcase
92
+ end
93
+
94
+ # Path name of current Ruby executable.
95
+ # @!attribute [r] exe
96
+ # @return [String] complete path name to current Ruby executable
97
+ # @return [nil] when executable can not be detected
98
+ def exe
99
+ defined?(@exe) ? @exe : @exe = find_exe
100
+ end
101
+
102
+ # @!attribute [r] id
103
+ # @return [Symbol] interpreter identifier
104
+
105
+ # @!method is?(what)
106
+ # @param what [Symbol, String] the identifier to check
107
+ # @return [true, false] whether the interpreter is the given identifier
108
+
109
+ # @comment YARD requires this line
110
+
111
+ private
112
+
113
+ def identify
114
+ if defined?(RUBY_PLATFORM) && (RUBY_PLATFORM == 'parrot')
115
+ return :cardinal
116
+ end
117
+ return :mri unless defined?(RUBY_ENGINE)
118
+ return RUBY_ENGINE.to_sym if RUBY_ENGINE != 'ruby'
119
+ RUBY_DESCRIPTION.downcase.include?('enterprise') ? :ree : :mri
120
+ end
121
+
122
+ def find_exe
123
+ require 'rbconfig' unless defined?(RbConfig)
124
+ RbConfig.ruby
125
+ rescue LoadError
126
+ ENV['RUBY']
127
+ rescue NoMethodError
128
+ File.join(
129
+ RbConfig::CONFIG['bindir'],
130
+ RbConfig::CONFIG['ruby_install_name'] + RbConfig::CONFIG['EXEEXT']
131
+ )
132
+ end
133
+ end
134
+
135
+ @id = identify
136
+ end
137
+ end
@@ -10,181 +10,145 @@ module HostOS
10
10
  #
11
11
  # @note You need to require `host-os/support` explicitly.
12
12
  module Support
13
- # @attribute [r] dev_null
14
- # @return [String] name of or path to the null device
15
- # @note This attribute is only available on Windows, OS2 and Unix systems.
16
-
17
- # @attribute [r] open_command
18
- # @return [String] name of the open command
19
- # @note This attribute is only available on Windows, MacOS and Linux systems.
20
-
21
- # @attribute [r] rss_bytes
22
- # @return [Integer] number of bytes used by the current process
23
- # @note This attribute is only available on Windows and Unix or when using
24
- # JRuby
25
-
26
- # @attribute [r] suggested_thread_count
27
- # @return [Integer] suggested number of threads to use
28
-
29
- # @attribute [r] temp_dir
30
- # @return [String] name of the temporary directory
31
-
32
- # @!method app_config_path(app_name)
33
- # @param app_name [String] name of the application
34
- # @return [String] absolute name of the directory
35
- # Determines the name of the directory where application specific data
36
- # should be stored.
37
- # @note This method is only available on Windows and Posix-compatible
38
- # systems.
39
-
40
- # @comment YARD requires this emoty line :/
41
- module Windows
13
+ if defined?(File::NULL)
14
+ # @attribute [r] dev_null
15
+ # @return [String] name of or path to the null device
16
+ # @note This attribute is only available on
17
+ # Windows, OS2 and Unix systems for older Ruby versions.
18
+ def dev_null
19
+ File::NULL
20
+ end
21
+ elsif HostOS.windows?
42
22
  def dev_null
43
23
  'NUL'
44
24
  end
25
+ elsif HostOS.os2?
26
+ def dev_null
27
+ 'nul'
28
+ end
29
+ elsif HostOS.unix?
30
+ def dev_null
31
+ '/dev/null'
32
+ end
33
+ end
45
34
 
35
+ if HostOS.windows?
36
+ # @attribute [r] open_command
37
+ # @return [String] name of the open command
38
+ # @note This attribute is only available on Windows, MacOS and Linux systems.
46
39
  def open_command
47
40
  'start'
48
41
  end
42
+ elsif HostOS.macosx?
43
+ def open_command
44
+ 'open'
45
+ end
46
+ elsif HostOS.linux?
47
+ def open_command
48
+ 'xdg-open'
49
+ end
50
+ end
49
51
 
52
+ if HostOS.windows?
53
+ # @attribute [r] rss_bytes
54
+ # @return [Integer] number of bytes used by the current process
55
+ # @note This attribute is only available on Windows and Unix or when using
56
+ # JRuby
50
57
  def rss_bytes
51
58
  `tasklist /FI "PID eq #{Process.pid}" /FO CSV`.split(',"')[-1].tr(
52
59
  ',.',
53
60
  '__'
54
61
  ).to_i * 1024
55
62
  end
56
-
57
- private
58
-
59
- def _app_config_path
60
- ENV['LOCALAPPDATA'] ||
61
- "#{ENV['USERPROFILE']}/Local Settings/Application Data"
62
- end
63
- end
64
-
65
- module Unix
66
- def dev_null
67
- '/dev/null'
63
+ elsif Interpreter.jruby?
64
+ def rss_bytes
65
+ require 'java' unless defined?(java)
66
+ bean = java.lang.management.ManagementFactory.getMemoryMXBean
67
+ bean.heap_memory_usage.used + bean.non_heap_memory_usage.used
68
68
  end
69
-
69
+ elsif HostOS.unix?
70
70
  def rss_bytes
71
71
  `ps -o rss= -p #{Process.pid}`.to_i * 1024
72
72
  end
73
-
74
- private
75
-
76
- def _app_config_path
77
- (ENV['XDG_CONFIG_HOME'] || '~/.config')
78
- end
79
- end
80
-
81
- module OS2
82
- def dev_null
83
- 'nul'
84
- end
85
73
  end
86
74
 
87
- module MacOS
88
- def open_command
89
- 'open'
75
+ if HostOS.windows?
76
+ # @param app_name [String] name of the application
77
+ # @return [String] absolute name of the directory
78
+ # Determines the name of the directory where application specific data
79
+ # should be stored.
80
+ # @note This method is only available on Windows and Posix-compatible
81
+ # systems.
82
+ def app_config_path(app_name)
83
+ File.expand_path(
84
+ app_name,
85
+ ENV['LOCALAPPDATA'] ||
86
+ "#{ENV['USERPROFILE']}/Local Settings/Application Data"
87
+ )
90
88
  end
91
-
92
- private
93
-
94
- def _app_config_path
95
- '~/Library/Application Support'
89
+ elsif HostOS.macosx?
90
+ def app_config_path(app_name)
91
+ File.expand_path(app_name, '~/Library/Application Support')
96
92
  end
97
- end
98
-
99
- module Linux
100
- def open_command
101
- 'xdg-open'
93
+ elsif HostOS.unix?
94
+ def app_config_path(app_name)
95
+ File.expand_path(app_name, ENV['XDG_CONFIG_HOME'] || '~/.config')
102
96
  end
103
97
  end
104
98
 
105
- module AppConfigPath
106
- def app_config_path(app_name)
107
- File.expand_path(app_name, _app_config_path)
108
- end
99
+ # @attribute [r] suggested_thread_count
100
+ # @return [Integer] suggested number of threads to use
101
+ def suggested_thread_count
102
+ @suggested_thread_count ||= find_suggested_thread_count
109
103
  end
110
104
 
111
- module JRuby
112
- def rss_bytes
113
- require 'java' unless defined?(java)
114
- bean = java.lang.management.ManagementFactory.getMemoryMXBean
115
- bean.heap_memory_usage.used + bean.non_heap_memory_usage.used
116
- end
105
+ # @attribute [r] temp_dir
106
+ # @return [String] name of the temporary directory
107
+ def temp_dir
108
+ @temp_dir ||= find_temp_dir
117
109
  end
118
110
 
119
- module Tools
120
- def suggested_thread_count
121
- @suggested_thread_count ||= find_suggested_thread_count
122
- end
111
+ private
123
112
 
124
- def temp_dir
125
- @temp_dir ||= find_temp_dir
113
+ def find_suggested_thread_count
114
+ count = ENV['TC'].to_i
115
+ return count if count > 0
116
+ begin
117
+ require('etc') unless defined?(Etc)
118
+ Etc.nprocessors
119
+ rescue LoadError
120
+ 4
126
121
  end
122
+ end
127
123
 
128
- private
129
-
130
- def find_suggested_thread_count
131
- count = ENV['TC'].to_i
132
- return count if count > 0
124
+ def find_temp_dir
125
+ return Dir.tmpdir if defined?(Dir.tmpdir)
126
+ ret = as_dir('TMPDIR') || as_dir('TMP') || as_dir('TEMP')
127
+ return ret if ret
128
+ ret =
133
129
  begin
134
130
  require('etc') unless defined?(Etc)
135
- Etc.nprocessors
131
+ Etc.systmpdir
136
132
  rescue LoadError
137
- 4
133
+ "#{ENV['LOCALAPPDATA']}/Temp"
138
134
  end
139
- end
140
-
141
- def find_temp_dir
142
- return Dir.tmpdir if defined?(Dir.tmpdir)
143
- ret = as_dir('TMPDIR') || as_dir('TMP') || as_dir('TEMP')
144
- return ret if ret
145
- ret =
146
- begin
147
- require('etc') unless defined?(Etc)
148
- Etc.systmpdir
149
- rescue LoadError
150
- "#{ENV['LOCALAPPDATA']}/Temp"
151
- end
152
- as_dir('system temp', ret) || as_dir('/tmp', '/tmp') || as_dir('.', '.')
153
- end
135
+ as_dir('system temp', ret) || as_dir('/tmp', '/tmp') || as_dir('.', '.')
136
+ end
154
137
 
155
- def as_dir(name, dirname = nil)
156
- dirname ||= ENV[name]
157
- return if dirname.nil? || dirname.empty?
158
- dirname = File.expand_path(dirname)
159
- stat = File.stat(dirname)
160
- stat.directory? or warn("#{name} is not a valid directory - #{dirname}")
161
- stat.writable? or warn("#{name} is not writable - #{dirname}")
162
- (stat.world_writable? && !stat.sticky?) and
163
- warn("#{name} is world-writable - #{dirname}")
164
- dirname
165
- rescue SystemCallError
166
- nil
167
- end
138
+ def as_dir(name, dirname = nil)
139
+ dirname ||= ENV[name]
140
+ return if dirname.nil? || dirname.empty?
141
+ dirname = File.expand_path(dirname)
142
+ stat = File.stat(dirname)
143
+ stat.directory? or warn("#{name} is not a valid directory - #{dirname}")
144
+ stat.writable? or warn("#{name} is not writable - #{dirname}")
145
+ (stat.world_writable? && !stat.sticky?) and
146
+ warn("#{name} is world-writable - #{dirname}")
147
+ dirname
148
+ rescue SystemCallError
149
+ nil
168
150
  end
169
151
  end
170
152
 
171
- extend Support::Windows if windows?
172
- extend Support::Unix if unix?
173
- extend Support::MacOS if macosx?
174
- extend Support::OS2 if os2?
175
- extend Support::Linux if linux?
176
- extend Support::AppConfigPath if windows? || unix?
177
- extend Support::JRuby if Interpreter.jruby?
178
- extend Support::Tools
179
-
180
- module Support
181
- private_constant :Windows
182
- private_constant :Unix
183
- private_constant :OS2
184
- private_constant :MacOS
185
- private_constant :Linux
186
- private_constant :AppConfigPath
187
- private_constant :JRuby
188
- private_constant :Tools
189
- end
153
+ extend Support
190
154
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module HostOS
4
- # @return [String] the version number of the gem
5
- VERSION = '0.2.0'
4
+ # Version number of the gem.
5
+ VERSION = '0.2.2'
6
6
  end
data/lib/host-os.rb CHANGED
@@ -6,276 +6,65 @@
6
6
  # It helps you write environment-specific code in a clean way.
7
7
  #
8
8
  module HostOS
9
- module Helper
10
- def is?(what)
11
- id == what ||
12
- (id == (defined?(what.to_sym) ? what.to_sym : what.to_s.to_sym))
13
- end
14
-
15
- private
16
-
17
- def respond_to_missing?(name, _include_all = false)
18
- (name[-1] == '?') || super
19
- end
20
-
21
- def method_missing(name, *args)
22
- return super if name[-1] != '?'
23
- return is?(name[0..-2]) if args.empty?
24
- raise(
25
- ArgumentError,
26
- "wrong number of arguments (given #{args.size}, expected 0)"
27
- )
28
- end
29
- end
30
- private_constant :Helper
31
-
32
- #
33
- # This module allows to identify the current environment by checking the `ENV`
34
- # for the following variables in order:
35
- #
36
- # * RAILS_ENV
37
- # * RACK_ENV
38
- # * ENVIRONMENT
39
- # * ENV
40
- #
41
- # You can check for any boolean attribute:
42
- #
43
- # @example Query if the environment is configured as "staging"
44
- # HostOS.env.staging?
45
- # @example Test if the environment is configured as "production"
46
- # HostOS.env.is? :production
47
- #
48
- # @note When no environment is configured 'prod
49
- #
50
- module Env
51
- extend Helper
52
-
53
- class << self
54
- # @attribute [r] production?
55
- # @return [true, false] whether the environment is configured as "production"
56
- # @note This will return true if the environment is not configured.
57
-
58
- # @attribute [r] test?
59
- # @return [true, false] whether the environment is configured as "test"
60
-
61
- # @attribute [r] development?
62
- # @return [true, false] whether the environment is configured as
63
- # "development"
64
-
65
- # @attribute [r] id
66
- # @return [Symbol] environment identifier
67
- def id
68
- ID
69
- end
70
-
71
- # @!method is?(what)
72
- # @param what [Symbol, String] the identifier to check
73
- # @return [true, false] whether the environment is the given identifier
74
-
75
- # @comment YARD requires this line
76
-
77
- private
78
-
79
- def identify
80
- found =
81
- ENV['RAILS_ENV'] || ENV['RACK_ENV'] || ENV['ENVIRONMENT'] ||
82
- ENV['ENV']
83
- return :production if found.nil? || found.empty?
84
- found.downcase.tr(' -', '__').to_sym
85
- end
86
- end
87
-
88
- # @return [Symbol] environment identifier
89
- ID = identify
90
- end
91
-
92
- #
93
- # This module allows to identify the used Ruby interpreter.
94
- #
95
- # Besides here documented boolean attributes you can also check for any other
96
- # boolean attribute or interpreter name:
97
- #
98
- # @example Query for the Opal interpreter
99
- # HostOS.interpreter.opal?
100
- # @example Query for TruffleRuby
101
- # HostOS.interpreter.truffleruby?
102
- #
103
- module Interpreter
104
- extend Helper
105
-
106
- class << self
107
- # @attribute [r] id
108
- # @return [Symbol] interpreter identifier
109
- def id
110
- ID
111
- end
112
-
113
- # @attribute [r] mri?
114
- # @return [true, false] whether the interpreter is the Yukihiro
115
- # Matsumoto's C-based (default) Ruby Interpreter
116
- def mri?
117
- ID == :mri
118
- end
119
- alias cruby? mri?
120
- alias default? mri?
121
-
122
- # @attribute [r] cardinal?
123
- # @return [true, false] whether the interpreter is the Parrot based
124
- # Cardinal interpreter
125
- def cardinal?
126
- ID == :cardinal
127
- end
128
- alias parrot? cardinal?
129
-
130
- # @attribute [r] jruby?
131
- # @return [true, false] whether the interpreter is the Java based JRuby
132
- # Interpreter
133
- def jruby?
134
- ID == :jruby
135
- end
136
- alias java? jruby?
137
-
138
- # @attribute [r] rbx?
139
- # @return [true, false] whether the interpreter is the Rubinius
140
- # Interpreter
141
- def rbx?
142
- ID == :rbx
143
- end
144
- alias rubinius? rbx?
145
-
146
- # @attribute [r] ree?
147
- # @return [true, false] whether the interpreter is the Ruby Enterprise
148
- # Edition
149
- def ree?
150
- ID == :ree
151
- end
152
- alias enterprise? ree?
153
-
154
- # @attribute [r] jit_enabled?
155
- # @return [true, false] whether the interpreter currently uses a JIT
156
- # Compiler
157
- def jit_enabled?
158
- jit_type != :none
159
- end
160
-
161
- # @attribute [r] jit_type
162
- # @return [:mjit, :rjit, :yjit, :java, :none] type of currently used JIT
163
- # Compiler
164
- def jit_type
165
- return :mjit if defined?(RubyVM::MJIT) && RubyVM::MJIT.enabled?
166
- return :yjit if defined?(RubyVM::YJIT) && RubyVM::YJIT.enabled?
167
- return :rjit if defined?(RubyVM::RJIT) && RubyVM::RJIT.enabled?
168
- jruby? ? :java : :none
169
- end
170
-
171
- # @!visibility private
172
- def to_s
173
- NAMES[ID] || ID.to_s.upcase
174
- end
175
-
176
- # @!method is?(what)
177
- # @param what [Symbol, String] the identifier to check
178
- # @return [true, false] whether the interpreter is the given identifier
179
-
180
- # @comment YARD requires this line
181
-
182
- private
183
-
184
- def identify
185
- if defined?(RUBY_PLATFORM) && (RUBY_PLATFORM == 'parrot')
186
- return :cardinal
187
- end
188
- return :mri unless defined?(RUBY_ENGINE)
189
- return RUBY_ENGINE.to_sym if RUBY_ENGINE != 'ruby'
190
- RUBY_DESCRIPTION.downcase.include?('enterprise') ? :ree : :mri
191
- end
192
- end
193
-
194
- # @return [Symbol] interpreter identifier
195
- ID = identify
196
-
197
- NAMES = {
198
- cardinal: 'Cardinal',
199
- jruby: 'JRuby',
200
- mri: 'CRuby',
201
- rby: 'Rubinius',
202
- ree: 'Enterprise Ruby',
203
- truffleruby: 'TruffleRuby'
204
- }.compare_by_identity.freeze
205
-
206
- private_constant :NAMES
207
- end
208
-
209
- extend Helper
210
-
211
9
  class << self
212
- # @attribute [r] id
213
- # @return [Symbol] OS identifier
214
- def id
215
- ID
216
- end
217
-
218
- # @attribute [r] type
219
10
  # @return [:unix, :windows, :vms, :os2, :unknown] OS type
220
- def type
221
- TYPE
222
- end
11
+ attr_reader :type
223
12
 
224
- # @attribute [r] interpreter
13
+ # @!attribute [r] interpreter
225
14
  # @return [Interpreter] interpreter information
226
15
  def interpreter
227
16
  Interpreter
228
17
  end
229
18
 
230
- # @attribute [r] env
19
+ # @!attribute [r] env
231
20
  # @return [Env] environment information
232
21
  def env
233
22
  Env
234
23
  end
235
24
 
236
- # @attribute [r] unix?
25
+ # @!attribute [r] unix?
237
26
  # @return [true, false] whether the host OS is a Unix OS
238
27
  def unix?
239
- TYPE == :unix
28
+ @type == :unix
240
29
  end
241
30
 
242
- # @attribute [r] windows?
31
+ # @!attribute [r] windows?
243
32
  # @return [true, false] whether the host OS is a Windows OS
244
33
  def windows?
245
- TYPE == :windows
34
+ @type == :windows
246
35
  end
247
36
 
248
- # @attribute [r] vms?
37
+ # @!attribute [r] vms?
249
38
  # @return [true, false] whether the host OS is VMS
250
39
  def vms?
251
- TYPE == :vms
40
+ @type == :vms
252
41
  end
253
42
 
254
- # @attribute [r] os2?
43
+ # @!attribute [r] os2?
255
44
  # @return [true, false] whether the host OS is OS/2
256
45
  def os2?
257
- TYPE == :os2
46
+ @type == :os2
258
47
  end
259
48
 
260
- # @attribute [r] macosx?
49
+ # @!attribute [r] macosx?
261
50
  # @return [true, false] whether the host OS is identified as MacOS
262
51
  def macosx?
263
- ID == :macosx
52
+ @id == :macosx
264
53
  end
265
54
 
266
- # @attribute [r] linux?
55
+ # @!attribute [r] linux?
267
56
  # @return [true, false] whether the host OS is identified as Linux derivate
268
57
  def linux?
269
- ID == :linux
58
+ @id == :linux
270
59
  end
271
60
 
272
- # @attribute [r] cygwin?
61
+ # @!attribute [r] cygwin?
273
62
  # @return [true, false] whether the host OS is Windows/Cygwin
274
63
  def cygwin?
275
- ID == :cygwin
64
+ @id == :cygwin
276
65
  end
277
66
 
278
- # @attribute [r] posix?
67
+ # @!attribute [r] posix?
279
68
  # @return [true, false] whether the host OS is Posix compatible
280
69
  # This attribute is `true` when Posix compatible commands like `fork` are
281
70
  # available.
@@ -286,14 +75,41 @@ module HostOS
286
75
  # @param what [Symbol, String] the identifier to check
287
76
  # @return [true, false] whether the host OS is the given identifier or type
288
77
  def is?(what)
289
- return true if (ID == what) || (TYPE == what)
290
- what = defined?(what.to_sym) ? what.to_sym : what.to_s.to_sym
291
- (ID == what) || (TYPE == what)
78
+ return (@id == what) || (@type == what) if what.is_a?(Symbol)
79
+ if defined?(what.to_sym)
80
+ what = what.to_sym
81
+ return (@id == what) || (@type == what)
82
+ end
83
+ if defined?(what.to_s)
84
+ what = what.to_s.to_sym
85
+ return (@id == what) || (@type == what)
86
+ end
87
+ false
292
88
  end
293
89
 
90
+ # @!attribute [r] id
91
+ # @return [Symbol] OS identifier
92
+
294
93
  # @!visibility private
295
94
  def to_s
296
- NAMES[ID] || ID.to_s.upcase
95
+ @to_s ||=
96
+ {
97
+ bccwin: 'BCCWin',
98
+ cygwin: 'Cygwin',
99
+ dragonfly: 'Dragonly',
100
+ freebsd: 'FreeBSD',
101
+ linux: 'Linux',
102
+ macosx: 'MacOSX',
103
+ mingw: 'MinGW',
104
+ mswin: 'MSWin',
105
+ netbsd: 'NetBSD',
106
+ openbsd: 'OpenBSD',
107
+ sunos: 'SunOS',
108
+ wince: 'WinCE',
109
+ windows: 'Windows'
110
+ }.compare_by_identity[
111
+ @id
112
+ ] || @id.to_s.upcase
297
113
  end
298
114
 
299
115
  private
@@ -331,20 +147,92 @@ module HostOS
331
147
  end
332
148
  end
333
149
 
334
- ID, TYPE = identify
335
- NAMES = {
336
- bccwin: 'BCCWin',
337
- cygwin: 'Cygwin',
338
- dragonfly: 'Dragonly',
339
- freebsd: 'FreeBSD',
340
- linux: 'Linux',
341
- macosx: 'MacOSX',
342
- mingw: 'MinGW',
343
- mswin: 'MSWin',
344
- netbsd: 'NetBSD',
345
- openbsd: 'OpenBSD',
346
- sunos: 'SunOS',
347
- wince: 'WinCE',
348
- windows: 'Windows'
349
- }.compare_by_identity.freeze
150
+ module Helper
151
+ attr_reader :id
152
+
153
+ def is?(what)
154
+ return id == what if what.is_a?(Symbol)
155
+ return id == what.to_sym if defined?(what.to_sym)
156
+ return id == what.to_s.to_sym if defined?(what.to_s)
157
+ false
158
+ end
159
+
160
+ private
161
+
162
+ def respond_to_missing?(name, _include_all = false)
163
+ (name[-1] == '?') || super
164
+ end
165
+
166
+ def method_missing(name, *args)
167
+ return super if name[-1] != '?'
168
+ return is?(name[0..-2]) if args.empty?
169
+ raise(
170
+ ArgumentError,
171
+ "wrong number of arguments (given #{args.size}, expected 0)"
172
+ )
173
+ end
174
+ end
175
+ private_constant :Helper
176
+
177
+ #
178
+ # This module allows to identify the current environment by checking the `ENV`
179
+ # for the following variables in order:
180
+ #
181
+ # * RAILS_ENV
182
+ # * RACK_ENV
183
+ # * ENVIRONMENT
184
+ # * ENV
185
+ #
186
+ # You can check for any boolean attribute:
187
+ #
188
+ # @example Query if the environment is configured as "staging"
189
+ # HostOS.env.staging?
190
+ # @example Test if the environment is configured as "production"
191
+ # HostOS.env.is? :production
192
+ #
193
+ # @note When no environment is configured `production` is assumed.
194
+ #
195
+ module Env
196
+ extend Helper
197
+
198
+ class << self
199
+ # @!attribute [r] production?
200
+ # @return [true, false] whether the environment is configured as "production"
201
+ # @note This will return true if the environment is not configured.
202
+
203
+ # @!attribute [r] test?
204
+ # @return [true, false] whether the environment is configured as "test"
205
+
206
+ # @!attribute [r] development?
207
+ # @return [true, false] whether the environment is configured as
208
+ # "development"
209
+
210
+ # @!attribute [r] id
211
+ # @return [Symbol] environment identifier
212
+
213
+ # @!method is?(what)
214
+ # @param what [Symbol, String] the identifier to check
215
+ # @return [true, false] whether the environment is the given identifier
216
+
217
+ # @comment YARD requires this line
218
+
219
+ private
220
+
221
+ def identify
222
+ found =
223
+ ENV['RAILS_ENV'] || ENV['RACK_ENV'] || ENV['ENVIRONMENT'] ||
224
+ ENV['ENV']
225
+ return :production if found.nil? || found.empty?
226
+ found.downcase.gsub(/\W/, '_').to_sym
227
+ end
228
+ end
229
+
230
+ @id = identify
231
+ end
232
+
233
+ autoload :Interpreter, "#{__dir__}/host-os/interpreter.rb"
234
+
235
+ extend Helper
236
+
237
+ @id, @type = identify
350
238
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: host-os
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Blumtritt
@@ -24,6 +24,7 @@ files:
24
24
  - README.md
25
25
  - examples/current.rb
26
26
  - lib/host-os.rb
27
+ - lib/host-os/interpreter.rb
27
28
  - lib/host-os/support.rb
28
29
  - lib/host-os/version.rb
29
30
  - lib/host_os.rb
@@ -31,10 +32,10 @@ homepage: https://github.com/mblumtritt/host-os
31
32
  licenses:
32
33
  - BSD-3-Clause
33
34
  metadata:
35
+ rubygems_mfa_required: 'true'
34
36
  source_code_uri: https://github.com/mblumtritt/host-os
35
37
  bug_tracker_uri: https://github.com/mblumtritt/host-os/issues
36
- documentation_uri: https://rubydoc.info/gems/host-os
37
- rubygems_mfa_required: 'true'
38
+ documentation_uri: https://rubydoc.info/gems/host-os/0.2.2/HostOS
38
39
  rdoc_options: []
39
40
  require_paths:
40
41
  - lib
@@ -49,7 +50,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
49
50
  - !ruby/object:Gem::Version
50
51
  version: '0'
51
52
  requirements: []
52
- rubygems_version: 3.6.9
53
+ rubygems_version: 3.7.0
53
54
  specification_version: 4
54
55
  summary: This module offers details on the host OS, the present Ruby interpreter and
55
56
  environment.