Class: RightConf::Platform
- Inherits:
-
Object
- Object
- RightConf::Platform
- Includes:
- Singleton
- Defined in:
- lib/rconf/platforms/linux.rb,
lib/rconf/platform.rb,
lib/rconf/platforms/darwin.rb,
lib/rconf/platforms/windows.rb
Overview
Linux specific implementation
Constant Summary collapse
- FEDORA_REL =
'/etc/fedora-release'
- FEDORA_SIG =
/Fedora release ([0-9]+) \(.*\)/
Instance Attribute Summary collapse
-
#flavor ⇒ Object
readonly
Returns the value of attribute flavor.
-
#release ⇒ Object
readonly
Returns the value of attribute release.
Instance Method Summary collapse
-
#darwin? ⇒ Boolean
Is current platform darwin?.
-
#dispatch(*args, &blk) ⇒ Object
Call platform specific implementation of method whose symbol is returned by the passed in block.
-
#family ⇒ Object
Generic platform family.
-
#init ⇒ Object
Initialize flavor and release.
-
#linux? ⇒ Boolean
Is current platform linux?.
-
#windows? ⇒ Boolean
Is current platform windows?.
Methods included from Singleton
Instance Attribute Details
#flavor ⇒ Object (readonly)
Returns the value of attribute flavor.
20 21 22 |
# File 'lib/rconf/platforms/linux.rb', line 20 def flavor @flavor end |
#release ⇒ Object (readonly)
Returns the value of attribute release.
20 21 22 |
# File 'lib/rconf/platforms/linux.rb', line 20 def release @release end |
Instance Method Details
#darwin? ⇒ Boolean
Is current platform darwin?
Return
- true
-
If current platform is darwin
- false
-
Otherwise
47 48 49 |
# File 'lib/rconf/platform.rb', line 47 def darwin? return family == :darwin end |
#dispatch(*args, &blk) ⇒ Object
Call platform specific implementation of method whose symbol is returned by the passed in block. Arguments are passed through. e.g.
Platform.dispatch(2) { :echo }
will result in ‘echo_linux(2)’ being executed in self if running on linux, ‘echo_windows(2)’ if running on Windows and ‘echo_darwin(2)’ if on Mac OS X. Note that the method is run in the instance of the caller.
Parameters
- args
-
Pass-through arguments
Block
Given block should not take any argument and return a symbol for the method that should be called
Return
- res(ObjecT)
-
Result returned by platform specific implementation
78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/rconf/platform.rb', line 78 def dispatch(*args, &blk) raise "Platform.dispatch requires a block" unless blk binding = blk.binding.eval('self') meth = blk.call target = dispatch_candidates(meth).detect do |candidate| binding.respond_to?(candidate) end raise "No platform dispatch target found in #{binding.class} for " + "'#{meth.inspect}', tried " + dispatch_candidates(meth).join(', ') unless target binding.__send__(target, *args) end |
#family ⇒ Object
Generic platform family
Return
- family(Symbol)
-
One of :linux, :windows or :darwin
25 26 27 28 29 30 31 |
# File 'lib/rconf/platform.rb', line 25 def family @family ||= case RbConfig::CONFIG['host_os'] when /mswin|win32|dos|mingw|cygwin/i then :windows when /darwin/i then :darwin when /linux/i then :linux end end |
#init ⇒ Object
Initialize flavor and release
23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/rconf/platforms/linux.rb', line 23 def init system('lsb_release --help > /dev/null 2>&1') if $?.success? # Use the lsb_release utility if it's available @flavor = `lsb_release -is`.strip.downcase @release = `lsb_release -rs`.strip elsif File.exist?(FEDORA_REL) && (match = FEDORA_SIG.match(File.read(FEDORA_REL))) # Parse the fedora-release file if it exists @distro = 'fedora' @release = match[1] else @distro = @release = 'unknown' end end |
#linux? ⇒ Boolean
Is current platform linux?
Return
- true
-
If current platform is linux
- false
-
Otherwise
38 39 40 |
# File 'lib/rconf/platform.rb', line 38 def linux? return family == :linux end |
#windows? ⇒ Boolean
Is current platform windows?
Return
- true
-
If current platform is Windows
- false
-
Otherwise
55 56 57 |
# File 'lib/rconf/platform.rb', line 55 def windows? return family == :windows end |