Module: SassC::Util
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
-
#abstract(obj) ⇒ Object
Throws a NotImplementedError for an abstract method.
-
#caller_info(entry = nil) ⇒ [String, Integer, (String, nil)]
Returns information about the caller of the previous method.
-
#clamp(value, min, max) ⇒ Object
Restricts the numeric ‘value` to be within `min` and `max`, inclusive.
-
#deprecated(obj, message = nil) ⇒ Object
Prints a deprecation warning for the caller method.
-
#ironruby? ⇒ Boolean
Whether or not this is running on IronRuby.
-
#jruby? ⇒ Boolean
Whether or not this is running on JRuby.
-
#jruby_version ⇒ Array<Integer>
Returns an array of ints representing the JRuby version number.
-
#map_keys(hash) {|key| ... } ⇒ Hash
Maps the keys in a hash according to a block.
-
#paths(arrs) ⇒ Array<Arrays>
Return an array of all possible paths through the given arrays.
-
#rails_env ⇒ String?
Returns the environment of the Rails application, if this is running in a Rails context.
-
#rails_root ⇒ String?
Returns the root of the Rails application, if this is running in a Rails context.
-
#rbx? ⇒ Boolean
Whether or not this is running on Rubinius.
-
#relative_path_from(path, from) ⇒ Pathname?
Returns ‘path` relative to `from`.
-
#round(value) ⇒ Numeric
Like [Fixnum.round], but leaves rooms for slight floating-point differences.
-
#sass_warn(msg) ⇒ Object
The same as ‘Kernel#warn`, but is silenced by #silence_sass_warnings.
-
#silence_sass_warnings { ... } ⇒ Object
Silences all Sass warnings within a block.
-
#windows? ⇒ Boolean
Whether or not this is running on Windows.
Instance Method Details
#abstract(obj) ⇒ Object
Throws a NotImplementedError for an abstract method.
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.
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.
112 113 114 115 116 117 |
# File 'lib/sassc/util.rb', line 112 def deprecated(obj, = nil) obj_class = obj.is_a?(Class) ? "#{obj}." : "#{obj.class}#" = "DEPRECATION WARNING: #{obj_class}#{caller_info[2]} " + "will be removed in a future version of Sass.#{("\n" + ) if }" SassC::Util.sass_warn end |
#ironruby? ⇒ Boolean
Whether or not this is running on IronRuby.
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.
195 196 197 198 |
# File 'lib/sassc/util.rb', line 195 def jruby? return @jruby if defined?(@jruby) @jruby = RUBY_PLATFORM =~ /java/ end |
#jruby_version ⇒ Array<Integer>
Returns an array of ints representing the JRuby version number.
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.
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.
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_env ⇒ String?
Returns the environment of the Rails application, if this is running in a Rails context. Returns ‘nil` if no such environment is defined.
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_root ⇒ String?
Returns the root of the Rails application, if this is running in a Rails context. Returns ‘nil` if no such root is defined.
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.
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).
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.
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.
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.
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.
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 |