Module: Hirb::Util

### Home Classes Methods

### In Files

- lib/hirb/util.rb

### Methods

- #any_const_get
- #camelize
- #capture_stdout
- #choose_from_array
- #command_exists?
- #detect_terminal_size
- #find_home
- #recursive_hash_merge

### Files

- LICENSE.txt
- README.rdoc

### Class/Module Index

Quicksearch

- Hirb
- Hirb::Console
- Hirb::DynamicView
- Hirb::Formatter
- Hirb::Helpers
- Hirb::Helpers::AutoTable
- Hirb::Helpers::ObjectTable
- Hirb::Helpers::ParentChildTree
- Hirb::Helpers::Table
- Hirb::Helpers::Table::Filters
- Hirb::Helpers::Table::Resizer
- Hirb::Helpers::Table::TooManyFieldsForWidthError
- Hirb::Helpers::Tree
- Hirb::Helpers::Tree::Node
- Hirb::Helpers::Tree::Node::MissingLevelError
- Hirb::Helpers::Tree::Node::MissingValueError
- Hirb::Helpers::Tree::ParentlessNodeError
- Hirb::Helpers::VerticalTable
- Hirb::Menu
- Hirb::Menu::Error
- Hirb::ObjectMethods
- Hirb::Pager
- Hirb::String
- Hirb::Util
- Hirb::View
- Hirb::Views

No matching classes.

# Hirb::Util

Group of handy utility functions used throughout Hirb.

### Public Instance Methods

any_const_get(name) click to toggle source

Returns a constant like Module#const_get no matter what namespace it’s nested in. Returns nil if the constant is not found.

    # File lib/hirb/util.rb, line 7
    def any_const_get(name)
      return name if name.is_a?(Module)
      begin
        klass = Object
        name.split('::').each {|e|
          klass = klass.const_get(e)
        }
        klass
      rescue
         nil
      end
    end

camelize(string) click to toggle source

From Rails ActiveSupport, converting undescored lowercase to camel uppercase.

    # File lib/hirb/util.rb, line 26
    def camelize(string)
      string.to_s.gsub(/\/(.?)/) { "::#{$1.upcase}" }.gsub(/(?:^|_)(.)/) { $1.upcase }
    end

capture_stdout(&block) click to toggle source

Captures STDOUT of anything run in its block and returns it as string.

    # File lib/hirb/util.rb, line 74
    def capture_stdout(&block)
      original_stdout = $stdout
      $stdout = fake = StringIO.new
      begin
        yield
      ensure
        $stdout = original_stdout
      end
      fake.string
    end

choose_from_array(array, input, options={}) click to toggle source

Used by Hirb::Menu to select items from an array. Array counting starts at 1. Ranges of numbers are specified with a ‘-’ or ‘..’. Multiple ranges can be comma delimited. Anything that isn’t a valid number is ignored. All elements can be returned with a ‘*’. Examples:

    1-3,5-6 -> [1,2,3,5,6]
    *   -> all elements in array
    ''  -> []

    # File lib/hirb/util.rb, line 36
    def choose_from_array(array, input, options={})
      options = {:splitter=>","}.merge(options)
      return array if input.strip == '*'
      result = []
      input.split(options[:splitter]).each do |e|
        if e =~ /-|\.\./
          min,max = e.split(/-|\.\./)
          slice_min = min.to_i - 1
          result.push(*array.slice(slice_min, max.to_i - min.to_i + 1))
        elsif e =~ /\s*(\d+)\s*/
          index = $1.to_i - 1
          next if index < 0
          result.push(array[index]) if array[index]
        end
      end
      result
    end

command_exists?(command) click to toggle source

Determines if a shell command exists by searching for it in ENV.

    # File lib/hirb/util.rb, line 55
    def command_exists?(command)
      ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exists? File.join(d, command) }
    end

detect_terminal_size() click to toggle source

Returns [width, height] of terminal when detected, nil if not detected. Think of this as a simpler version of Highline’s Highline::SystemExtensions.terminal_size()

    # File lib/hirb/util.rb, line 61
    def detect_terminal_size
      if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
        [ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
      elsif (RUBY_PLATFORM =~ /java/ || !STDIN.tty?) && command_exists?('tput')
        [`tput cols`.to_i, `tput lines`.to_i]
      else
        command_exists?('stty') ? `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse : nil
      end
    rescue
      nil
    end

find_home() click to toggle source

From Rubygems, determine a user’s home.

    # File lib/hirb/util.rb, line 86
    def find_home
      ['HOME', 'USERPROFILE'].each {|e| return ENV[e] if ENV[e] }
      return "#{ENV['HOMEDRIVE']}#{ENV['HOMEPATH']}" if ENV['HOMEDRIVE'] && ENV['HOMEPATH']
      File.expand_path("~")
    rescue
      File::ALT_SEPARATOR ? "C:/" : "/"
    end

recursive_hash_merge(hash1, hash2) click to toggle source

Recursively merge hash1 with hash2.

    # File lib/hirb/util.rb, line 21
    def recursive_hash_merge(hash1, hash2)
      hash1.merge(hash2) {|k,o,n| (o.is_a?(Hash)) ? recursive_hash_merge(o,n) : n}
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.