Module: SassC::Util

Extended by:
Util
Included in:
Util
Defined in:
lib/sassc/util.rb

Overview

A module containing various useful functions.

Defined Under Namespace

Classes: NormalizedMap

Constant Summary collapse

RUBY_VERSION_COMPONENTS =

An array of ints representing the Ruby version number.

RUBY_VERSION.split(".").map {|s| s.to_i}
RUBY_ENGINE =

The Ruby engine we’re running under. Defaults to ‘“ruby”` if the top-level constant is undefined.

defined?(::RUBY_ENGINE) ? ::RUBY_ENGINE : "ruby"

Instance Method Summary collapse

Instance Method Details

#abstract(obj) ⇒ Object

Throws a NotImplementedError for an abstract method.

Parameters:

  • obj (Object)

    ‘self`

Raises:

  • (NotImplementedError)


104
105
106
# File 'lib/sassc/util.rb', line 104

def abstract(obj)
  raise NotImplementedError.new("#{obj.class} must implement ##{caller_info[2]}")
end

#caller_info(entry = nil) ⇒ [String, Integer, (String, nil)]

Returns information about the caller of the previous method.

Parameters:

  • entry (String) (defaults to: nil)

    An entry in the ‘#caller` list, or a similarly formatted string

Returns:

  • ([String, Integer, (String, nil)])

    An array containing the filename, line, and method name of the caller. The method name may be nil



90
91
92
93
94
95
96
97
98
# 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

#clamp(value, min, max) ⇒ Object

Restricts the numeric ‘value` to be within `min` and `max`, inclusive. If the value is lower than `min`



44
45
46
47
48
# 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

#deprecated(obj, message = nil) ⇒ Object

Prints a deprecation warning for the caller method.

Parameters:

  • obj (Object)

    ‘self`

  • message (String) (defaults to: nil)

    A message describing what to do instead.



112
113
114
115
116
117
# 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

#ironruby?Boolean

Whether or not this is running on IronRuby.

Returns:

  • (Boolean)


179
180
181
182
# File 'lib/sassc/util.rb', line 179

def ironruby?
  return @ironruby if defined?(@ironruby)
  @ironruby = RUBY_ENGINE == "ironruby"
end

#jruby?Boolean

Whether or not this is running on JRuby.

Returns:

  • (Boolean)


195
196
197
198
# File 'lib/sassc/util.rb', line 195

def jruby?
  return @jruby if defined?(@jruby)
  @jruby = RUBY_PLATFORM =~ /java/
end

#jruby_versionArray<Integer>

Returns an array of ints representing the JRuby version number.

Returns:

  • (Array<Integer>)


203
204
205
# File 'lib/sassc/util.rb', line 203

def jruby_version
  @jruby_version ||= ::JRUBY_VERSION.split(".").map {|s| s.to_i}
end

#map_keys(hash) {|key| ... } ⇒ Hash

Maps the keys in a hash according to a block.

Examples:

map_keys({:foo => "bar", :baz => "bang"}) {|k| k.to_s}
  #=> {"foo" => "bar", "baz" => "bang"}

Parameters:

  • hash (Hash)

    The hash to map

Yields:

  • (key)

    A block in which the keys are transformed

Yield Parameters:

  • key (Object)

    The key that should be mapped

Yield Returns:

  • (Object)

    The new value for the key

Returns:

  • (Hash)

    The mapped hash

See Also:

  • #map_vals
  • #map_hash


38
39
40
# File 'lib/sassc/util.rb', line 38

def map_keys(hash)
  map_hash(hash) {|k, v| [yield(k), v]}
end

#paths(arrs) ⇒ Array<Arrays>

Return an array of all possible paths through the given arrays.

Examples:

paths([[1, 2], [3, 4], [5]]) #=>
  # [[1, 3, 5],
  #  [2, 3, 5],
  #  [1, 4, 5],
  #  [2, 4, 5]]

Parameters:

  • arrs (Array<Array>)

Returns:

  • (Array<Arrays>)


78
79
80
81
82
# 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

#rails_envString?

Returns the environment of the Rails application, if this is running in a Rails context. Returns ‘nil` if no such environment is defined.

Returns:

  • (String, nil)


157
158
159
160
161
# 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

#rails_rootString?

Returns the root of the Rails application, if this is running in a Rails context. Returns ‘nil` if no such root is defined.

Returns:

  • (String, nil)


143
144
145
146
147
148
149
150
# 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

#rbx?Boolean

Whether or not this is running on Rubinius.

Returns:

  • (Boolean)


187
188
189
190
# File 'lib/sassc/util.rb', line 187

def rbx?
  return @rbx if defined?(@rbx)
  @rbx = RUBY_ENGINE == "rbx"
end

#relative_path_from(path, from) ⇒ Pathname?

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).

Parameters:

  • path (String, Pathname)
  • from (String, Pathname)

Returns:

  • (Pathname?)


217
218
219
220
221
222
223
224
225
226
227
# 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

#round(value) ⇒ Numeric

Like [Fixnum.round], but leaves rooms for slight floating-point differences.

Parameters:

  • value (Numeric)

Returns:

  • (Numeric)


55
56
57
58
59
60
61
62
63
64
65
# 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

#sass_warn(msg) ⇒ Object

The same as ‘Kernel#warn`, but is silenced by #silence_sass_warnings.

Parameters:

  • msg (String)


132
133
134
# File 'lib/sassc/util.rb', line 132

def sass_warn(msg)
  Sass.logger.warn("#{msg}\n")
end

#silence_sass_warnings { ... } ⇒ Object

Silences all Sass warnings within a block.

Yields:

  • A block in which no Sass warnings will be printed



122
123
124
125
126
127
# 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

#windows?Boolean

Whether or not this is running on Windows.

Returns:

  • (Boolean)


171
172
173
174
# File 'lib/sassc/util.rb', line 171

def windows?
  return @windows if defined?(@windows)
  @windows = (RbConfig::CONFIG['host_os'] =~ /mswin|windows|mingw/i)
end