Class: SprocketsFS::SprocketsDir

Inherits:
Object
  • Object
show all
Includes:
FromHash
Defined in:
lib/sprockets_fs.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#mount_dirObject

Returns the value of attribute mount_dir.



19
20
21
# File 'lib/sprockets_fs.rb', line 19

def mount_dir
  @mount_dir
end

#parent_dirsObject

Returns the value of attribute parent_dirs.



19
20
21
# File 'lib/sprockets_fs.rb', line 19

def parent_dirs
  @parent_dirs
end

Instance Method Details

#can_write?(path) ⇒ Boolean

Returns:

  • (Boolean)


130
131
132
133
134
135
136
137
138
# File 'lib/sprockets_fs.rb', line 130

def can_write?(path)
  #base = convert_to_absolute("#{path[1..-1]}.coffee",false) || convert_to_absolute("#{path[1..-1]}.erb",false)
  base = unprocessed_parent_relative_file(path[1..-1])
  if base
    false
  else
    true
  end
end

#contents(path) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/sprockets_fs.rb', line 82

def contents(path)
  all = files
  res = if path == '/'
    all.select { |x| x.split("/").size == 1 } + dirs.select { |x| x.split("/").size == 1 }
  else
    all.select do |f|
      f.starts_with?(path[1..-1])
    end.map { |f| f.gsub("#{path[1..-1]}/","") }
  end.uniq

  res = res.map do |f|
    if f =~ /\.coffee$/
      [f,f.gsub(/\.coffee$/,"")]
    elsif f =~ /\.erb$/
      [f,f.gsub(/\.erb$/,"")]
    else
      f
    end
  end.flatten

  puts "contents #{res.size}"

  res
end

#convert_to_absolute(file, safe = true) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/sprockets_fs.rb', line 42

def convert_to_absolute(file,safe=true)
  file = file.to_s if file
  parent_dirs.each do |dir|
    base = "#{dir}/#{file}"
    return base if FileTest.exist?(base)
  end
  raise "can't convert #{file} to absolute" if safe
  nil
end

#convert_to_relative(file) ⇒ Object



32
33
34
35
36
37
38
39
40
41
# File 'lib/sprockets_fs.rb', line 32

def convert_to_relative(file)
  file = file.to_s if file
  parent_dirs.each do |dir|
    if file.starts_with?(dir)
      return file.gsub("#{dir}/","")
    end
  end
  raise "can't convert #{file} to relative, dirs are\n"+parent_dirs.join("\n")
  nil
end

#directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/sprockets_fs.rb', line 78

def directory?(path)
  path == "/" || dirs.include?(path[1..-1])
end

#dirsObject



58
59
60
61
62
63
64
65
66
# File 'lib/sprockets_fs.rb', line 58

def dirs
  res = []
  files.each do |f|
    if f.split("/").size > 1
      res << File.dirname(f)
    end
  end
  res
end

#file?(path) ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
71
72
73
74
75
76
77
# File 'lib/sprockets_fs.rb', line 68

def file?(path)
  files.each do |f|
    return true if f == path[1..-1]
    if f.split(".").size > 2
      short = f.split(".")[0..-2].join(".")
      return true if short == path[1..-1]
    end
  end
  false
end

#filesObject



51
52
53
54
55
56
57
# File 'lib/sprockets_fs.rb', line 51

def files
  res = []
  env.each_file do |f|
    res << convert_to_relative(f)
  end
  res - ["rails.png"]
end

#parent_dir=(dir) ⇒ Object



20
21
22
# File 'lib/sprockets_fs.rb', line 20

def parent_dir=(dir)
  self.parent_dirs = [dir]
end

#read_file(path) ⇒ Object



116
117
118
119
120
121
122
123
124
125
# File 'lib/sprockets_fs.rb', line 116

def read_file(path)
  ML.log("path #{path}") do
    base = convert_to_absolute(path[1..-1],false)
    if base
      File.read(base).strip
    else
      env.find_asset(path[1..-1]).to_s.strip
    end
  end
end

#size(path) ⇒ Object



126
127
128
# File 'lib/sprockets_fs.rb', line 126

def size(path)
  read_file(path).size
end

#unprocessed_parent_relative_file(file) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/sprockets_fs.rb', line 107

def unprocessed_parent_relative_file(file)
  files.each do |possible_parent|
    if possible_parent.starts_with?(file) && possible_parent.length > file.length
      return possible_parent
    end
  end
  nil
end

#write_to(path, contents) ⇒ Object



140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/sprockets_fs.rb', line 140

def write_to(path,contents)
  full = convert_to_absolute(path[1..-1],false)
  if !full
    if parent_dirs.size == 1
      full = "#{parent_dirs.first}#{path}"
    else
      raise "can't write"
    end
  end

  File.create full, contents
end