Class: Unify::CSS

Inherits:
Object
  • Object
show all
Defined in:
lib/unify/css.rb

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ CSS



13
14
15
16
# File 'lib/unify/css.rb', line 13

def initialize(&block)
  @selector_stack = []
  @styles = {}
end

Instance Method Details

#_(name, properties = {}, &block) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/unify/css.rb', line 18

def _(name, properties = {}, &block)
  @selector_stack.push(name.to_s)
  (@styles[@selector_stack.join(' ')] ||= {}).merge!(properties)
  if block_given?
    instance_eval(&block)
  end
  @selector_stack.pop
end

#to_cssObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/unify/css.rb', line 27

def to_css
  css = ''
  for selector in @styles.keys.sort
    unless @styles[selector].empty?
      css << "#{selector}{"
      for key in @styles[selector].keys.sort
        css << "#{key}:#{@styles[selector][key]};"
      end
      css.chop!
      css << "}"
    end
  end
  css
end