Class: StyleTrain::Sheet

Inherits:
Object
  • Object
show all
Defined in:
lib/style_train/sheet.rb

Direct Known Subclasses

ThemedSheet

Constant Summary collapse

TAGS =
StyleTrain::Style::TAGS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Sheet

Returns a new instance of Sheet.



5
6
7
8
9
# File 'lib/style_train/sheet.rb', line 5

def initialize(opts={})
  self.output = []
  self.contexts = []
  self.level = 0
end

Instance Attribute Details

#contextsObject

Returns the value of attribute contexts.



3
4
5
# File 'lib/style_train/sheet.rb', line 3

def contexts
  @contexts
end

#levelObject

Returns the value of attribute level.



3
4
5
# File 'lib/style_train/sheet.rb', line 3

def level
  @level
end

#outputObject

Returns the value of attribute output.



3
4
5
# File 'lib/style_train/sheet.rb', line 3

def output
  @output
end

Class Method Details

.export(opts = {}) ⇒ Object



431
432
433
# File 'lib/style_train/sheet.rb', line 431

def self.export opts={}
  new.export opts
end

.render(render_method = :content) ⇒ Object



427
428
429
# File 'lib/style_train/sheet.rb', line 427

def self.render(render_method=:content)
  new.render(render_method)
end

Instance Method Details

#add_style_to_queue(s, &block) ⇒ Object



73
74
75
76
77
78
79
80
81
82
# File 'lib/style_train/sheet.rb', line 73

def add_style_to_queue(s, &block)
  self.output << s
  self.contexts.unshift( s )
  if block_given?
    self.level += 1
    yield
    self.level -= 1
  end
  self.contexts.shift
end

#alpha(value) ⇒ Object



305
306
307
# File 'lib/style_train/sheet.rb', line 305

def alpha(value)
  property('filter', "alpha(opacity=#{(value.to_i)})")
end

#attr(*selector, &block) ⇒ Object



68
69
70
71
# File 'lib/style_train/sheet.rb', line 68

def attr(*selector, &block)
  selector = selector.map{|s| "[#{s}]"}
  concat(*selector, &block)
end

#background(opts) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/style_train/sheet.rb', line 113

def background( opts )
  str = ""
  if opts.is_a?(Hash)
    str << property('background-color', Color.new(opts[:color])) if opts[:color]
    str << property('background-image', "url('#{opts[:image]}')") if opts[:image]
    str << property('background-position', opts[:position]) if opts[:position]
    str << property('background-attachment', opts[:attachment]) if opts[:attachment]
    str << property('background-repeat', background_repeat_value(opts[:repeat])) if opts[:repeat]
  else
    str << property('background', opts )
  end
  str
end

#background_repeat_value(value) ⇒ Object



127
128
129
130
131
132
133
134
135
136
# File 'lib/style_train/sheet.rb', line 127

def background_repeat_value(value)
  value = value.to_sym
  if value == :x || value == :y
    "repeat-#{value}"
  elsif value == :none
    'no-repeat'
  else
    value
  end
end

#border(*args) ⇒ Object



138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/style_train/sheet.rb', line 138

def border(*args)
  str = ""
  if args.size == 1
    opts = args.first
  else
    opts = args.last || {}
    none = true
  end
  
  
  if opts.is_a?(Hash)
    value = border_value(opts)
    if only = opts[:only]
      Array(only).flatten.each do |side| 
        value = :none if none
        str << property("border-#{side}", value) 
      end
    else  
      str << property( "border", value )
    end
  else
    str << property( "border", opts )
  end
  
  str
end

#border_value(opts) ⇒ Object



174
175
176
177
178
179
180
181
182
183
# File 'lib/style_train/sheet.rb', line 174

def border_value(opts)
  if opts.is_a?(Hash)
    color = opts[:color] || 'black'
    style = opts[:style] || 'solid'
    width = opts[:width] || opts[:size] || '1px'
    "#{width} #{style} #{color}"
  else
    opts.to_s
  end
end

#child(*selectors, &block) ⇒ Object



63
64
65
66
# File 'lib/style_train/sheet.rb', line 63

def child(*selectors, &block)
  s = Style.new( style_options.merge(:selectors => selectors, :child => true))
  add_style_to_queue(s, &block)
end

#concat(*selectors, &block) ⇒ Object Also known as: cat



56
57
58
59
# File 'lib/style_train/sheet.rb', line 56

def concat(*selectors, &block)
  s = Style.new( style_options.merge(:selectors => selectors, :concat => true ) )
  add_style_to_queue(s, &block)
end

#contentObject



423
424
425
# File 'lib/style_train/sheet.rb', line 423

def content
  # override me in subclasses
end

#contextObject



84
85
86
# File 'lib/style_train/sheet.rb', line 84

def context
  contexts.first
end

#corner_bottom_left(size) ⇒ Object



364
365
366
367
368
369
370
# File 'lib/style_train/sheet.rb', line 364

def corner_bottom_left size
  str = ""
  str << property('border-bottom-left-radius', size)
  str << property('-moz-border-radius-bottomleft', size)
  str << property('-webkit-border-bottom-left-radius', size)
  str
end

#corner_bottom_right(size) ⇒ Object



380
381
382
383
384
385
386
# File 'lib/style_train/sheet.rb', line 380

def corner_bottom_right size
  str = ""
  str << property('border-bottom-right-radius', size)
  str << property('-moz-border-radius-bottomright', size)
  str << property('-webkit-border-bottom-right-radius', size)
  str
end

#corner_top_left(size) ⇒ Object



356
357
358
359
360
361
362
# File 'lib/style_train/sheet.rb', line 356

def corner_top_left size
  str = ""
  str << property('border-top-left-radius', size)
  str << property('-moz-border-radius-topleft', size)
  str << property('-webkit-border-top-left-radius', size)
  str
end

#corner_top_right(size) ⇒ Object



372
373
374
375
376
377
378
# File 'lib/style_train/sheet.rb', line 372

def corner_top_right size
  str = ""
  str << property('border-top-right-radius', size)
  str << property('-moz-border-radius-topright', size)
  str << property('-webkit-border-top-right-radius', size)
  str
end

#corners(opts) ⇒ Object



321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
# File 'lib/style_train/sheet.rb', line 321

def corners(opts)
  str = ""
  if opts.is_a?(Hash)
    if opts[:left]
      str << corner_top_left( opts[:left] )
      str << corner_bottom_left( opts[:left] )
    end
    
    if opts[:right]
      str << corner_top_right( opts[:right] )
      str << corner_bottom_right( opts[:right] )
    end
    
    if opts[:top]
      str << corner_top_right( opts[:top] )
      str << corner_top_left( opts[:top] )
    end
    
    if opts[:bottom]
      str << corner_bottom_right( opts[:bottom] )
      str << corner_bottom_left( opts[:bottom] )
    end
    
    str << corner_top_left( opts[:top_left] )         if opts[:top_left]
    str << corner_top_right( opts[:top_right] )       if opts[:top_right]
    str << corner_bottom_left( opts[:bottom_left] )   if opts[:bottom_left]
    str << corner_bottom_right( opts[:bottom_right] ) if opts[:bottom_right]
  else
    str << property('border-radius', opts )
    str << property('-moz-border-radius', opts)
    str << property('-webkit-border-radius', opts)
  end
  str
end

#default_shadow_colorObject



404
405
406
# File 'lib/style_train/sheet.rb', line 404

def default_shadow_color
  :black # this too
end

#default_shadow_offsetObject



400
401
402
# File 'lib/style_train/sheet.rb', line 400

def default_shadow_offset
  0.25.em # this can be overwritten on a class by class basis
end

#export(opts = {}) ⇒ Object



435
436
437
438
439
# File 'lib/style_train/sheet.rb', line 435

def export opts={}
  name = file_name(opts[:file_name]) 
  str = render(opts[:render_method] || :content), opts
  File.open("#{StyleTrain.dir}/#{file_name}.css", 'w'){ |f| f.write(str) }
end

#file_name(name = nil) ⇒ Object



441
442
443
# File 'lib/style_train/sheet.rb', line 441

def file_name name=nil
  name || self.class.to_s.underscore
end

#gradient(opts = {}) ⇒ Object

Raises:

  • (ArgumentError)


412
413
414
415
416
417
418
419
420
421
# File 'lib/style_train/sheet.rb', line 412

def gradient(opts={})
  raise ArgumentError, "gradient styles require a :start and :end color" unless opts[:start] && opts[:end]
  opts[:from] ||= 'top'
  direction = opts[:from] == 'top' ? "left top, left bottom" : "left top, right top" 
  str = ""
  str << property('background', opts[:end])
  str << property('background', "-webkit-gradient(linear, #{direction}, from(#{opts[:start]}), to(#{opts[:end]}))")
  str << property('background', "-moz-linear-gradient(#{opts[:from]},  #{opts[:start]},  #{opts[:end]})")
  str
end

#headerObject



32
33
34
35
36
37
38
# File 'lib/style_train/sheet.rb', line 32

def header
<<-CSS
/*
  Generated by StlyeTrain CSS generator via the class #{self.class}
*/
CSS
end

#id(*selectors, &block) ⇒ Object



51
52
53
54
# File 'lib/style_train/sheet.rb', line 51

def id(*selectors, &block)
  selectors = selectors.map{|s| "##{s}"}
  style(*selectors, &block)
end

#list(opts) ⇒ Object



217
218
219
220
221
222
223
# File 'lib/style_train/sheet.rb', line 217

def list(opts)
  str = ""
  str << property('list-style-image', opts[:image]) if opts[:image]
  str << property('list-style-type', opts[:type]) if opts[:type]
  str << property('list-style-position', opts[:position]) if opts[:position]
  str
end

#margin(*opts) ⇒ Object



225
226
227
228
229
230
231
232
233
234
235
236
237
# File 'lib/style_train/sheet.rb', line 225

def margin(*opts)
  opts = opts.size == 1 ? opts.first : opts
  if opts.is_a?(Hash)
      str = ""
      str << property('margin-left', opts[:left]) if opts[:left]
      str << property('margin-top', opts[:top]) if opts[:top]
      str << property('margin-bottom', opts[:bottom]) if opts[:bottom]
      str << property('margin-right', opts[:right]) if opts[:right]
      str
  else
    property('margin', opts)
  end
end

#opacity(value, opts = {}) ⇒ Object



297
298
299
300
301
302
303
# File 'lib/style_train/sheet.rb', line 297

def opacity(value, opts={})
  value = value.to_f
  str = ""
  str << property( 'opacity', value )
  str << alpha( value.to_f*100 ) if opts[:alpha]
  str 
end

#outline(opts = {}) ⇒ Object



185
186
187
188
# File 'lib/style_train/sheet.rb', line 185

def outline(opts={})
  value = border_value(opts)
  property "outline", value
end

#overflow(opts) ⇒ Object



286
287
288
289
290
291
292
293
294
295
# File 'lib/style_train/sheet.rb', line 286

def overflow(opts)
  if opts.is_a?(Hash)
    str = ""
    str << property( 'overflow-x', opts[:x]) if opts[:x]
    str << property( 'overflow-y', opts[:y]) if opts[:y]
    str
  else
    property 'overflow', opts
  end
end

#padding(*opts) ⇒ Object



239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'lib/style_train/sheet.rb', line 239

def padding(*opts)
  opts = opts.size == 1 ? opts.first : opts
  if opts.is_a?(Hash)
    str = ""
    str << property('padding-left', opts[:left]) if opts[:left]
    str << property('padding-top', opts[:top]) if opts[:top]
    str << property('padding-bottom', opts[:bottom]) if opts[:bottom]
    str << property('padding-right', opts[:right]) if opts[:right]
    str
  else
    property('padding', opts)
  end
end

#position(opts) ⇒ Object



253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/style_train/sheet.rb', line 253

def position(opts)
  if opts.is_a?(Hash)
    str = ""
    str << property('position', opts[:type]) if opts[:type]
    str << property('bottom', opts[:bottom]) if opts[:bottom]
    str << property('top', opts[:top]) if opts[:top]
    str << property('left', opts[:left]) if opts[:left]
    str << property('right', opts[:right]) if opts[:right]
    str << property('float', opts[:float]) if opts[:float]
    str << property('clear', opts[:clear]) if opts[:clear]
    str << property('display', opts[:display]) if opts[:display]
    str << property('visibility', opts[:visibility]) if opts[:visibility]
    str << property('z-index', opts[:z_index]) if opts[:z_index]
    str << property('overflow', opts[:overflow]) if opts[:overflow]
    str << property('overflow-x', opts[:overflow_x]) if opts[:overflow_x]
    str << property('overflow-y', opts[:overflow_y]) if opts[:overflow_y]
    str << property('clip', "rect(#{opts[:clip].join(' ')})") if opts[:clip]
    str
  else
    property('position', opts)
  end
end

#property(label, value) ⇒ Object



106
107
108
109
110
111
# File 'lib/style_train/sheet.rb', line 106

def property( label, value )
  value = value.join(' ') if value.is_a?(Array)
  str = "#{label}: #{value};"
  self.context.properties << str
  str
end

#render(render_method = :content, opts = {}) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/style_train/sheet.rb', line 11

def render render_method = :content, opts={}
  self.output = [header]
  if render_method == :content
    content
  else
    send render_method
  end
  
  render_array(opts)
end

#render_array(opts = {}) ⇒ Object



22
23
24
25
26
27
28
29
30
# File 'lib/style_train/sheet.rb', line 22

def render_array opts={}
  if opts[:type] 
    render_type = opts[:type]
  else
    render_type = :full
  end
  
  output.map{|s| s.is_a?(String) ? s : s.render( render_type ) }.join("\n")
end

#shadow(opts = {}) ⇒ Object



388
389
390
391
392
393
394
395
396
397
398
# File 'lib/style_train/sheet.rb', line 388

def shadow(opts={})
  opts[:horizontal_offset] ||= default_shadow_offset
  opts[:vertical_offset] ||= default_shadow_offset
  opts[:blur] ||= default_shadow_offset
  opts[:color] ||= default_shadow_color
  str = ""
  str << property('box-shadow', shadow_options(opts))
  str << property('-webkit-box-shadow', shadow_options(opts))
  str << property('-moz-box-shadow', shadow_options(opts))
  str
end

#shadow_options(opts) ⇒ Object



408
409
410
# File 'lib/style_train/sheet.rb', line 408

def shadow_options(opts)
  "#{opts[:inner] ? 'inset ' : ''}#{opts[:horizontal_offset]} #{opts[:vertical_offset]} #{opts[:blur]} #{opts[:color]}"
end

#style(*selectors, &block) ⇒ Object Also known as: c



44
45
46
47
# File 'lib/style_train/sheet.rb', line 44

def style(*selectors, &block)
  s = Style.new( style_options.merge(:selectors => selectors) )
  add_style_to_queue(s, &block)
end

#style_optionsObject



40
41
42
# File 'lib/style_train/sheet.rb', line 40

def style_options
  {:level => level, :context => context}
end

#table_options(opts) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/style_train/sheet.rb', line 276

def table_options(opts)
  str = ""
  str << property('border-collapse', opts[:border]) if opts[:border]
  str << property('border-spacing', opts[:border_spacing]) if opts[:border_spacing]
  str << property('caption-side', opts[:caption]) if opts[:caption]
  str << property('empty-cells', opts[:empty]) if opts[:empty]
  str << property('table-layout', opts[:layout]) if opts[:layout]
  str 
end

#text(opts) ⇒ Object Also known as: font



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'lib/style_train/sheet.rb', line 190

def text(opts)
  str = ""
  if family = opts[:font] || opts[:family]
    str << property('font-family', family)
  end
  str << property('font-weight', opts[:weight]) if opts[:weight]
  str << property('font-variant', opts[:variant]) if opts[:variant]
  str << property('font-style', opts[:style]) if opts[:style]
  str << property('font-size', opts[:size]) if opts[:size]
  str << property('color', opts[:color]) if opts[:color]
  str << property('text-direction', opts[:direction]) if opts[:direction]
  str << property('letter-spacing', opts[:spacing]) if opts[:spacing]
  if height = opts[:line_height] || opts[:height]
    str << property('line-height', height)
  end
  str << property('text-align', opts[:align]) if opts[:align]
  str << property('text-decoration', opts[:decoration]) if opts[:decoration]
  str << property('text-indent', opts[:indent]) if opts[:indent]
  str << property('text-transform', opts[:transform]) if opts[:transform]
  str << property('vertical-align', opts[:vertical_align]) if opts[:vertical_align]
  str << property('white-space', opts[:white_space]) if opts[:white_space]
  str << property('word-spacing', opts[:word_spacing]) if opts[:word_spacing]
  str
end