Class Builder::CSS
In: vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb
Parent: BlankSlate

Create a Cascading Style Sheet (CSS) using Ruby.

Example usage:

  css = Builder::CSS.new

  text_color      = '#7F7F7F'
  preferred_fonts = 'Helvetica, Arial, sans_serif'

  css.comment! 'This is our stylesheet'
  css.body {
    background_color '#FAFAFA'
    font_size        'small'
    font_family      preferred_fonts
    color            text_color
  }

  css.id!('navbar') {
    width            '500px'
  }

  css.class!('navitem') {
    color            'red'
  }

  css.a :hover {
    text_decoration  'underline'
  }

  css.div(:id => 'menu') {
    background       'green'
  }

  css.div(:class => 'foo') {
    background       'red'
  }

This will yield the following stylesheet:

  /* This is our stylesheet */
  body {
    background_color: #FAFAFA;
    font_size:        small;
    font_family:      Helvetica, Arial, sans_serif;
    color:            #7F7F7F;
  }

  #navbar {
    width:            500px;
  }

  .navitem {
    color:            red;
  }

  a:hover {
    text_decoration:  underline;
  }

  div#menu {
    background:       green;
  }

  div.foo {
    background:       red;
  }

Methods

+   >   >>   class!   comment!   group!   id!   method_missing   new   nil?   store!   target!   |  

Public Class methods

Create a CSS builder.

out:Object receiving the markup.1 out must respond to <<.
indent:Number of spaces used for indentation (0 implies no indentation and no line breaks).

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 101
101:     def initialize(indent=2)
102:       @indent      = indent
103:       @target      = []
104:       @parts       = []
105:       @library     = {}
106:     end

Public Instance methods

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 108
108:     def +(part)
109:       _join_with_op! '+'
110:       self
111:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 118
118:     def >(part)
119:       _join_with_op! '>'
120:       self
121:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 113
113:     def >>(part)
114:       _join_with_op! ''
115:       self
116:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 145
145:     def class!(arg, &block)
146:       _start_container('.'+arg.to_s, nil, block_given?)
147:       _css_block(block) if block
148:       _unify_block
149:       self
150:     end

Create a comment string in the output.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 134
134:     def comment!(comment_text)
135:       @target << "/* #{comment_text} */\n"
136:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 156
156:     def group!(*args, &block)
157:       args.each do |arg|
158:         if arg.is_a?(Symbol)
159:           instance_eval(&@library[arg])
160:         else
161:           instance_eval(&arg)
162:         end
163:         _text ', ' unless arg == args.last
164:       end
165:       if block
166:         _css_block(block)
167:         _unify_block
168:       end
169:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 138
138:     def id!(arg, &block)
139:       _start_container('#'+arg.to_s, nil, block_given?)
140:       _css_block(block) if block
141:       _unify_block
142:       self
143:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 171
171:     def method_missing(sym, *args, &block)
172:       sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol)
173:       if block
174:         _start_container(sym, args.first)
175:         _css_block(block)
176:         _unify_block
177:       elsif @in_block
178:         _indent
179:         _css_line(sym, *args)
180:         _newline
181:         return self
182:       else
183:         _start_container(sym, args.first, false)
184:         _unify_block
185:       end
186:       self
187:     end

"Cargo culted" from Jim who also "cargo culted" it. See xmlbase.rb.

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 190
190:     def nil?
191:       false
192:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 152
152:     def store!(sym, &block)
153:       @library[sym] = block.to_proc
154:     end

Return the target of the builder

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 129
129:     def target!
130:       @target * ''
131:     end

[Source]

     # File vendor/rails/activesupport/lib/active_support/vendor/builder-2.1.2/builder/css.rb, line 123
123:     def |(part)
124:       _join_with_op! ','
125:       self
126:     end

[Validate]