Browse files

more refactoring - now with importer

  • Loading branch information...
1 parent 55883f8 commit 71f635e758c0e0c460f3ce83ab3aef627028883b @kristianmandrup committed May 24, 2012
View
4 lib/generators/strapon/ricco/ricco_generator.rb
@@ -56,6 +56,10 @@ def after_generate
puts ""
puts "#{'*'*70}"
end
+
+ protected
+
+ include Strapon::Stylesheets::Builder
end
end
end
View
40 lib/strapon/stylesheets/builder.rb
@@ -1,31 +1,33 @@
require 'strapon/file_repository'
module Strapon
- module StylesheetsBuilder
- def copy_stylesheets
- FileRepository.new.build.stylesheets.each do |stylesheet|
- copy_stylesheet stylesheet.path, stylesheet.name
+ module Stylesheets
+ module Builder
+ def copy_stylesheets
+ FileRepository.new.build.stylesheets.each_pair do |folder, stylesheet|
+ copy_stylesheet stylesheet
+ end
end
- end
- def copy_stylesheet path, name
- copy_file stylesheet_path(:src, path), stylesheet_path(:target, path)
- end
+ def copy_stylesheet stylesheet
+ copy_file stylesheet_path(:src, stylesheet), stylesheet_path(:target, stylesheet)
+ end
- def stylesheet_path type, path
- File.join send("#{type}_stylesheet_dir"), path, "_#{name}.#{stylesheet_ext}"
- end
+ def stylesheet_path type, stylesheet
+ File.join send("#{type}_stylesheet_dir"), stylesheet.path, "_#{stylesheet.name}.#{stylesheet_ext}"
+ end
- def stylesheet_ext
- 'css.sass'
- end
+ def stylesheet_ext
+ 'css.sass'
+ end
- def src_stylesheet_dir
- 'stylesheets/site/'
- end
+ def src_stylesheet_dir
+ 'stylesheets/site/'
+ end
- def target_stylesheet_dir
- File.join "app/assets/", stylesheet_src_dir
+ def target_stylesheet_dir
+ File.join "app/assets/", stylesheet_src_dir
+ end
end
end
end
View
78 lib/strapon/stylesheets/file_repository.rb
@@ -1,49 +1,55 @@
require 'strapon/stylesheets/stylesheet'
-class FileRepository
- # TODO: Should load css files from stylesheets.yml
- attr_reader :stylesheets
+module Strapon
+ module Stylesheets
+ class FileRepository
+ def initialize path = nil
+ @path = path unless path.blank?
+ end
- def initialize path = nil
- @path = path unless path.blank?
- end
+ def stylesheets
+ @stylesheets ||= {}
+ end
- def load_stylesheets
- @load_stylesheets ||= Hashie::Mash.new(YAML.load path).stylesheets
- end
+ def load_stylesheets
+ @load_stylesheets ||= Hashie::Mash.new(YAML.load path).stylesheets
+ end
- def build
- load_stylesheets.each_pair do |key, value|
- case value
- when String
- add_stylesheet "#{value}_#{key}", key
- when Hash
- build_from_hash key, value
+ def build
+ load_stylesheets.each_pair do |key, value|
+ case value
+ when String
+ add_stylesheet key, "#{value}_#{key}", key
+ when Hash
+ build_from_hash key, value
+ end
+ end
+ self
end
- end
- self
- end
- def build_from_hash key, hash
- index = hash[:index]
- raise ArgumentError, "The entry #{key}: is missing index:" unless index
- files = get_files(hash).compact
- raise ArgumentError, "The entry #{key}: is missing file: or files: key" if files.empty?
- files.flatten.each do |file|
- add_stylesheet "#{index}_#{file}", file
- end
- end
+ def build_from_hash key, hash
+ index = hash[:index]
+ raise ArgumentError, "The entry #{key}: is missing index:" unless index
+ files = get_files(hash).compact
+ raise ArgumentError, "The entry #{key}: is missing file: or files: key" if files.empty?
+ files.flatten.each do |file|
+ add_stylesheet key, "#{index}_#{file}", file
+ end
+ end
- def get_files hash
- [hash[:file] || hash[:files]]
- end
+ def get_files hash
+ [hash[:file] || hash[:files]]
+ end
- def add_stylesheet path, name
- stylesheets << StyleSheet.new path, name
- end
+ def add_stylesheet key, path, name
+ stylesheets[key] ||= []
+ stylesheets[key] << StyleSheet.new path, name
+ end
- def path
- @path ||= File.dirname(__FILE__) + '/stylesheet_index.yml'
+ def path
+ @path ||= File.dirname(__FILE__) + '/stylesheet_index.yml'
+ end
+ end
end
end
View
37 lib/strapon/stylesheets/importer.rb
@@ -0,0 +1,37 @@
+module Strapon
+ module Stylesheets
+ # Use in application.css.sass.erb
+ # <%= Strapon::Stylesheets::Importer.import_styles %>
+ class Importer
+ def initialize path = nil
+ @path = path unless path.blank?
+ end
+
+ def import_styles
+ folder_index.map do |folder|
+ import_statements folder
+ end.join("\n")
+ end
+
+ protected
+
+ def import_statements folder
+ stylesheets[folder].map do |stylesheet|
+ "@import #{stylesheet.import_path}"
+ end.join("\n")
+ end
+
+ def folder_index
+ Yaml.load(path)['index']
+ end
+
+ def path
+ @path ||= Rails.root.join 'app/assets/stylesheets/', 'index.yml'
+ end
+
+ def stylesheets
+ @stylesheets ||= FileRepository.new.build.stylesheets
+ end
+ end
+ end
+end
View
4 lib/strapon/stylesheets/stylesheet.rb
@@ -4,4 +4,8 @@ class Stylesheet
def initialize name, path
@name, @path = [name, path]
end
+
+ def import_path
+ File.join path, name
+ end
end

0 comments on commit 71f635e

Please sign in to comment.