Class: OS

  • BlankSlate
    • OS

Attributes

Instance Attributes

env [RW] public

Returns the value of attribute env.

original_env [RW] public

Returns the value of attribute original_env.

Constructor Summary

public initialize(env, original_env = env)
[View source]


23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/rango/ext/os.rb', line 23

def initialize(env, original_env = env)
  @env, @original_env = env, original_env
  self.keys.each do |key|
    if key.match(/(path|lib)$/)
      # OS.path
      # => ["/bin", "/usr/bin", "/sbin", "/usr/sbin"]
      define_singleton_method(key) do
        @env[key].split(":").sort
      end
    elsif @env[key].match(/^\d+$/)
      define_singleton_method(key) { @env[key].to_i }
    elsif @env[key].empty?
      define_singleton_method(key) { nil }
    else
      # OS.home
      # => "/Users/botanicus"
      define_singleton_method(key) do
        case value = @env[key]
        when /^\d+$/      then value.to_i
        when /^\d+\.\d+$/ then value.to_f
        when /^true$/i    then true
        when /^false$/i   then false
        when /^$/         then nil
        else              value
        end
      end
    end
  end
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

private method_missing(name, *args, &block)
[View source]


88
89
90
# File 'lib/rango/ext/os.rb', line 88

def method_missing(name, *args, &block)
  super(name, *args, &block) unless args.empty? && block.nil?
end

Public Visibility

Public Class Method Summary

parse(original_env = ENV)

Takes ENV or another hash-like object and convert it into OS instance.

Returns: OS

Public Instance Method Summary

#==(other)
#[](key)

Get value for given key or nil.

Returns: OS

#inspect
#keys
#root?

TODO.

Public Class Method Details

parse

public OS parse(original_env = ENV)

Takes ENV or another hash-like object and convert it into OS instance

Meta Tags

Parameters:

Returns:

[OS]

Instance of OS

Author:

Botanicus

Since:

0.0.3

[View source]


15
16
17
18
19
20
# File 'lib/rango/ext/os.rb', line 15

def self.parse(original_env = ENV)
  input  = original_env.select { |key, value| key.match(/^[a-zA-Z][a-zA-Z_]*$/) }
  result = Hash.new
  input.each { |key, value| result[key.downcase.to_sym] = value }
  self.new(result, original_env)
end

Public Instance Method Details

==

public ==(other)
[View source]


65
66
67
68
69
# File 'lib/rango/ext/os.rb', line 65

def ==(other)
  same_keys = self.keys == other.keys
  same_values = self.keys.all? { |key| self[key] == other[key] }
  same_keys && same_values
end

[]

public OS [](key)

Get value for given key or nil

Meta Tags

Parameters:

Returns:

[OS]

Instance of OS

Author:

Botanicus

Since:

0.0.3

[View source]


61
62
63
# File 'lib/rango/ext/os.rb', line 61

def [](key)
  self.send(key) if self.keys.include?(key.to_sym)
end

inspect

public inspect
[View source]


75
76
77
78
79
80
# File 'lib/rango/ext/os.rb', line 75

def inspect
  string = "#<OS keys=[%s]>"
  format = lambda { |key| "#{key}=#{self.send(key).inspect}" }
  inner  = self.keys.sort.map(&format).join(", ")
  string % inner
end

keys

public keys
[View source]


71
72
73
# File 'lib/rango/ext/os.rb', line 71

def keys
  @env.keys.sort
end

root?

public root?

TODO

[View source]


83
84
85
# File 'lib/rango/ext/os.rb', line 83

def root?
  self.uid.eql?(0)
end