style_train 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/Rakefile CHANGED
@@ -19,6 +19,7 @@ begin
19
19
  gem.email = "baccigalupi@gmail.com"
20
20
  gem.homepage = "http://github.com/baccigalupi/style_train"
21
21
  gem.authors = ["Kane Baccigalupi"]
22
+ gem.add_dependency('activesupport', '>= 3.0.0')
22
23
  end
23
24
  Jeweler::GemcutterTasks.new
24
25
  rescue LoadError
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -230,5 +230,19 @@ module StyleTrain
230
230
  def self.render(render_method=:content)
231
231
  new.render(render_method)
232
232
  end
233
+
234
+ def self.export opts={}
235
+ new.export opts
236
+ end
237
+
238
+ def export opts={}
239
+ name = file_name(opts[:file_name])
240
+ render opts[:render_method] || :content
241
+ File.open("#{StyleTrain.dir}/#{file_name}.css", 'w'){ |f| f.write(output) }
242
+ end
243
+
244
+ def file_name name=nil
245
+ name || self.class.to_s.underscore
246
+ end
233
247
  end
234
248
  end
@@ -0,0 +1,37 @@
1
+ module StyleTrain
2
+ class Theme
3
+ attr_accessor :value_hash, :name
4
+
5
+ def self.required_keys *args
6
+ if args.empty?
7
+ @required_keys ||= []
8
+ else
9
+ @required_keys = args.map{ |v| v.to_sym }
10
+ end
11
+ end
12
+
13
+ def self.themes
14
+ @themes ||= Gnash.new
15
+ end
16
+
17
+ def initialize(name, value_hash)
18
+ raise ArgumentError, "Unique name is required as first argument" if !name || self.class.themes[name]
19
+ missing_keys = self.class.required_keys - value_hash.keys
20
+ raise ArgumentError, "Required keys not provided: #{missing_keys.map{|k| k.inspect}.join(", ")}" if !missing_keys.empty?
21
+ self.value_hash = Gnash.new(value_hash)
22
+ self.name = name
23
+ self.class.themes[name] = self
24
+ end
25
+
26
+ def [](key)
27
+ value_hash[key]
28
+ end
29
+
30
+ def []=(key, value)
31
+ if (value.nil? || value.to_s.empty?) && self.class.required_keys.include?(key.to_sym)
32
+ raise "Cannot set a required key to nothing"
33
+ end
34
+ value_hash[key] = value
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,35 @@
1
+ module StyleTrain
2
+ class ThemedSheet < Sheet
3
+ def self.themes(klass=nil)
4
+ if klass
5
+ if !klass.ancestors.include?(StyleTrain::Theme)
6
+ raise ArgumentError, "themes must be a StyleTrain::Theme class"
7
+ end
8
+ @themes = klass
9
+ else
10
+ raise ArgumentError, "No themes class is defined. Please add one with the class method #themes" unless @themes
11
+ @themes
12
+ end
13
+ end
14
+
15
+ def theme
16
+ @theme || self.class.themes.themes[:default] || raise( "No theme has been set" )
17
+ end
18
+
19
+ def theme=(key)
20
+ (@theme = self.class.themes.themes[key]) || raise( ArgumentError, "Theme #{key.inspect} not found in #{self.class.themes}")
21
+ end
22
+
23
+ def self.export
24
+ sheet = new
25
+ themes.themes.keys.each do |key|
26
+ sheet.theme = key
27
+ sheet.export
28
+ end
29
+ end
30
+
31
+ def file_name name=nil
32
+ (name || self.class.to_s.underscore) + "_#{theme.name.to_s.underscore}"
33
+ end
34
+ end
35
+ end
data/lib/style_train.rb CHANGED
@@ -1,13 +1,16 @@
1
1
  $LOAD_PATH.unshift( File.dirname(__FILE__) )
2
2
 
3
- # ruby libs
4
- require 'singleton'
5
-
6
3
  # Support stuff
4
+ require 'active_support/inflector'
7
5
  require 'style_train/support/gnash'
8
- require 'style_train/support/string'
9
6
  require 'style_train/support/numbers'
10
7
 
8
+ module StyleTrain
9
+ class << self
10
+ attr_accessor :dir
11
+ end
12
+ end
13
+
11
14
  # The color load!
12
15
  require 'style_train/color_types/color_type'
13
16
  require 'style_train/color_types/rgb_color'
@@ -15,8 +18,8 @@ require 'style_train/color_types/hsl_color'
15
18
  require 'style_train/color_types/keyword_color'
16
19
  require 'style_train/color_types/hex_color'
17
20
  require "style_train/color"
18
- # require 'lib/palette'
19
21
 
22
+ # Big Picture Stuff, sheets, themes, etc
20
23
  require "style_train/sheet"
21
- # require 'set'
22
- # require 'style'
24
+ require 'style_train/theme'
25
+ require 'style_train/themed_sheet'
File without changes
data/spec/sheet_spec.rb CHANGED
@@ -514,22 +514,22 @@ describe Sheet do
514
514
  end
515
515
  end
516
516
  end
517
+
518
+ class StyleSheet < StyleTrain::Sheet
519
+ def content
520
+ body {
521
+ background :color => '#666'
522
+ text :font => 'verdana'
523
+ }
524
+
525
+ style('#wrapper'){
526
+ background :color => :white
527
+ margin [1.em, :auto]
528
+ }
529
+ end
530
+ end
517
531
 
518
532
  describe 'subclassing' do
519
- class StyleSheet < StyleTrain::Sheet
520
- def content
521
- body {
522
- background :color => '#666'
523
- text :font => 'verdana'
524
- }
525
-
526
- style('#wrapper'){
527
- background :color => :white
528
- margin [1.em, :auto]
529
- }
530
- end
531
- end
532
-
533
533
  it 'works' do
534
534
  StyleSheet.render.should include(
535
535
  <<-CSS
@@ -546,4 +546,33 @@ CSS
546
546
  )
547
547
  end
548
548
  end
549
+
550
+ describe 'export' do
551
+ before :all do
552
+ @dir = File.dirname(__FILE__) + "/generated_files"
553
+ StyleTrain.dir = @dir
554
+ Dir.chdir(@dir)
555
+ end
556
+
557
+ before do
558
+ Dir.glob('*.css').each do |file|
559
+ File.delete(file)
560
+ end
561
+ end
562
+
563
+ it 'exports to the StyleTrain dir location' do
564
+ StyleSheet.export
565
+ Dir.glob('*.css').size.should == 1
566
+ end
567
+
568
+ it 'uses the class name underscored by default' do
569
+ StyleSheet.export
570
+ File.exist?(@dir + '/style_sheet.css').should == true
571
+ end
572
+
573
+ it 'uses an alternate name when provided' do
574
+ StyleSheet.new.export(:file_name => 'foo')
575
+ File.exists?(@dir + 'foo')
576
+ end
577
+ end
549
578
  end
@@ -0,0 +1,8 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ describe StyleTrain, 'configuration' do
4
+ it 'has a css directory' do
5
+ StyleTrain.dir = '/foo'
6
+ StyleTrain.dir.should == '/foo'
7
+ end
8
+ end
@@ -0,0 +1,80 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ Theme = StyleTrain::Theme unless defined?( Theme )
4
+
5
+ describe Theme do
6
+ class MyTheme < Theme
7
+ required_keys :foo, :bar
8
+ end
9
+
10
+ describe 'Theme.required_keys' do
11
+ it 'stores the values' do
12
+ MyTheme.instance_eval('@required_keys').should == [:foo, :bar]
13
+ end
14
+
15
+ it 'retreives the keys when no argument is supplied' do
16
+ MyTheme.required_keys.should == [:foo, :bar]
17
+ end
18
+ end
19
+
20
+ describe 'initialize' do
21
+ it 'should work if all the right arguments are provided' do
22
+ lambda { MyTheme.new(:default, :foo => '#666', :bar => 'white') }.should_not raise_error
23
+ end
24
+
25
+ it 'requires a name' do
26
+ lambda { MyTheme.new(nil, :foo => '#666', :bar => 'white') }.should raise_error(
27
+ ArgumentError, "Unique name is required as first argument"
28
+ )
29
+ end
30
+
31
+ it 'requires a unique name' do
32
+ MyTheme.new(:orange, :foo => 'orange', :bar => 'white')
33
+ lambda{ MyTheme.new(:orange, :foo => 'orange', :bar => 'cream') }.should raise_error(
34
+ ArgumentError, "Unique name is required as first argument"
35
+ )
36
+ end
37
+
38
+ it 'requires a values hash contains required keys' do
39
+ lambda { MyTheme.new(:red, :foo => 'red') }.should raise_error(
40
+ ArgumentError, "Required keys not provided: :bar"
41
+ )
42
+ end
43
+
44
+ it 'adds the theme to the themes hash' do
45
+ theme = MyTheme.new(:yellow, :foo => 'lightyellow', :bar => 'white')
46
+ MyTheme.themes[:yellow].should == theme
47
+ end
48
+
49
+ it 'sets the value hash to value_hash accessor' do
50
+ theme = MyTheme.new(:maroon, :foo => 'maroon', :bar => 'white')
51
+ theme.value_hash.should == Gnash.new({:foo => 'maroon', :bar => 'white'})
52
+ end
53
+ end
54
+
55
+ describe 'usage' do
56
+ before :all do
57
+ @theme = MyTheme.new(:blue, :foo => 'blue', :bar => 'white', :extra => 'lightyellow')
58
+ end
59
+
60
+ it 'values can be accessed via []' do
61
+ @theme[:foo].should == 'blue'
62
+ @theme[:bar].should == 'white'
63
+ @theme[:extra].should == 'lightyellow'
64
+ end
65
+
66
+ it 'values can be set with []=' do
67
+ @theme[:bar] = 'cream'
68
+ @theme[:bar].should == 'cream'
69
+ end
70
+
71
+ it 'raises an error if a required key is set to nil' do
72
+ lambda{ @theme[:foo] = nil }.should raise_error("Cannot set a required key to nothing")
73
+ lambda{ @theme[:extra] = nil }.should_not raise_error
74
+ end
75
+
76
+ it 'raises an error if a required key is set to a blank string' do
77
+ lambda{ @theme[:foo] = '' }.should raise_error("Cannot set a required key to nothing")
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,111 @@
1
+ require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
+
3
+ ThemedSheet = StyleTrain::ThemedSheet unless defined?( ThemedSheet )
4
+
5
+ describe ThemedSheet do
6
+ class FooeyTheme < StyleTrain::Theme
7
+ required_keys :foo, :bar
8
+ end
9
+
10
+ FooeyTheme.new(:default, {
11
+ :foo => '#666',
12
+ :bar => 'white'
13
+ })
14
+
15
+ FooeyTheme.new(:inverted, {
16
+ :foo => '#CCC',
17
+ :bar => 'black'
18
+ })
19
+
20
+ class FooeySheet < ThemedSheet
21
+ themes FooeyTheme
22
+
23
+ def content
24
+ style(:fooey){
25
+ color theme[:foo]
26
+ background :color => theme[:bar]
27
+ }
28
+ end
29
+ end
30
+
31
+ describe "self.themes" do
32
+ it 'has a setter' do
33
+ ThemedSheet.instance_eval("@themes").should == nil
34
+ ThemedSheet.themes FooeyTheme
35
+ ThemedSheet.instance_eval("@themes").should == FooeyTheme
36
+ end
37
+
38
+ it 'requires that it be set with a Theme class' do
39
+ lambda{ ThemedSheet.themes String }.should raise_error(
40
+ ArgumentError, "themes must be a StyleTrain::Theme class"
41
+ )
42
+ end
43
+
44
+ it 'returns the class when no argument is given' do
45
+ ThemedSheet.themes FooeyTheme
46
+ ThemedSheet.themes.should == FooeyTheme
47
+ end
48
+
49
+ it 'raises an error when no class exists' do
50
+ ThemedSheet.instance_eval "@themes = nil"
51
+ lambda{ ThemedSheet.themes }.should raise_error(
52
+ ArgumentError, "No themes class is defined. Please add one with the class method #themes"
53
+ )
54
+ end
55
+ end
56
+
57
+ describe '#theme' do
58
+ before :all do
59
+ @sheet = FooeySheet.new
60
+ end
61
+
62
+ it 'throws an error if a theme class is not defined' do
63
+ ThemedSheet.instance_eval "@themes = nil"
64
+ sheet = ThemedSheet.new
65
+ lambda {sheet.theme}.should raise_error(
66
+ ArgumentError, "No themes class is defined. Please add one with the class method #themes"
67
+ )
68
+ end
69
+
70
+ it 'will get the default theme if none is found' do
71
+ @sheet.theme.should == FooeyTheme.themes[:default]
72
+ end
73
+
74
+ it 'can be set via any name in the theme class' do
75
+ @sheet.theme = :inverted
76
+ @sheet.theme.should == FooeyTheme.themes[:inverted]
77
+ end
78
+
79
+ it 'will raise an error if the name is not found in the class' do
80
+ lambda{ @sheet.theme = :not_in_fooey }.should raise_error(
81
+ ArgumentError, "Theme :not_in_fooey not found in FooeyTheme"
82
+ )
83
+ end
84
+ end
85
+
86
+ describe 'export' do
87
+ before :all do
88
+ @dir = File.dirname(__FILE__) + "/generated_files"
89
+ StyleTrain.dir = @dir
90
+ Dir.chdir(@dir)
91
+ end
92
+
93
+ before do
94
+ Dir.glob('*.css').each do |file|
95
+ File.delete(file)
96
+ end
97
+ end
98
+
99
+ it 'exports a file for each theme in the theme class' do
100
+ Dir.glob('*.css').size.should == 0
101
+ FooeySheet.export
102
+ Dir.glob('*.css').size.should == 2
103
+ end
104
+
105
+ it 'uses the theme name when constructing the css file name' do
106
+ FooeySheet.export
107
+ File.exist?(@dir + '/fooey_sheet_default.css').should == true
108
+ File.exist?(@dir + '/fooey_sheet_inverted.css').should == true
109
+ end
110
+ end
111
+ end
data/style_train.gemspec CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{style_train}
8
- s.version = "0.1.0"
8
+ s.version = "0.2.0"
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"]
@@ -30,16 +30,21 @@ Gem::Specification.new do |s|
30
30
  "lib/style_train/sheet.rb",
31
31
  "lib/style_train/support/gnash.rb",
32
32
  "lib/style_train/support/numbers.rb",
33
- "lib/style_train/support/string.rb",
33
+ "lib/style_train/theme.rb",
34
+ "lib/style_train/themed_sheet.rb",
34
35
  "spec/color/color_spec.rb",
35
36
  "spec/color/color_type_spec.rb",
36
37
  "spec/color/hex_color_spec.rb",
37
38
  "spec/color/keyword_color_spec.rb",
38
39
  "spec/color/rgb_color_spec.rb",
40
+ "spec/generated_files/.gitkeep",
39
41
  "spec/numbers_spec.rb",
40
42
  "spec/sheet_spec.rb",
41
43
  "spec/spec.opts",
42
44
  "spec/spec_helper.rb",
45
+ "spec/style_train_spec.rb",
46
+ "spec/theme_spec.rb",
47
+ "spec/themed_sheet_spec.rb",
43
48
  "style_train.gemspec",
44
49
  "utils/alexch_color_gist/color.rb",
45
50
  "utils/alexch_color_gist/color_test.rb",
@@ -58,7 +63,10 @@ Gem::Specification.new do |s|
58
63
  "spec/color/rgb_color_spec.rb",
59
64
  "spec/numbers_spec.rb",
60
65
  "spec/sheet_spec.rb",
61
- "spec/spec_helper.rb"
66
+ "spec/spec_helper.rb",
67
+ "spec/style_train_spec.rb",
68
+ "spec/theme_spec.rb",
69
+ "spec/themed_sheet_spec.rb"
62
70
  ]
63
71
 
64
72
  if s.respond_to? :specification_version then
@@ -66,9 +74,12 @@ Gem::Specification.new do |s|
66
74
  s.specification_version = 3
67
75
 
68
76
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
77
+ s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0"])
69
78
  else
79
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
70
80
  end
71
81
  else
82
+ s.add_dependency(%q<activesupport>, [">= 3.0.0"])
72
83
  end
73
84
  end
74
85
 
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: 27
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
8
+ - 2
9
9
  - 0
10
- version: 0.1.0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Kane Baccigalupi
@@ -17,8 +17,23 @@ cert_chain: []
17
17
 
18
18
  date: 2011-02-17 00:00:00 -08:00
19
19
  default_executable:
20
- dependencies: []
21
-
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: activesupport
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
22
37
  description: 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.
23
38
  email: baccigalupi@gmail.com
24
39
  executables: []
@@ -42,16 +57,21 @@ files:
42
57
  - lib/style_train/sheet.rb
43
58
  - lib/style_train/support/gnash.rb
44
59
  - lib/style_train/support/numbers.rb
45
- - lib/style_train/support/string.rb
60
+ - lib/style_train/theme.rb
61
+ - lib/style_train/themed_sheet.rb
46
62
  - spec/color/color_spec.rb
47
63
  - spec/color/color_type_spec.rb
48
64
  - spec/color/hex_color_spec.rb
49
65
  - spec/color/keyword_color_spec.rb
50
66
  - spec/color/rgb_color_spec.rb
67
+ - spec/generated_files/.gitkeep
51
68
  - spec/numbers_spec.rb
52
69
  - spec/sheet_spec.rb
53
70
  - spec/spec.opts
54
71
  - spec/spec_helper.rb
72
+ - spec/style_train_spec.rb
73
+ - spec/theme_spec.rb
74
+ - spec/themed_sheet_spec.rb
55
75
  - style_train.gemspec
56
76
  - utils/alexch_color_gist/color.rb
57
77
  - utils/alexch_color_gist/color_test.rb
@@ -100,3 +120,6 @@ test_files:
100
120
  - spec/numbers_spec.rb
101
121
  - spec/sheet_spec.rb
102
122
  - spec/spec_helper.rb
123
+ - spec/style_train_spec.rb
124
+ - spec/theme_spec.rb
125
+ - spec/themed_sheet_spec.rb
@@ -1,24 +0,0 @@
1
- # From ActiveSupport
2
-
3
- unless String.instance_methods.include?( 'constantize' )
4
- class String
5
- # Constantize tries to find a declared constant with the name specified
6
- # in the string. It raises a NameError when the name is not in CamelCase
7
- # or is not initialized.
8
- #
9
- # @example
10
- # "Module".constantize #=> Module
11
- # "Class".constantize #=> Class
12
- def constantize
13
- unless /\A(?:::)?([A-Z]\w*(?:::[A-Z]\w*)*)\z/ =~ self
14
- raise NameError, "#{self.inspect} is not a valid constant name!"
15
- end
16
-
17
- Object.module_eval("::#{$1}", __FILE__, __LINE__)
18
- end
19
-
20
- def dasherize
21
- self.gsub('_', '-') || self
22
- end
23
- end
24
- end