bunch 0.0.4 → 0.0.5

Sign up to get free protection for your applications and to get access to all the features.
data/lib/bunch.rb CHANGED
@@ -12,30 +12,49 @@ rescue LoadError
12
12
  end
13
13
 
14
14
  require 'bunch/version'
15
+ require 'bunch/rack'
16
+ require 'bunch/caching'
15
17
  require 'bunch/directory_node'
16
18
  require 'bunch/file_node'
17
19
  require 'bunch/coffee_node'
18
20
  require 'bunch/sass_node'
19
- require 'bunch/rack'
20
21
 
21
22
  module Bunch
22
23
  end
23
24
 
24
25
  class << Bunch
25
- def Tree(fn)
26
+ def content_for(path)
27
+ tree_for(normalized_path(path)).content
28
+ end
29
+
30
+ def tree_for(path)
26
31
  case
27
- when !File.exist?(fn)
28
- raise Errno::ENOENT, fn
29
- when File.directory?(fn)
30
- Bunch::DirectoryNode.new(fn)
31
- when fn =~ /\.coffee$/
32
- Bunch::CoffeeNode.new(fn)
33
- when fn =~ /\.s(a|c)ss$/
34
- Bunch::SassNode.new(fn)
32
+ when File.directory?(path)
33
+ Bunch::DirectoryNode.new(path)
34
+ when path =~ /\.coffee$/
35
+ Bunch::CoffeeNode.new(path)
36
+ when path =~ /\.s(a|c)ss$/
37
+ Bunch::SassNode.new(path)
35
38
  else
36
- Bunch::FileNode.new(fn)
39
+ Bunch::FileNode.new(path)
37
40
  end
38
41
  end
42
+
43
+ private
44
+ def normalized_path(path)
45
+ case
46
+ when File.exist?(path)
47
+ path
48
+ when File.exist?(chopped_path = path.sub(%r(\.[^.]*$), ''))
49
+ chopped_path
50
+ when File.basename(path).start_with?('all.') && File.exist?(dir_path = File.dirname(path))
51
+ dir_path
52
+ when (path_w_different_extension = Dir["#{chopped_path}.*"].first)
53
+ path_w_different_extension
54
+ else
55
+ raise Errno::ENOENT, path.to_s
56
+ end
57
+ end
39
58
  end
40
59
 
41
60
  #class Module
@@ -0,0 +1,42 @@
1
+ module Bunch
2
+ module Caching
3
+ class << self
4
+ def initialize_cache
5
+ @cache = Hash.new { |h, k| h[k] = {} }
6
+ end
7
+
8
+ def stored?(fn)
9
+ @cache.keys.include?(fn)
10
+ end
11
+
12
+ def read(fn)
13
+ @cache[fn][:content]
14
+ end
15
+
16
+ def mtime(fn)
17
+ @cache[fn][:mtime]
18
+ end
19
+
20
+ def write(fn, mtime, content)
21
+ @cache[fn] = {:mtime => mtime, :content => content}
22
+ end
23
+
24
+ def fetch(fn, &blk)
25
+ current_mtime = File.mtime(fn)
26
+
27
+ if stored?(fn) && mtime(fn) == current_mtime
28
+ read(fn)
29
+ else
30
+ content = blk.call
31
+ write(fn, current_mtime, content)
32
+ content
33
+ end
34
+ end
35
+ end
36
+ initialize_cache
37
+
38
+ def fetch(filename, &blk)
39
+ Bunch::Caching.fetch(filename, &blk)
40
+ end
41
+ end
42
+ end
data/lib/bunch/cli.rb CHANGED
@@ -25,7 +25,7 @@ module Bunch
25
25
  end
26
26
 
27
27
  def generate_files
28
- tree = Bunch::Tree(@input.to_s)
28
+ tree = Bunch.tree_for(@input.to_s)
29
29
 
30
30
  if @output
31
31
  FileUtils.mkdir_p(@output.to_s)
@@ -33,22 +33,22 @@ module Bunch
33
33
 
34
34
  if @opts[:all]
35
35
  if @output
36
- write("all.#{tree.target_extension}", tree.contents)
36
+ write("all.#{tree.target_extension}", tree.content)
37
37
  else
38
- puts tree.contents
38
+ puts tree.content
39
39
  end
40
40
  end
41
41
 
42
42
  if @opts[:individual]
43
43
  tree.children.each do |child|
44
- write("#{child.name}.#{child.target_extension}", child.contents)
44
+ write("#{child.name}.#{child.target_extension}", child.content)
45
45
  end
46
46
  end
47
47
  end
48
48
 
49
49
  private
50
- def write(fn, contents)
51
- File.open(@output.join(fn), 'w') { |f| f.write(contents) }
50
+ def write(fn, content)
51
+ File.open(@output.join(fn), 'w') { |f| f.write(content) }
52
52
  end
53
53
  end
54
54
 
@@ -1,5 +1,7 @@
1
1
  module Bunch
2
2
  class CoffeeNode
3
+ include Caching
4
+
3
5
  def initialize(fn)
4
6
  unless defined?(@@coffee_required)
5
7
  require 'coffee-script'
@@ -10,8 +12,8 @@ module Bunch
10
12
  raise "'gem install coffee-script' to compile .coffee files."
11
13
  end
12
14
 
13
- def contents
14
- @contents ||= CoffeeScript.compile(File.read(@filename), :bare => false)
15
+ def content
16
+ @content ||= fetch(@filename) { CoffeeScript.compile(File.read(@filename), :bare => false) }
15
17
  end
16
18
 
17
19
  def name
@@ -12,7 +12,7 @@ module Bunch
12
12
 
13
13
  def children
14
14
  @children ||= begin
15
- children = filenames.map &Bunch.method(:Tree)
15
+ children = filenames.map &Bunch.method(:tree_for)
16
16
  ordering_file = @root.join('_.yml')
17
17
 
18
18
  if File.exist?(ordering_file)
@@ -36,8 +36,8 @@ module Bunch
36
36
  end
37
37
  end
38
38
 
39
- def contents
40
- @contents ||= children.map(&:contents).join
39
+ def content
40
+ @content ||= children.map(&:content).join
41
41
  end
42
42
 
43
43
  def name
@@ -13,7 +13,7 @@ module Bunch
13
13
  end
14
14
  end
15
15
 
16
- def contents
16
+ def content
17
17
  File.read(@filename)
18
18
  end
19
19
 
data/lib/bunch/rack.rb CHANGED
@@ -15,7 +15,7 @@ module Bunch
15
15
  path = @root.join(env['PATH_INFO'].sub(/^\//, '')).to_s
16
16
  type = MIME::Types.type_for(path).first || 'text/plain'
17
17
 
18
- [200, headers(type), [generate(path)]]
18
+ [200, headers(type), [content_for(path)]]
19
19
  rescue => e
20
20
  [500, headers('text/plain'), [error_log(e)]]
21
21
  end
@@ -25,23 +25,13 @@ module Bunch
25
25
  @headers.merge('Content-Type' => mime_type.to_s)
26
26
  end
27
27
 
28
- def generate(path)
29
- case
30
- when File.exist?(path)
31
- contents(path)
32
- when File.exist?(chopped_path = path.sub(%r(\.[^.]*$), ''))
33
- contents(chopped_path)
34
- when File.basename(path).start_with?('all.')
35
- contents(File.dirname(path))
36
- when (path_w_different_extension = Dir["#{chopped_path}.*"].first)
37
- contents(path_w_different_extension)
38
- else
39
- raise Errno::ENOENT, path.to_s
40
- end
28
+ private
29
+ def content_for(path)
30
+ Bunch.content_for(path)
41
31
  end
42
32
 
43
- def contents(path)
44
- Bunch::Tree(path.to_s).contents
33
+ def headers(mime_type)
34
+ @headers.merge('Content-Type' => mime_type.to_s)
45
35
  end
46
36
 
47
37
  def error_log(e)
@@ -1,5 +1,7 @@
1
1
  module Bunch
2
2
  class SassNode
3
+ include Caching
4
+
3
5
  def initialize(fn)
4
6
  unless defined?(@@sass_required)
5
7
  require 'sass'
@@ -10,8 +12,8 @@ module Bunch
10
12
  raise "'gem install sass' to compile .sass and .scss files."
11
13
  end
12
14
 
13
- def contents
14
- @contents ||= Sass::Engine.for_file(@filename, {}).render
15
+ def content
16
+ @content ||= fetch(@filename) { Sass::Engine.for_file(@filename, {}).render }
15
17
  end
16
18
 
17
19
  def name
data/lib/bunch/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Bunch
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: bunch
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-01-02 00:00:00.000000000 Z
12
+ date: 2012-01-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: mime-types
16
- requirement: &70349040409660 !ruby/object:Gem::Requirement
16
+ requirement: &70263668761740 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70349040409660
24
+ version_requirements: *70263668761740
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: slop
27
- requirement: &70349040409240 !ruby/object:Gem::Requirement
27
+ requirement: &70263668761320 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :runtime
34
34
  prerelease: false
35
- version_requirements: *70349040409240
35
+ version_requirements: *70263668761320
36
36
  description: Directory-structure-based asset bundling.
37
37
  email:
38
38
  - rfitz@academia.edu
@@ -65,6 +65,7 @@ files:
65
65
  - example/js/test4/test4b.coffee
66
66
  - example/js/test4/test4c.coffee
67
67
  - lib/bunch.rb
68
+ - lib/bunch/caching.rb
68
69
  - lib/bunch/cli.rb
69
70
  - lib/bunch/coffee_node.rb
70
71
  - lib/bunch/directory_node.rb