Class: Builder::CSS

Inherits:
BlankSlate show all
Defined in:
lib/active_support/vendor/builder-2.1.2/builder/css.rb

Overview

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;
}

Instance Method Summary collapse

Methods inherited from BlankSlate

find_hidden_method, hide, reveal

Constructor Details

#initialize(indent = 2) ⇒ CSS

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



101
102
103
104
105
106
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 101

def initialize(indent=2)
  @indent      = indent
  @target      = []
  @parts       = []
  @library     = {}
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sym, *args, &block) ⇒ Object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 171

def method_missing(sym, *args, &block)
  sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol)
  if block
    _start_container(sym, args.first)
    _css_block(block)
    _unify_block
  elsif @in_block
    _indent
    _css_line(sym, *args)
    _newline
    return self
  else
    _start_container(sym, args.first, false)
    _unify_block
  end
  self
end

Instance Method Details

#+(part) ⇒ Object



108
109
110
111
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 108

def +(part)
  _join_with_op! '+'
  self
end

#>(part) ⇒ Object



118
119
120
121
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 118

def >(part)
  _join_with_op! '>'
  self
end

#>>(part) ⇒ Object



113
114
115
116
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 113

def >>(part)
  _join_with_op! ''
  self
end

#class!(arg, &block) ⇒ Object



145
146
147
148
149
150
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 145

def class!(arg, &block)
  _start_container('.'+arg.to_s, nil, block_given?)
  _css_block(block) if block
  _unify_block
  self
end

#comment!(comment_text) ⇒ Object

Create a comment string in the output.



134
135
136
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 134

def comment!(comment_text)
  @target << "/* #{comment_text} */\n"
end

#group!(*args, &block) ⇒ Object



156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 156

def group!(*args, &block)
  args.each do |arg|
    if arg.is_a?(Symbol)
      instance_eval(&@library[arg])
    else
      instance_eval(&arg)
    end
    _text ', ' unless arg == args.last
  end
  if block
    _css_block(block)
    _unify_block
  end
end

#id!(arg, &block) ⇒ Object



138
139
140
141
142
143
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 138

def id!(arg, &block)
  _start_container('#'+arg.to_s, nil, block_given?)
  _css_block(block) if block
  _unify_block
  self
end

#nil?Boolean

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

Returns:

  • (Boolean)


190
191
192
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 190

def nil?
  false
end

#store!(sym, &block) ⇒ Object



152
153
154
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 152

def store!(sym, &block)
  @library[sym] = block.to_proc
end

#target!Object

Return the target of the builder



129
130
131
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 129

def target!
  @target * ''
end

#|(part) ⇒ Object



123
124
125
126
# File 'lib/active_support/vendor/builder-2.1.2/builder/css.rb', line 123

def |(part)
  _join_with_op! ','
  self
end