Class: ViewAssets::Manager::Modifier

Inherits:
Object
  • Object
show all
Includes:
Term::ANSIColor
Defined in:
lib/view_assets/manager/modifier.rb

Direct Known Subclasses

CssModifier, JsModifier

Instance Method Summary collapse

Instance Method Details

#analyize(type, asset) ⇒ Object



161
162
163
# File 'lib/view_assets/manager/modifier.rb', line 161

def analyize(type, asset)
  return type != '' || asset != []
end

#generate_requirements_block(manifest, requirements) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/view_assets/manager/modifier.rb', line 75

def generate_requirements_block(manifest, requirements)
  if retrieve_type(manifest) == :app
    # Remove 'app/:asset_path/application' or
    #        'app/:asset_path/application.:ext'
    requirements.delete("app/#{asset_path}/application")
    requirements.delete("app/#{asset_path}/application.#{ext}")

    # Remove 'app/:asset_path/controller/controller' or
    #        'app/:asset_path/controller/controller.ext'
    if manifest.count('/') > 2
      controller_asset = manifest.gsub(
                           /(?<path>.+\/.+\/(?<controller>.+)\/).+/,
                           '\k<path>\k<controller>'
                         )

      requirements.delete("#{controller_asset}")
      requirements.delete("#{controller_asset}.#{ext}")
    end
  end

  requirements.delete("#{manifest}.#{ext}")
  requirements.reject! { |req| req.match(manifest + '/') }

  requirements.each_with_object(["/**"]) do |requirement, block|
    directive, prefix = case retrieve_type(requirement)
                        when :vendor
                          [' *= require_vendor ', "vendor/#{asset_path}/"]
                        when :lib
                          [' *= require_lib ', "lib/#{asset_path}/"]
                        when :app
                          [' *= require ', "app/#{asset_path}"]
                        end

    block.push("#{directive}#{requirement.gsub(prefix, "")}")
  end.push(' */').join("\n")+"\n"
end

#mapObject



165
166
167
# File 'lib/view_assets/manager/modifier.rb', line 165

def map
  @map_value
end

#modify(path, new_requirement_block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/view_assets/manager/modifier.rb', line 130

def modify(path, new_requirement_block)
  path = PathInfo.new(path).abs
  manifest = if FileTest.exists?("#{path}.#{ext}")
               "#{path}.#{ext}"
             elsif FileTest.exists?("#{path}/index.#{ext}")
               "#{path}/index.#{ext}"
             else
               raise Error.new(red("Can't Load #{path.rel}"))
             end

  start_of_requirement_block = false
  end_of_requirement_block = false
  asset_content = ''

  Pathname.new(manifest).each_line do |line|
    legal_directive = analyize(*directive.parse(line))

    start_of_requirement_block = true if legal_directive && !start_of_requirement_block
    end_of_requirement_block = true if !legal_directive && start_of_requirement_block && !end_of_requirement_block

    if !start_of_requirement_block || end_of_requirement_block
      asset_content << line
    end
  end

  File.open(manifest, 'w') do |file|
    file << new_requirement_block
    file << asset_content
  end
end

#remove(index, options = {}) ⇒ Object

Raises:



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/view_assets/manager/modifier.rb', line 112

def remove(index, options = {})
  options = { :verbal => false }.update(options)

  raise Error.new(red("#{index} is not exist.")) unless map[retrieve_type(index)].key?(index)

  puts green("rm #{index}") if options[:verbal]
  index_path = PathInfo.new(index).abs
  if FileTest.exist?("#{index_path}.#{ext}")
    FileUtils.rm_r("#{index_path}.#{ext}")
  elsif FileTest.exist?(index_path)
    FileUtils.rm_r("#{index_path}")
  else
    raise Error.new(red("#{index} Is Not Exist."))
  end

  update_requirements(index, '', options)
end

#retrieve_type(index) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/view_assets/manager/modifier.rb', line 31

def retrieve_type(index)
  case
  when /^vendor\/.+$/.match(index)
    :vendor
  when /^lib\/.+$/.match(index)
    :lib
  when /^app\/.+$/.match(index)
    :app
  end
end

#update(index, new_index, options = {}) ⇒ Object

Raises:



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/view_assets/manager/modifier.rb', line 6

def update(index, new_index, options = {})
  options = { :verbal => false }.update(options)

  type = retrieve_type(index)
  validate_index = map[type].key?(index)

  # unless validate_index
  #   map.each_value do |manifests|
  #     validate_index = manifests.select do |manifest, indexes|
  #                        indexes.any? do |map_index|
  #                          map_index.match(index)
  #                        end
  #                      end
  #
  #     break if validate_index
  #   end
  # end

  raise Error.new(red("#{index} is not exist.")) unless validate_index

  puts green("mv #{index}, #{new_index}") if options[:verbal]
  update_index(index, new_index)
  update_requirements(index, new_index, options)
end

#update_index(index, new_index) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/view_assets/manager/modifier.rb', line 42

def update_index(index, new_index)
  path_to_index = PathInfo.new(index).abs
  new_path_to_index = PathInfo.new(new_index).abs

  if FileTest.exists?(path_to_index)
    FileUtils.mv(path_to_index, new_path_to_index)
  elsif FileTest.exists?("#{path_to_index}.#{ext}")
    FileUtils.mv("#{path_to_index}.#{ext}", "#{new_path_to_index}.#{ext}")
  else
    raise Error.new(red("Manifest #{index} doesn't exist."))
  end
end

#update_mapObject



169
170
171
# File 'lib/view_assets/manager/modifier.rb', line 169

def update_map
  @map_value = @map.draw
end

#update_requirements(index, new_index, options = {}) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/view_assets/manager/modifier.rb', line 55

def update_requirements(index, new_index, options = {})
  options = { :verbal => false }.update(options)

  map.each_value do |type|
    type.each do |manifest, requirements|
      if requirements.any? { |requirement| requirement == index }
        puts ("Update #{green(manifest)}") if options[:verbal]

        if new_index.empty?
          requirements.delete(index)
        else
          requirements[requirements.index(index)] = new_index
        end

        modify(manifest, generate_requirements_block(manifest, requirements))
      end
    end
  end
end