Class: JsSourcemap::Env

Inherits:
Object
  • Object
show all
Defined in:
lib/js-sourcemap/env.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeEnv

Returns a new instance of Env.



9
10
11
12
13
14
15
# File 'lib/js-sourcemap/env.rb', line 9

def initialize
  self.sources_dir = rails_path "public#{Rails.application.config.assets.prefix}"
  self.sourcemap_yml = rails_path "config/sourcemap.yml"
  self.mapping_dir = rails_path "#{sourcemap_config.fetch "mapping_directory"}#{Rails.application.config.assets.prefix}"
  self.domain = sourcemap_config.fetch "domain"
  self.manifest = get_manifest_file
end

Instance Attribute Details

#domainObject

Returns the value of attribute domain.



6
7
8
# File 'lib/js-sourcemap/env.rb', line 6

def domain
  @domain
end

#is_build_dir_privateObject

Returns the value of attribute is_build_dir_private.



6
7
8
# File 'lib/js-sourcemap/env.rb', line 6

def is_build_dir_private
  @is_build_dir_private
end

#manifestObject

Returns the value of attribute manifest.



6
7
8
# File 'lib/js-sourcemap/env.rb', line 6

def manifest
  @manifest
end

#mapping_dirObject

Returns the value of attribute mapping_dir.



6
7
8
# File 'lib/js-sourcemap/env.rb', line 6

def mapping_dir
  @mapping_dir
end

#sourcemap_ymlObject

Returns the value of attribute sourcemap_yml.



6
7
8
# File 'lib/js-sourcemap/env.rb', line 6

def sourcemap_yml
  @sourcemap_yml
end

#sources_dirObject

Returns the value of attribute sources_dir.



6
7
8
# File 'lib/js-sourcemap/env.rb', line 6

def sources_dir
  @sources_dir
end

Instance Method Details

#build_absolute_path(path) ⇒ Object



57
58
59
60
61
# File 'lib/js-sourcemap/env.rb', line 57

def build_absolute_path(path)
    path = path.gsub(/.*(#{self.sources_dir}\/|#{self.mapping_dir}\/)/,'')
    path = "#{Rails.application.config.assets.prefix}/#{path}"
    "#{self.domain}#{path}"
end

#config_pathObject



21
22
23
# File 'lib/js-sourcemap/env.rb', line 21

def config_path
  @config_path ||= Pathname.new(self.sourcemap_yml)
end

#get_manifest_fileObject



45
46
47
48
49
50
51
52
53
54
55
# File 'lib/js-sourcemap/env.rb', line 45

def get_manifest_file
  return @manifest_hash if @manifest_hash
  files = Dir[File.join(self.sources_dir,"manifest*")]
  if files and files.length == 1
    file = File.read(files.first)
    @manifest_hash = JSON.parse(file)["files"]
  else
    puts "Either none or more than one minifest file exists"
  end

end

#load_configObject



29
30
31
32
33
34
# File 'lib/js-sourcemap/env.rb', line 29

def load_config
  return {} if not readable?
  File.open config_path do |f|
    YAML.load f.read
  end
end

#rails_path(path) ⇒ Object



41
42
43
# File 'lib/js-sourcemap/env.rb', line 41

def rails_path(path)
  "#{Rails.root}/#{path}"
end

#readable?Boolean

Returns:

  • (Boolean)


25
26
27
# File 'lib/js-sourcemap/env.rb', line 25

def readable?
  config_path.readable?
end

#relative_path(path) ⇒ Object



36
37
38
39
# File 'lib/js-sourcemap/env.rb', line 36

def relative_path(path)
  dir = File.dirname(__FILE__)
  File.expand_path path, dir
end

#sourcemap_configObject



17
18
19
# File 'lib/js-sourcemap/env.rb', line 17

def sourcemap_config
  @sourcemap_config ||= load_config
end