Class: OS
Instance Attribute Summary collapse
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#original_env ⇒ Object
readonly
Returns the value of attribute original_env.
Class Method Summary collapse
-
.parse(original_env = ENV) ⇒ OS
Takes ENV or another hash-like object and convert it into OS instance.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#[](key) ⇒ OS
Get value for given key or nil.
-
#initialize(env, original_env = env) ⇒ OS
constructor
A new instance of OS.
- #inspect ⇒ Object
- #keys ⇒ Object
-
#root? ⇒ Boolean
TODO.
Constructor Details
#initialize(env, original_env = env) ⇒ OS
Returns a new instance of OS.
21 22 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 |
# File 'lib/rubyexts/os.rb', line 21 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(":").uniq.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
#method_missing(name, *args, &block) ⇒ Object (private)
86 87 88 |
# File 'lib/rubyexts/os.rb', line 86 def method_missing(name, *args, &block) super(name, *args, &block) unless args.empty? && block.nil? end |
Instance Attribute Details
#env ⇒ Object (readonly)
Returns the value of attribute env.
20 21 22 |
# File 'lib/rubyexts/os.rb', line 20 def env @env end |
#original_env ⇒ Object (readonly)
Returns the value of attribute original_env.
20 21 22 |
# File 'lib/rubyexts/os.rb', line 20 def original_env @original_env end |
Class Method Details
.parse(original_env = ENV) ⇒ OS
Takes ENV or another hash-like object and convert it into OS instance
13 14 15 16 17 18 |
# File 'lib/rubyexts/os.rb', line 13 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 |
Instance Method Details
#==(other) ⇒ Object
63 64 65 66 67 |
# File 'lib/rubyexts/os.rb', line 63 def ==(other) same_keys = self.keys == other.keys same_values = self.keys.all? { |key| self[key] == other[key] } same_keys && same_values end |
#[](key) ⇒ OS
Get value for given key or nil
59 60 61 |
# File 'lib/rubyexts/os.rb', line 59 def [](key) self.send(key) if self.keys.include?(key.to_sym) end |
#inspect ⇒ Object
73 74 75 76 77 78 |
# File 'lib/rubyexts/os.rb', line 73 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 ⇒ Object
69 70 71 |
# File 'lib/rubyexts/os.rb', line 69 def keys @env.keys.sort end |
#root? ⇒ Boolean
TODO
81 82 83 |
# File 'lib/rubyexts/os.rb', line 81 def root? self.uid.eql?(0) end |