Class: MagLove::Asset::Theme

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, options = {})
  @path = path
  @options = options
  @mtime = File.mtime(absolute_path)
  begin
    if ::Tilt[input_type]
      template = ::Tilt.new(absolute_path)
      @contents = template.render(Object.new, @options.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

#contentsObject (readonly)

Returns the value of attribute contents.



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

def contents
  @contents
end

#mtimeObject (readonly)

Returns the value of attribute mtime.



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

def mtime
  @mtime
end

#optionsObject (readonly)

Returns the value of attribute options.



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

def options
  @options
end

#pathObject (readonly)

Returns the value of attribute path.



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

def path
  @path
end

#validObject (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_pathObject



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

def absolute_path
  if @options[:base]
    File.absolute_path("src/base/#{theme_config(:base_version)}/#{path}")
  else
    File.absolute_path("src/themes/#{@options[:theme]}/#{path}")
  end
end

#input_typeObject



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

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

#logical_pathObject



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_pathObject



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_typeObject



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

def output_type
  OUTPUT_MAPPING[input_type] or input_type
end

#valid?Boolean

Returns:

  • (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