Sha256: b20fdeb4021ff9e80916ccb0a000153760d94a486218d7357952a49d2e479453

Contents?: true

Size: 1.85 KB

Versions: 1

Compression:

Stored size: 1.85 KB

Contents

require 'wright/util/stolen_from_activesupport'

module Wright
  # @api private
  # Various utility functions.
  module Util
    # Converts a class constant into its corresponding resource name.
    #
    # @param klass [Class] the class constant
    #
    # @example
    #   Wright::Util.class_to_resource_name(Wright::Resource::Package)
    #   # => "package"
    #
    #   Wright::Util.class_to_resource_name(Foo::Bar::BazQux)
    #   # => "baz_qux"
    #
    # @return [String] the resource name of the given class
    def self.class_to_resource_name(klass)
      ActiveSupport.underscore(klass.name).split('/').last
    end

    # Converts a file path into its corresponding class name.
    #
    # @param filename [String] the filename
    #
    # @example
    #   Wright::Util.filename_to_classname('foo/bar/baz.rb')
    #   # => "Foo::Bar::Baz"
    #
    #   Wright::Util.filename_to_classname('foo/bar/')
    #   # => "Foo::Bar"
    #
    # @return [String] the class name for the given filename
    def self.filename_to_classname(filename)
      ActiveSupport.camelize(filename.chomp('.rb').chomp('/'))
    end

    def self.distro
      os_release = ::File.read('/etc/os-release')
      /^ID_LIKE=(?<id_like>.*)$/ =~ os_release
      /^ID=(?<id>.*)$/ =~ os_release
      id_like || id || 'linux'
    end
    private_class_method :distro

    # Determines the system's OS family.
    #
    # @example
    #   Wright::Util.os_family
    #   # => "debian"
    # @example
    #   Wright::Util.os_family
    #   # => "macosx"
    #
    # @return [String] the system's OS family (base distribution for
    #   GNU/Linux systems) or 'other' for unknown operating systems
    def self.os_family
      system_arch = RbConfig::CONFIG['target_os']
      case system_arch
      when /darwin/
        'macosx'
      when /linux/
        distro
      else
        'other'
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
wright-0.1.2 lib/wright/util.rb