lib/cytoplasm.rb in cytoplasm-0.2.3 vs lib/cytoplasm.rb in cytoplasm-0.2.4

- old
+ new

@@ -2,26 +2,22 @@ require 'cytoplasm/railtie' require 'yaml' require 'active_support/core_ext/hash/deep_merge' module Cytoplasm - # List of all Cytoplasm configuration settings, with their corresponding default values. - # Create a YAML file at config/initializers/cytoplasm.yml to extend the default settings. - @conf = { - :jqueryui => { - :version => "1.9.2", - :theme => "ui-darkness" - }, - :fontloader => { - :directory => "fontsquirrel", - :googlewebfonts_apikey => "AIzaSyDs7hjZSILIAN3T4oFv3qf_DCyy6PfC30E" - } - } - # List of all Cytoplasm variables, with their corresponding default values. - # Create a YAML file at config/initializers/cytoplasm.vars.yml to extend the default values. - @vars = { + # Create a YAML file at config/cytoplasm-config.yml to extend the default values. + @defaults = { + :setup => { + :jqueryui => { + :version => "1.9.2", + :theme => "ui-darkness" + }, + :fontloader => { + :googlewebfonts_apikey => "AIzaSyDs7hjZSILIAN3T4oFv3qf_DCyy6PfC30E" + } + }, :layout => { :mode => "fluid", :padding => "0px", :header => { :margin => "0px", @@ -112,10 +108,11 @@ } } } } + @vars = @defaults @cssvars = "" # Dependencies @dependencies = { :css => [ @@ -137,47 +134,25 @@ 'cytoLogo' ] }; def initialize - configure_with("config/initializers/cytoplasm.yml") - load_vars_from("config/initializers/cytoplasm.vars.yml") + load_vars_from("config/cytoplasm-config.yml") end - # Configure through hash - def self.configure(opts={}) - opts.each {|k,v| @conf[k.to_sym] = v if @valid_settings.include? k.to_sym} - end def self.load_vars(opts={}) - opts = optsToSym(opts) - @vars.deep_merge!(opts) - #opts.each {|k,v| @vars[k.to_sym] = v if @valid_vars.include? k.to_sym} + @vars = @defaults + @vars.deep_merge!(opts_to_sym(opts)) end - def self.optsToSym(opts) + def self.opts_to_sym(opts) fixed = {} - unless opts.is_a? Hash - return opts - else - opts.each do |k,v| - fixed[k.to_sym] = ((v.is_a?(Hash)) ? optsToSym(v) : v) - end - end + return opts unless opts.is_a? Hash + opts.each {|k,v| fixed[k.to_sym] = ((v.is_a?(Hash)) ? opts_to_sym(v) : v)} return fixed end - # Configure through yaml file - def self.configure_with(path_to_yaml_file) - begin - conf = YAML::load(IO.read(path_to_yaml_file)) - rescue Errno::ENOENT - puts "YAML configuration file couldn't be found. Using defaults."; return - rescue Psych::SyntaxError - puts "YAML configuration file contains invalid syntax. Using defaults."; return - end - configure(conf) - end def self.load_vars_from(path_to_yaml_file) begin vars = YAML::load(IO.read(path_to_yaml_file)) rescue Errno::ENOENT puts "YAML configuration file couldn't be found. Using defaults."; return @@ -186,60 +161,51 @@ end load_vars(vars) compile_css() end + def self.save_config(opts={}) + puts "SAVING CONFIGURATION FILE" + load_vars(opts) + File.open("config/cytoplasm-config.yml", 'w+') {|f| f.write(@vars.to_yaml) } + end + # Getters and setters - def self.config(which=nil,value=nil) - return @conf if which.nil? + def self.traverse_hash(hash,which=nil,value=nil) + return hash if which.nil? if which.is_a? String and which.include? '.' - returnvar = @conf + returnvar = hash levels = [] which.split(".").each do |level| levels << level returnvar = returnvar[level.to_sym] unless returnvar[level.to_sym].nil? end else - returnvar = @conf[which.to_sym] + returnvar = hash[which.to_sym] end unless returnvar.nil? if value.nil? - return returnvar + return returnvar.to_s else - @conf[which.to_sym] = value + if which.include? "." + + else + hash[which.to_sym] = value + end end end end def self.variable(which=nil,value=nil) - return @vars if which.nil? - if which.is_a? String and which.include? '.' - returnvar = @vars - levels = [] - which.split(".").each do |level| - levels << level - returnvar = returnvar[level.to_sym] unless returnvar[level.to_sym].nil? - end - else - returnvar = @vars[which.to_sym] - end - - unless returnvar.nil? - if value.nil? - return returnvar - else - @vars[which.to_sym] = value - end - end + traverse_hash(@vars,which,value) end - # Shortcuts to the above - def self.conf(which=nil,value=nil) - return config(which,value) - end def self.vars(which=nil,value=nil) - return variable(which,value) + variable(which,value) end + def self.defaults(which=nil,value=nil) + traverse_hash(@defaults,which,value) + end # Dependency loaders def self.dependencies(type="*") deps = [] valid_types = ["*","css","js"] @@ -252,20 +218,15 @@ deps << d end end end when "css" - deps << "http://ajax.googleapis.com/ajax/libs/jqueryui/"+conf("jqueryui.version")+"/themes/"+conf("jqueryui.theme")+"/jquery-ui.css" + deps << "http://ajax.googleapis.com/ajax/libs/jqueryui/"+vars("setup.jqueryui.version")+"/themes/"+vars("setup.jqueryui.theme")+"/jquery-ui.css" deps += load_fonts() - #deps << "cytoplasm/cytoplasm-"+Time.now.to_i.to_s - @dependencies[:css].each do |stylesheet| - deps << "cytoplasm/"+stylesheet - end + @dependencies[:css].each {|stylesheet| deps << "cytoplasm/"+stylesheet} when "js" - @dependencies[:js].each do |plugin| - deps << "cytoplasm/"+plugin - end + @dependencies[:js].each {|plugin| deps << "cytoplasm/"+plugin} end return deps end def self.deps(type="*") return dependencies(type) @@ -285,23 +246,19 @@ Dir.chdir("../") end File.open(varfile,"wb") do |f| css = "" - @vars.each do |k,v| - css += hash_to_css(k,v) - end + @vars.each {|k,v| css += hash_to_css(k,v)} f.write(css) end end def self.hash_to_css(k,v,parent="") output = "" if v.is_a? Hash parent += "#{k}_" - v.each do |kk,vv| - output += hash_to_css(kk,vv,parent) - end + v.each {|kk,vv| output += hash_to_css(kk,vv,parent)} return output else k = k.to_s v = v.to_s output = "" \ No newline at end of file