Class: MagLove::Asset::Theme

Inherits:
Object
  • Object
show all
Includes:
Helper::LogHelper
Defined in:
lib/maglove/asset/theme.rb

Constant Summary collapse

OUTPUT_MAPPING =
{
  "haml" => "html",
  "less" => "css",
  "scss" => "css",
  "coffee" => "js",
  "js" => "js",
  "yml" => "json"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper::LogHelper

#debug, #error, #error!, #info, #logger

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
41
# File 'lib/maglove/asset/theme.rb', line 26

def initialize(path, options = {})
  @path = path
  @options = options
  @options[:theme] = Maglove.theme.identifier
  @mtime = File.mtime(absolute_path)
  begin
    if ::Tilt[input_type]
      template = ::Tilt.new(absolute_path)
      @contents = template.render(Object.new, @options.merge(base_path: "src/base/#{Maglove.theme.base_version}/"))
    else
      @contents = File.read(absolute_path)
    end
  rescue Maglove::Engine::RenderError => e
    error("▸ HAML error: #{e.message}")
  end
end

Instance Attribute Details

#contentsObject (readonly)

Returns the value of attribute contents.



15
16
17
# File 'lib/maglove/asset/theme.rb', line 15

def contents
  @contents
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



15
16
17
# File 'lib/maglove/asset/theme.rb', line 15

def mtime
  @mtime
end

#optionsObject (readonly)

Returns the value of attribute options.



15
16
17
# File 'lib/maglove/asset/theme.rb', line 15

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



15
16
17
# File 'lib/maglove/asset/theme.rb', line 15

def path
  @path
end

#validObject (readonly)

Returns the value of attribute valid.



15
16
17
# File 'lib/maglove/asset/theme.rb', line 15

def valid
  @valid
end

Instance Method Details

#absolute_pathObject



70
71
72
73
74
75
76
# File 'lib/maglove/asset/theme.rb', line 70

def absolute_path
  if @options[:base]
    Maglove.theme.base_dir.file(path).absolute_path
  else
    Maglove.theme.src_dir.file(path).absolute_path
  end
end

#input_typeObject



43
44
45
# File 'lib/maglove/asset/theme.rb', line 43

def input_type
  File.extname(path).delete("\.")
end

#logical_pathObject



78
79
80
81
82
83
84
85
86
# File 'lib/maglove/asset/theme.rb', line 78

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_pathObject



88
89
90
91
# File 'lib/maglove/asset/theme.rb', line 88

def output_path
  return false unless valid?
  "dist/themes/#{@options[:theme]}/#{logical_path}"
end

#output_typeObject



47
48
49
# File 'lib/maglove/asset/theme.rb', line 47

def output_type
  OUTPUT_MAPPING[input_type] or input_type
end

#valid?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/maglove/asset/theme.rb', line 51

def valid?
  !contents.nil?
end

#write!Object



55
56
57
# File 'lib/maglove/asset/theme.rb', line 55

def write!
  write_to!(output_path)
end

#write_to!(path) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/maglove/asset/theme.rb', line 59

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