Class: MagLove::Asset::Theme
- Inherits:
-
Object
- Object
- MagLove::Asset::Theme
- Includes:
- Helper::LogHelper, Workspace
- Defined in:
- lib/maglove/asset/theme.rb
Constant Summary collapse
- OUTPUT_MAPPING =
{ "haml" => "html", "less" => "css", "scss" => "css", "coffee" => "js", "yml" => "json" }
Instance Attribute Summary collapse
-
#contents ⇒ Object
readonly
Returns the value of attribute contents.
-
#mtime ⇒ Object
readonly
Returns the value of attribute mtime.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#valid ⇒ Object
readonly
Returns the value of attribute valid.
Instance Method Summary collapse
- #absolute_path ⇒ Object
-
#initialize(path, options = {}) ⇒ Theme
constructor
A new instance of Theme.
- #input_type ⇒ Object
- #logical_path ⇒ Object
- #output_path ⇒ Object
- #output_type ⇒ Object
- #valid? ⇒ Boolean
- #write! ⇒ Object
- #write_to!(path) ⇒ Object
Methods included from Helper::LogHelper
#debug, #error, #error!, #info, #logger
Methods included from Workspace
#gem_dir, #theme_base_dir, #theme_base_file, #theme_config, #theme_dir, #theme_file, #workspace_dir, #workspace_file
Constructor Details
#initialize(path, options = {}) ⇒ Theme
Returns a new instance of Theme.
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/maglove/asset/theme.rb', line 26 def initialize(path, = {}) @path = path = @mtime = File.mtime(absolute_path) begin if ::Tilt[input_type] template = ::Tilt.new(absolute_path) @contents = template.render(Object.new, .merge(base_path: theme_base_dir.to_s)) else @contents = File.read(absolute_path) end rescue StandardError => e error("▸ #{e.message}") end end |
Instance Attribute Details
#contents ⇒ Object (readonly)
Returns the value of attribute contents.
16 17 18 |
# File 'lib/maglove/asset/theme.rb', line 16 def contents @contents end |
#mtime ⇒ Object (readonly)
Returns the value of attribute mtime.
16 17 18 |
# File 'lib/maglove/asset/theme.rb', line 16 def mtime @mtime end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
16 17 18 |
# File 'lib/maglove/asset/theme.rb', line 16 def end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
16 17 18 |
# File 'lib/maglove/asset/theme.rb', line 16 def path @path end |
#valid ⇒ Object (readonly)
Returns the value of attribute valid.
16 17 18 |
# File 'lib/maglove/asset/theme.rb', line 16 def valid @valid end |
Instance Method Details
#absolute_path ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/maglove/asset/theme.rb', line 69 def absolute_path if [:base] File.absolute_path("src/base/#{theme_config(:base_version)}/#{path}") else File.absolute_path("src/themes/#{@options[:theme]}/#{path}") end end |
#input_type ⇒ Object
42 43 44 |
# File 'lib/maglove/asset/theme.rb', line 42 def input_type File.extname(path).delete("\.") end |
#logical_path ⇒ Object
77 78 79 80 81 82 83 84 85 |
# File 'lib/maglove/asset/theme.rb', line 77 def logical_path return false unless valid? dirname = File.dirname(path) if dirname == "/" "#{File.basename(path, '.*')}.#{output_type}" else "#{dirname}/#{File.basename(path, '.*')}.#{output_type}" end end |
#output_path ⇒ Object
87 88 89 90 |
# File 'lib/maglove/asset/theme.rb', line 87 def output_path return false unless valid? "dist/themes/#{@options[:theme]}/#{logical_path}" end |
#output_type ⇒ Object
46 47 48 |
# File 'lib/maglove/asset/theme.rb', line 46 def output_type OUTPUT_MAPPING[input_type] or input_type end |
#valid? ⇒ Boolean
50 51 52 |
# File 'lib/maglove/asset/theme.rb', line 50 def valid? !contents.nil? end |
#write! ⇒ Object
54 55 56 |
# File 'lib/maglove/asset/theme.rb', line 54 def write! write_to!(output_path) end |
#write_to!(path) ⇒ Object
58 59 60 61 62 63 64 65 66 67 |
# File 'lib/maglove/asset/theme.rb', line 58 def write_to!(path) return false unless valid? FileUtils.mkdir_p(File.dirname(path)) File.open("#{path}+", 'wb') { |f| f.write @contents } FileUtils.mv("#{path}+", path) File.utime(mtime, mtime, path) true ensure FileUtils.rm("#{path}+") if File.exist?("#{path}+") end |