module SassC::Util
A module containing various useful functions.
Constants
- RUBY_ENGINE
-
The Ruby engine we’re running under. Defaults to ‘“ruby”` if the top-level constant is undefined. @api public
- RUBY_VERSION_COMPONENTS
-
An array of ints representing the Ruby version number. @api public
Public Instance Methods
Source
# File lib/sassc/util.rb, line 104 def abstract(obj) raise NotImplementedError.new("#{obj.class} must implement ##{caller_info[2]}") end
Throws a NotImplementedError for an abstract method.
@param obj [Object] ‘self` @raise [NotImplementedError]
Source
# File lib/sassc/util.rb, line 90 def caller_info(entry = nil) # JRuby evaluates `caller` incorrectly when it's in an actual default argument. entry ||= caller[1] info = entry.scan(/^((?:[A-Za-z]:)?.*?):(-?.*?)(?::.*`(.+)')?$/).first info[1] = info[1].to_i # This is added by Rubinius to designate a block, but we don't care about it. info[2].sub!(/ \{\}\Z/, '') if info[2] info end
Returns information about the caller of the previous method.
@param entry [String] An entry in the ‘#caller` list, or a similarly formatted string @return [[String, Integer, (String, nil)]]
An array containing the filename, line, and method name of the caller. The method name may be nil
Source
# File lib/sassc/util.rb, line 44 def clamp(value, min, max) return min if value < min return max if value > max return value end
Restricts the numeric ‘value` to be within `min` and `max`, inclusive. If the value is lower than `min`
Source
# File lib/sassc/util.rb, line 112 def deprecated(obj, message = nil) obj_class = obj.is_a?(Class) ? "#{obj}." : "#{obj.class}#" full_message = "DEPRECATION WARNING: #{obj_class}#{caller_info[2]} " + "will be removed in a future version of Sass.#{("\n" + message) if message}" SassC::Util.sass_warn full_message end
Prints a deprecation warning for the caller method.
@param obj [Object] ‘self` @param message [String] A message describing what to do instead.
Source
# File lib/sassc/util.rb, line 179 def ironruby? return @ironruby if defined?(@ironruby) @ironruby = RUBY_ENGINE == "ironruby" end
Whether or not this is running on IronRuby.
@return [Boolean]
Source
# File lib/sassc/util.rb, line 195 def jruby? return @jruby if defined?(@jruby) @jruby = RUBY_PLATFORM =~ /java/ end
Whether or not this is running on JRuby.
@return [Boolean]
Source
# File lib/sassc/util.rb, line 203 def jruby_version @jruby_version ||= ::JRUBY_VERSION.split(".").map {|s| s.to_i} end
Returns an array of ints representing the JRuby version number.
@return [Array<Integer>]
Source
# File lib/sassc/util.rb, line 38 def map_keys(hash) map_hash(hash) {|k, v| [yield(k), v]} end
Maps the keys in a hash according to a block. @example
map_keys({:foo => "bar", :baz => "bang"}) {|k| k.to_s} #=> {"foo" => "bar", "baz" => "bang"}
@param hash [Hash] The hash to map @yield [key] A block in which the keys are transformed @yieldparam key [Object] The key that should be mapped @yieldreturn [Object] The new value for the key @return [Hash] The mapped hash @see map_vals @see map_hash
Source
# File lib/sassc/util.rb, line 78 def paths(arrs) arrs.inject([[]]) do |paths, arr| arr.map {|e| paths.map {|path| path + [e]}}.flatten(1) end end
Return an array of all possible paths through the given arrays.
@param arrs [Array<Array>] @return [Array<Arrays>]
@example
paths([[1, 2], [3, 4], [5]]) #=> # [[1, 3, 5], # [2, 3, 5], # [1, 4, 5], # [2, 4, 5]]
Source
# File lib/sassc/util.rb, line 157 def rails_env return ::Rails.env.to_s if defined?(::Rails.env) return RAILS_ENV.to_s if defined?(RAILS_ENV) nil end
Returns the environment of the Rails application, if this is running in a Rails context. Returns ‘nil` if no such environment is defined.
@return [String, nil]
Source
# File lib/sassc/util.rb, line 143 def rails_root if defined?(::Rails.root) return ::Rails.root.to_s if ::Rails.root raise "ERROR: Rails.root is nil!" end return RAILS_ROOT.to_s if defined?(RAILS_ROOT) nil end
Returns the root of the Rails application, if this is running in a Rails context. Returns ‘nil` if no such root is defined.
@return [String, nil]
Source
# File lib/sassc/util.rb, line 187 def rbx? return @rbx if defined?(@rbx) @rbx = RUBY_ENGINE == "rbx" end
Whether or not this is running on Rubinius.
@return [Boolean]
Source
# File lib/sassc/util.rb, line 217 def relative_path_from(path, from) pathname(path.to_s).relative_path_from(pathname(from.to_s)) rescue NoMethodError => e raise e unless e.name == :zero? # Work around https://github.com/ruby/ruby/pull/713. path = path.to_s from = from.to_s raise ArgumentError("Incompatible path encodings: #{path.inspect} is #{path.encoding}, " + "#{from.inspect} is #{from.encoding}") end
Returns ‘path` relative to `from`.
This is like ‘Pathname#relative_path_from` except it accepts both strings and pathnames, it handles Windows path separators correctly, and it throws an error rather than crashing if the paths use different encodings (github.com/ruby/ruby/pull/713).
@param path [String, Pathname] @param from [String, Pathname] @return [Pathname?]
Source
# File lib/sassc/util.rb, line 55 def round(value) # If the number is within epsilon of X.5, round up (or down for negative # numbers). mod = value % 1 mod_is_half = (mod - 0.5).abs < SassC::Script::Value::Number.epsilon if value > 0 !mod_is_half && mod < 0.5 ? value.floor : value.ceil else mod_is_half || mod < 0.5 ? value.floor : value.ceil end end
Like [Fixnum.round], but leaves rooms for slight floating-point differences.
@param value [Numeric] @return [Numeric]
Source
# File lib/sassc/util.rb, line 132 def sass_warn(msg) Sass.logger.warn("#{msg}\n") end
The same as ‘Kernel#warn`, but is silenced by {#silence_sass_warnings}.
@param msg [String]
Source
# File lib/sassc/util.rb, line 122 def silence_sass_warnings old_level, Sass.logger.log_level = Sass.logger.log_level, :error yield ensure SassC.logger.log_level = old_level end
Silences all Sass warnings within a block.
@yield A block in which no Sass warnings will be printed
Source
# File lib/sassc/util.rb, line 171 def windows? return @windows if defined?(@windows) @windows = (RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw/i) end
Whether or not this is running on Windows.
@return [Boolean]