style_train 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.2.4
@@ -154,7 +154,7 @@ module StyleTrain
154
154
  :whitesmoke => [245, 245, 245],
155
155
  :yellow => [255, 255, 0],
156
156
  :yellowgreen => [154, 205, 50],
157
-
157
+ :transparent => [0,0,0]
158
158
  }
159
159
 
160
160
  attr_reader :keyword
@@ -8,7 +8,7 @@ module StyleTrain
8
8
  end
9
9
 
10
10
  def render render_method = :content
11
- self.output = ''
11
+ self.output = header
12
12
  if render_method == :content
13
13
  content
14
14
  else
@@ -17,6 +17,14 @@ module StyleTrain
17
17
  output
18
18
  end
19
19
 
20
+ def header
21
+ <<-CSS
22
+ /*
23
+ Generated by StlyeTrain CSS generator via the class #{self.class}
24
+ */
25
+ CSS
26
+ end
27
+
20
28
  def style(selector)
21
29
  self.output << "\n"
22
30
  if selector.is_a?(String) || TAGS.include?(selector)
@@ -69,7 +77,7 @@ module StyleTrain
69
77
  def background( opts )
70
78
  str = ""
71
79
  str << property('background-color', Color.new(opts[:color])) if opts[:color]
72
- str << property('background-image', opts[:image]) if opts[:image]
80
+ str << property('background-image', "url('#{opts[:image]}')") if opts[:image]
73
81
  str << property('background-position', opts[:position]) if opts[:position]
74
82
  str << property('background-attachment', opts[:attachment]) if opts[:attachment]
75
83
  str << property('background-repeat', background_repeat_value(opts[:repeat])) if opts[:repeat]
@@ -80,25 +88,31 @@ module StyleTrain
80
88
  value = value.to_sym
81
89
  if value == :x || value == :y
82
90
  "repeat-#{value}"
91
+ elsif value == :none
92
+ 'no-repeat'
83
93
  else
84
94
  value
85
95
  end
86
96
  end
87
97
 
88
98
  def border(opts={})
89
- value = border_value(opts)
90
- if only = opts[:only]
91
- if only.is_a?(Array)
92
- str = ""
93
- only.each do |type|
94
- str << property( "border-#{type}", value )
99
+ if opts.is_a?(Hash)
100
+ value = border_value(opts)
101
+ if only = opts[:only]
102
+ if only.is_a?(Array)
103
+ str = ""
104
+ only.each do |type|
105
+ str << property( "border-#{type}", value )
106
+ end
107
+ str
108
+ else
109
+ property "border-#{only}", value
95
110
  end
96
- str
97
111
  else
98
- property "border-#{only}", value
112
+ property "border", value
99
113
  end
100
114
  else
101
- property "border", value
115
+ property "border", opts
102
116
  end
103
117
  end
104
118
 
@@ -116,7 +130,9 @@ module StyleTrain
116
130
 
117
131
  def text(opts)
118
132
  str = ""
119
- str << property('font-family', opts[:font]) if opts[:font]
133
+ if family = opts[:font] || opts[:family]
134
+ str << property('font-family', family)
135
+ end
120
136
  str << property('font-weight', opts[:weight]) if opts[:weight]
121
137
  str << property('font-variant', opts[:variant]) if opts[:variant]
122
138
  str << property('font-style', opts[:style]) if opts[:style]
@@ -124,7 +140,9 @@ module StyleTrain
124
140
  str << property('color', opts[:color]) if opts[:color]
125
141
  str << property('text-direction', opts[:direction]) if opts[:direction]
126
142
  str << property('letter-spacing', opts[:spacing]) if opts[:spacing]
127
- str << property('line-height', opts[:line_height]) if opts[:line_height]
143
+ if height = opts[:line_height] || opts[:height]
144
+ str << property('line-height', height)
145
+ end
128
146
  str << property('text-align', opts[:align]) if opts[:align]
129
147
  str << property('text-decoration', opts[:decoration]) if opts[:decoration]
130
148
  str << property('text-indent', opts[:indent]) if opts[:indent]
@@ -135,6 +153,8 @@ module StyleTrain
135
153
  str
136
154
  end
137
155
 
156
+ alias :font :text
157
+
138
158
  def list(opts)
139
159
  str = ""
140
160
  str << property('list-style-image', opts[:image]) if opts[:image]
@@ -143,9 +163,12 @@ module StyleTrain
143
163
  str
144
164
  end
145
165
 
146
- def margin(opts)
166
+ def margin(*opts)
167
+ opts = opts.size == 1 ? opts.first : opts
147
168
  if opts.is_a?(Array)
148
169
  property('margin', opts)
170
+ elsif opts.is_a?(String)
171
+ property('margin', opts)
149
172
  else
150
173
  str = ""
151
174
  str << property('margin-left', opts[:left]) if opts[:left]
@@ -156,9 +179,12 @@ module StyleTrain
156
179
  end
157
180
  end
158
181
 
159
- def padding(opts)
182
+ def padding(*opts)
183
+ opts = opts.size == 1 ? opts.first : opts
160
184
  if opts.is_a?(Array)
161
185
  property('padding', opts)
186
+ elsif opts.is_a?(String)
187
+ property('padding', opts)
162
188
  else
163
189
  str = ""
164
190
  str << property('padding-left', opts[:left]) if opts[:left]
@@ -214,7 +240,7 @@ module StyleTrain
214
240
  end
215
241
 
216
242
  [
217
- 'color', 'display', 'float', 'clear', 'visibility', 'cursor',
243
+ 'color', 'display', 'float', 'clear', 'visibility', 'cursor', 'opacity',
218
244
  'height', 'width', 'max_height', 'max_width', 'min_height', 'min_width'
219
245
  ].each do |meth|
220
246
  class_eval <<-RUBY
@@ -1,23 +1,23 @@
1
1
  class Fixnum
2
- def px
3
- "#{self}px"
4
- end
5
-
6
- def em
7
- "#{self}em"
2
+ ['px', 'em', 'pt'].each do |meth|
3
+ class_eval <<-RUBY
4
+ def #{meth}
5
+ self.to_s + "#{meth}"
6
+ end
7
+ RUBY
8
8
  end
9
9
 
10
10
  def percent
11
11
  "#{self}%"
12
12
  end
13
-
14
- def pt
15
- "#{self}pt"
16
- end
17
13
  end
18
14
 
19
15
  class Float
20
- def em
21
- "#{self}em"
16
+ ['em', 'pt'].each do |meth|
17
+ class_eval <<-RUBY
18
+ def #{meth}
19
+ self.to_s + "#{meth}"
20
+ end
21
+ RUBY
22
22
  end
23
23
  end
data/spec/numbers_spec.rb CHANGED
@@ -22,4 +22,8 @@ describe Float do
22
22
  it '#em converts to a string with em at the end' do
23
23
  1.5.em.should == '1.5em'
24
24
  end
25
+
26
+ it '#pt converts to a string with % at the end' do
27
+ 12.5.pt.should == '12.5pt'
28
+ end
25
29
  end
data/spec/sheet_spec.rb CHANGED
@@ -3,6 +3,21 @@ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
3
3
  Sheet = StyleTrain::Sheet unless defined?( Sheet )
4
4
 
5
5
  describe Sheet do
6
+ class StyleSheet < StyleTrain::Sheet
7
+ def content
8
+ body {
9
+ background :color => '#666'
10
+ text :font => 'verdana'
11
+ }
12
+
13
+ style('#wrapper'){
14
+ background :color => :white
15
+ margin [1.em, :auto]
16
+ }
17
+ end
18
+ end
19
+
20
+
6
21
  describe 'attributes' do
7
22
  before :all do
8
23
  @sheet = Sheet.new
@@ -27,8 +42,17 @@ describe Sheet do
27
42
  @sheet = Sheet.new
28
43
  end
29
44
 
45
+ it '#header describes the file generation' do
46
+ StyleSheet.new.header.should == <<-CSS
47
+ /*
48
+ Generated by StlyeTrain CSS generator via the class StyleSheet
49
+ */
50
+ CSS
51
+ end
52
+
30
53
  it 'resets the output' do
31
- @sheet.should_receive(:output=).with('')
54
+ @sheet.stub(:header).and_return('HEADER')
55
+ @sheet.should_receive(:output=).with('HEADER')
32
56
  @sheet.render
33
57
  end
34
58
 
@@ -166,7 +190,7 @@ describe Sheet do
166
190
  end
167
191
 
168
192
  it ':image will create a background-image property' do
169
- @sheet.background(:image => "url('ok.png')").should include "background-image: url('ok.png');"
193
+ @sheet.background(:image => "ok.png").should include "background-image: url('ok.png');"
170
194
  end
171
195
 
172
196
  it ':position will create a background-position property' do
@@ -181,6 +205,10 @@ describe Sheet do
181
205
  @sheet.background(:repeat => :x).should include "background-repeat: repeat-x;"
182
206
  end
183
207
 
208
+ it ':repeat with :none will make a no-repeat value' do
209
+ @sheet.background(:repeat => :none).should include 'background-repeat: no-repeat'
210
+ end
211
+
184
212
  it 'will create a background-color property if it receives a string that converts to a color'
185
213
  it 'will create a background-color property if it receives a color'
186
214
  end
@@ -211,6 +239,10 @@ describe Sheet do
211
239
  'border-bottom: 1px solid black', 'border-left: 1px solid black'
212
240
  )
213
241
  end
242
+
243
+ it 'should do :none' do
244
+ @sheet.border(:none).should include 'border: none'
245
+ end
214
246
  end
215
247
 
216
248
  describe 'outline' do
@@ -358,6 +390,10 @@ describe Sheet do
358
390
  @sheet.text(:line_height => 2).should include 'line-height: 2'
359
391
  end
360
392
 
393
+ it 'uses alt syntax for line height' do
394
+ @sheet.text(:height => 2).should include 'line-height: 2'
395
+ end
396
+
361
397
  it 'aligns' do
362
398
  @sheet.text(:align => :center).should include 'text-align: center'
363
399
  end
@@ -385,6 +421,14 @@ describe Sheet do
385
421
  it 'specifies word spacing' do
386
422
  @sheet.text(:word_spacing => 25.px).should include 'word-spacing: 25px'
387
423
  end
424
+
425
+ it 'aliases to #font' do
426
+ @sheet.font(:word_spacing => 15.px).should include 'word-spacing: 15px'
427
+ end
428
+
429
+ it 'uses the :family option as well as the :font to specify font face' do
430
+ @sheet.font(:family => 'Serif').should include 'font-family: Serif'
431
+ end
388
432
  end
389
433
 
390
434
  describe 'list' do
@@ -406,6 +450,14 @@ describe Sheet do
406
450
  @sheet.margin( [10.px, 20.px, 13.px] ).should include 'margin: 10px 20px 13px'
407
451
  end
408
452
 
453
+ it 'takes an arguments as an array' do
454
+ @sheet.margin( 15.px, 20.px ).should include 'margin: 15px 20px'
455
+ end
456
+
457
+ it 'takes a single argument instead of an array' do
458
+ @sheet.margin( 25.px ).should include 'margin: 25px'
459
+ end
460
+
409
461
  it 'takes :left and just makes that declaration' do
410
462
  @sheet.margin( :left => 30.px ).should include 'margin-left: 30px'
411
463
  end
@@ -428,6 +480,15 @@ describe Sheet do
428
480
  @sheet.padding( [10.px, 20.px, 13.px] ).should include 'padding: 10px 20px 13px'
429
481
  end
430
482
 
483
+ it 'takes an arguments as an array' do
484
+ @sheet.padding( 15.px, 20.px ).should include 'padding: 15px 20px'
485
+ end
486
+
487
+ it 'takes a single argument instead of an array' do
488
+ @sheet.padding( 25.px ).should include 'padding: 25px'
489
+ end
490
+
491
+
431
492
  it 'takes :left and just makes that declaration' do
432
493
  @sheet.padding( :left => 30.px ).should include 'padding-left: 30px'
433
494
  end
@@ -512,6 +573,10 @@ describe Sheet do
512
573
  it 'visibility' do
513
574
  @sheet.visibility(:hidden).should include 'visibility: hidden'
514
575
  end
576
+
577
+ it 'opacity' do
578
+ @sheet.opacity(0.5).should include 'opacity: 0.5'
579
+ end
515
580
  end
516
581
  end
517
582
 
@@ -689,20 +754,6 @@ describe Sheet do
689
754
  end
690
755
  end
691
756
 
692
- class StyleSheet < StyleTrain::Sheet
693
- def content
694
- body {
695
- background :color => '#666'
696
- text :font => 'verdana'
697
- }
698
-
699
- style('#wrapper'){
700
- background :color => :white
701
- margin [1.em, :auto]
702
- }
703
- end
704
- end
705
-
706
757
  describe 'subclassing' do
707
758
  it 'works' do
708
759
  StyleSheet.render.should include(
@@ -749,4 +800,27 @@ CSS
749
800
  File.exists?(@dir + 'foo')
750
801
  end
751
802
  end
803
+
804
+ describe 'alternative syntax' do
805
+ # Available operators for overloading
806
+ # |, ^, &, <=>, ==, ===, =~, >, >=, <, <=, +, -, *, /, %, **, <<, >>, ~, +@, -@, [], []= (2 args)
807
+
808
+ # input[:readonly]{ ... }
809
+ # input['type=text']{ ... }
810
+ # div :input { ... }
811
+
812
+
813
+ # how to handle :hover and other psuedo selectors
814
+
815
+ # o :create_button {
816
+ #
817
+ # o :hover {
818
+ #
819
+ # }
820
+
821
+ # < :active {
822
+ #
823
+ # }
824
+ # }
825
+ end
752
826
  end
data/style_train.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{style_train}
8
- s.version = "0.2.3"
8
+ s.version = "0.2.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Kane Baccigalupi"]
12
- s.date = %q{2011-02-23}
12
+ s.date = %q{2011-02-25}
13
13
  s.description = %q{style_train builds CSS using pure Ruby, not a DSL interpreted via Ruby. This allows inheritance, modules, instance level calculations and all the goodness Ruby can offer.}
14
14
  s.email = %q{baccigalupi@gmail.com}
15
15
  s.extra_rdoc_files = [
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: style_train
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 3
10
- version: 0.2.3
9
+ - 4
10
+ version: 0.2.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kane Baccigalupi
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-02-23 00:00:00 -08:00
18
+ date: 2011-02-25 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency