Class: Evax

Inherits:
Object
  • Object
show all
Defined in:
lib/evax.rb,
lib/evax/version.rb

Defined Under Namespace

Modules: CssMinifier, Logger

Constant Summary collapse

VERSION =
"0.0.15"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_file = "config/assets.yml", relative_path = "public/assets") ⇒ Evax

Returns a new instance of Evax.



12
13
14
15
16
17
18
# File 'lib/evax.rb', line 12

def initialize( config_file = "config/assets.yml", relative_path = "public/assets" )
  @config_file   = File.expand_path( config_file )
  @relative_path = File.expand_path( relative_path )

  Evax::Logger.log "Config File: #{self.config_file}"
  Evax::Logger.log "Relative Path: #{self.relative_path}"
end

Instance Attribute Details

#config_fileObject (readonly)

Returns the value of attribute config_file.



10
11
12
# File 'lib/evax.rb', line 10

def config_file
  @config_file
end

#relative_pathObject (readonly)

Returns the value of attribute relative_path.



10
11
12
# File 'lib/evax.rb', line 10

def relative_path
  @relative_path
end

Class Method Details

.compress_css(css_string) ⇒ Object



90
91
92
# File 'lib/evax.rb', line 90

def self.compress_css( css_string )
  Evax::CssMinifier.build( css_string )
end

.compress_js(js_string) ⇒ Object



85
86
87
88
# File 'lib/evax.rb', line 85

def self.compress_js( js_string )
  opts = { :copyright => false }
  Uglifier.compile( js_string, opts )
end

Instance Method Details

#buildObject



39
40
41
42
# File 'lib/evax.rb', line 39

def build
  build_js  if js_configured?
  build_css if css_configured?
end

#build_css(group_names = []) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
# File 'lib/evax.rb', line 63

def build_css( group_names=[] )
  groups = config["stylesheets"]
  groups.select!{|k, v| group_names.include? k } if group_names.any?

  groups.each_key do |group_name|
    result_string = join( :stylesheets, group_name )
    result_string = Evax.compress_css( result_string ) if config["compress"]

    write_output( "#{group_name}.css", result_string )
  end
end

#build_js(group_names = []) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/evax.rb', line 51

def build_js( group_names=[] )
  groups = config["javascripts"]
  groups.select!{|k, v| group_names.include? k } if group_names.any?

  groups.each_key do |group_name|
    result_string = join( :javascripts, group_name )
    result_string = Evax.compress_js( result_string ) if config["compress"]

    write_output( "#{group_name}.js", result_string )
  end
end

#configObject



28
29
30
31
# File 'lib/evax.rb', line 28

def config
  default_opts = { "compress" => true }
  default_opts.merge(YAML::load_file( @config_file ))
end

#join(type, group_name) ⇒ Object



33
34
35
36
37
# File 'lib/evax.rb', line 33

def join( type, group_name )
  config[type.to_s][group_name].map do |file_path|
    File.read( File.join(relative_path, file_path) )
  end.join( "\n" )
end

#run_as_daemonObject



24
25
26
# File 'lib/evax.rb', line 24

def run_as_daemon
  watch
end

#run_onceObject



20
21
22
# File 'lib/evax.rb', line 20

def run_once
  build
end

#watchObject



44
45
46
47
48
49
# File 'lib/evax.rb', line 44

def watch
  script = Watchr::Script.new
  script.watch( asset_files_regex(:javascripts) ) {|asset| build_js( js_groups_for( asset[0] ) ) } if js_configured?
  script.watch( asset_files_regex(:stylesheets) ) {|asset| build_css( css_groups_for( asset[0] ) ) } if css_configured?
  Watchr::Controller.new( script, Watchr.handler.new ).run
end

#write_output(file_name, string) ⇒ Object



75
76
77
78
79
80
81
82
83
# File 'lib/evax.rb', line 75

def write_output( file_name, string )
  path      = File.expand_path( File.join( relative_path, config['output_path'] ) )
  file_path = File.join( path, file_name )

  Evax::Logger.log "Writing file: #{file_path}"

  FileUtils.mkdir_p path
  File.open( file_path, 'w' ) { |f| f.write string }
end