Class: SassyHash
- Inherits:
-
Hash
- Object
- Hash
- SassyHash
- Defined in:
- lib/sassy_hash.rb
Constant Summary collapse
- VERSION =
"0.0.6"
- VALID_UNIT =
%r{(?<unit>#{::Sass::SCSS::RX::NMSTART}#{::Sass::SCSS::RX::NMCHAR}|%*)}
- VALID_NUMBER =
%r{((?<float>^[0-9]*\.[0-9]+)|(?<int>^[0-9]+))#{VALID_UNIT}}
Class Method Summary collapse
Instance Method Summary collapse
- #[]=(key, value) ⇒ Object
-
#initialize(*args) ⇒ SassyHash
constructor
A new instance of SassyHash.
- #sassify! ⇒ Object
Constructor Details
#initialize(*args) ⇒ SassyHash
Returns a new instance of SassyHash.
14 15 16 17 18 |
# File 'lib/sassy_hash.rb', line 14 def initialize(*args) super(args).tap do |hash| hash.sassify! end end |
Class Method Details
.[](hash_values) ⇒ Object
8 9 10 11 12 |
# File 'lib/sassy_hash.rb', line 8 def self.[](hash_values) super(hash_values).tap do |hash| hash.sassify! end end |
.sass_convert_value(value) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/sassy_hash.rb', line 34 def self.sass_convert_value(value) case value when Integer, Fixnum return ::Sass::Script::Value::Number.new(value.to_i) when Float return ::Sass::Script::Value::Number.new(value.to_f) when Symbol return ::Sass::Script::Value::String.new(value.to_s) when String # hex color if value =~ ::Sass::SCSS::RX::HEXCOLOR return ::Sass::Script::Value::Color.from_hex(value) end #number if matches = value.match(VALID_NUMBER) num = if matches[:float] matches[:float].to_f elsif matches[:int] matches[:int].to_i end return ::Sass::Script::Value::Number.new(num, matches[:unit]) end #string return ::Sass::Script::Value::String.new(value) when Array return ::Sass::Script::Value::List.new(value.map {|v| sass_convert_value(v) }, :sapce) when Hash return ::Sass::Script::Value::Map.new(self[value]) when TrueClass, FalseClass return ::Sass::Script::Value::Bool.new(value) when ::Sass::Script::Value::Base return value else raise SassyHashException, "Non convertable value given" end end |
Instance Method Details
#[]=(key, value) ⇒ Object
28 29 30 31 32 |
# File 'lib/sassy_hash.rb', line 28 def []=(key, value) key = self.class.sass_convert_value(key.to_s) value = self.class.sass_convert_value(value) super(key, value) end |
#sassify! ⇒ Object
20 21 22 23 24 25 |
# File 'lib/sassy_hash.rb', line 20 def sassify! keys.each do |key| new_key = self.class.sass_convert_value(key.to_s) self[new_key] = self.class.sass_convert_value(delete(key)) end end |