gakubuchi 1.1.0 → 1.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: de0621a2c4aefec2689206b915624fdd2125bcbe
4
- data.tar.gz: a475393b99e9491be9e527f97becebc1b77f26b9
3
+ metadata.gz: 7c899d677ecc5feddbca366ee1b59e5ecc22e307
4
+ data.tar.gz: 2db0642f6fc120915d464ff07f01392d81d2aa12
5
5
  SHA512:
6
- metadata.gz: 2536c299852e56ccd4a6598b5753f2c08e4a61297a8cbe50545c80ba5eb0a15f305e31179f2e26199aa9e4df41faa2b68e8d0998679d5f587ee40b78012d22e1
7
- data.tar.gz: 51a8b358d5d9f26886189378e591eb0557a1eeb14af5c3993d4af950a6f1da144ccdea6ce562563f55dafb7c15577e3fffab7f64efccd70ff7d49cf142cd1a17
6
+ metadata.gz: cc51f9de01fa55a1c0db624963e46271f8787de54cf23e9bb03d0a7a286c44fef7fce0461584325ed749ec75b35b1d04fb5465966d332f8a584b456cf8433a6a
7
+ data.tar.gz: 90c16f8304742a31e13a9291261bf9f6fe313aafdc6ae46fc85559b459261fb4327ab9dab3d0c31942e9a16a148b06cef7669f78561a46f0a7d03856c92c12cb
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright 2015 yasaichi
1
+ Copyright 2015 Yuichi Goto
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
@@ -0,0 +1,27 @@
1
+ module Gakubuchi
2
+ class EngineRegistrar
3
+ def initialize(env)
4
+ @env = env
5
+ end
6
+
7
+ def register(target, engine)
8
+ klass = constantize(engine)
9
+ return false if !klass.instance_of?(::Class) || registered?(target)
10
+
11
+ @env.register_engine(target, klass)
12
+ true
13
+ end
14
+
15
+ def registered?(target)
16
+ @env.engines.key?(::Sprockets::Utils.normalize_extension(target))
17
+ end
18
+
19
+ private
20
+
21
+ def constantize(klass)
22
+ klass.to_s.constantize
23
+ rescue ::NameError
24
+ nil
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,5 @@
1
+ module Gakubuchi
2
+ class Error < StandardError
3
+ InvalidTemplate = Class.new(self)
4
+ end
5
+ end
@@ -1,8 +1,10 @@
1
1
  module Gakubuchi
2
2
  class Railtie < ::Rails::Railtie
3
- initializer 'gakubuchi.assets.precompile' do
4
- TemplateEngine.new('Slim::Template').register!('.slim')
5
- TemplateEngine.new('Tilt::HamlTemplate').register!('.haml')
3
+ config.assets.configure do |env|
4
+ engine_registrar = EngineRegistrar.new(env)
5
+
6
+ engine_registrar.register(:haml, '::Tilt::HamlTemplate')
7
+ engine_registrar.register(:slim, '::Slim::Template')
6
8
  end
7
9
 
8
10
  rake_tasks do
@@ -8,10 +8,10 @@ module Gakubuchi
8
8
 
9
9
  def execute!
10
10
  templates.each do |template|
11
- src = template.precompiled_pathname
11
+ src = template.digest_path
12
12
  next if src.nil?
13
13
 
14
- dest = template.destination_pathname
14
+ dest = template.destination_path
15
15
  FileUtils.copy_p(src, dest)
16
16
 
17
17
  FileUtils.remove(src) unless leave_digest_named_templates?
@@ -2,54 +2,63 @@ module Gakubuchi
2
2
  class Template
3
3
  extend ::Forwardable
4
4
 
5
- attr_reader :pathname
6
- def_delegator :@pathname, :hash
5
+ attr_reader :source_path, :extname
6
+ def_delegators :source_path, :basename, :hash
7
7
 
8
8
  %w(== === eql?).each do |method_name|
9
9
  define_method(method_name) do |other|
10
- self.class == other.class && @pathname.__send__(method_name, other.pathname)
10
+ self.class == other.class && source_path.public_send(method_name, other.source_path)
11
11
  end
12
12
  end
13
13
 
14
14
  def self.all
15
- ::Dir.glob(root.join('**/*.html*')).map { |path| new(path) }
15
+ ::Dir.glob(root.join('**/*.html*')).map { |source_path| new(source_path) }
16
16
  end
17
17
 
18
18
  def self.root
19
19
  ::Rails.root.join('app/assets', ::Gakubuchi.configuration.template_directory)
20
20
  end
21
21
 
22
- def initialize(path)
23
- @pathname = ::Pathname.new(path)
24
- end
22
+ def initialize(source_path)
23
+ path = ::Pathname.new(source_path)
24
+ root = self.class.root
25
+
26
+ @extname = extract_extname(path)
27
+ @source_path = path.absolute? ? path : root.join(path)
25
28
 
26
- def destination_pathname
27
- dirname = relative_pathname.dirname
28
- ::Rails.public_path.join(dirname, "#{relative_pathname.basename(extname)}.html")
29
+ case
30
+ when !@extname.include?('html')
31
+ fail Error::InvalidTemplate, 'source path must refer to a template file'
32
+ when !@source_path.fnmatch?(root.join('*').to_s)
33
+ fail Error::InvalidTemplate, "template must exist in #{root}"
34
+ end
29
35
  end
30
36
 
31
- def extname
32
- extnames = []
33
- basename_without_ext = pathname.basename
37
+ def destination_path
38
+ ::Rails.public_path.join(logical_path)
39
+ end
34
40
 
35
- loop do
36
- extname = basename_without_ext.extname
37
- break if extname.empty?
41
+ def digest_path
42
+ resolved_path = view_context.asset_digest_path(logical_path.to_s)
43
+ return if resolved_path.nil?
38
44
 
39
- extnames.unshift(extname)
40
- basename_without_ext = basename_without_ext.basename(extname)
41
- end
45
+ ::Pathname.new(::File.join(::Rails.public_path, view_context.assets_prefix, resolved_path))
46
+ end
42
47
 
43
- extnames.join
48
+ def logical_path
49
+ dirname = source_path.relative_path_from(self.class.root).dirname
50
+ ::Pathname.new(dirname).join("#{basename(extname)}.html")
44
51
  end
45
52
 
46
- def precompiled_pathname
47
- asset = ::Rails.application.assets.find_asset(relative_pathname)
48
- ::Rails.public_path.join('assets', asset.digest_path) if asset
53
+ private
54
+
55
+ def extract_extname(path)
56
+ extname = path.extname
57
+ extname.empty? ? extname : "#{extract_extname(path.basename(extname))}#{extname}"
49
58
  end
50
59
 
51
- def relative_pathname
52
- pathname.relative_path_from(self.class.root)
60
+ def view_context
61
+ @view_context ||= ::ActionView::Base.new
53
62
  end
54
63
  end
55
64
  end
@@ -1,3 +1,3 @@
1
1
  module Gakubuchi
2
- VERSION = '1.1.0'
2
+ VERSION = '1.2.0'
3
3
  end
data/lib/gakubuchi.rb CHANGED
@@ -4,14 +4,15 @@ require 'logger'
4
4
  require 'active_support/configurable'
5
5
 
6
6
  require 'gakubuchi/configuration'
7
+ require 'gakubuchi/error'
7
8
  require 'gakubuchi/fileutils'
8
9
  require 'gakubuchi/task'
9
10
  require 'gakubuchi/version'
10
11
 
11
12
  if defined?(::Rails::Railtie) && defined?(::Sprockets::Railtie)
12
13
  require 'pathname'
14
+ require 'gakubuchi/engine_registrar'
13
15
  require 'gakubuchi/template'
14
- require 'gakubuchi/template_engine'
15
16
  require 'gakubuchi/railtie'
16
17
  end
17
18
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gakubuchi
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - yasaichi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-01-14 00:00:00.000000000 Z
11
+ date: 2016-01-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: sprockets-rails
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - "~>"
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: '2.0'
33
+ version: 2.0.0
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - "~>"
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: '2.0'
40
+ version: 2.0.0
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: ammeter
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -190,11 +190,12 @@ files:
190
190
  - Rakefile
191
191
  - lib/gakubuchi.rb
192
192
  - lib/gakubuchi/configuration.rb
193
+ - lib/gakubuchi/engine_registrar.rb
194
+ - lib/gakubuchi/error.rb
193
195
  - lib/gakubuchi/fileutils.rb
194
196
  - lib/gakubuchi/railtie.rb
195
197
  - lib/gakubuchi/task.rb
196
198
  - lib/gakubuchi/template.rb
197
- - lib/gakubuchi/template_engine.rb
198
199
  - lib/gakubuchi/version.rb
199
200
  - lib/generators/gakubuchi/install/install_generator.rb
200
201
  - lib/generators/gakubuchi/install/templates/gakubuchi.rb.erb
@@ -1,29 +0,0 @@
1
- module Gakubuchi
2
- class TemplateEngine
3
- extend ::Forwardable
4
-
5
- attr_reader :klass
6
- alias_method :engine, :klass
7
-
8
- def_delegator '::Rails.application', :assets
9
- def_delegator '::Sprockets::Utils', :normalize_extension
10
- private :assets, :normalize_extension
11
-
12
- def initialize(engine)
13
- @klass = engine.to_s.constantize rescue nil
14
- end
15
-
16
- def register!(extname)
17
- if engine.instance_of?(::Class) && !registered?(extname)
18
- !!assets.register_engine(extname, engine)
19
- else
20
- false
21
- end
22
- end
23
-
24
- def registered?(extname)
25
- ext = normalize_extension(extname)
26
- assets.engines.key?(ext) && assets.engines[ext] == engine
27
- end
28
- end
29
- end