Class: Capsium::Package::Routes

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/capsium/package/routes.rb

Constant Summary collapse

ROUTES_FILE =
"routes.json"
DEFAULT_INDEX_TARGET =
"content/index.html"
INDEX_ROUTE =
"/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, manifest, storage) ⇒ Routes

Returns a new instance of Routes.



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/capsium/package/routes.rb', line 22

def initialize(path, manifest, storage)
  @path = path
  @dir = File.dirname(path)
  @manifest = manifest
  @storage = storage
  @config = if File.exist?(path)
              RoutesConfig.from_json(File.read(path))
            else
              generate_routes
            end
  validate_index_path(@config.resolve(INDEX_ROUTE)&.target&.file)
  validate
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



12
13
14
# File 'lib/capsium/package/routes.rb', line 12

def config
  @config
end

#indexObject (readonly)

Returns the value of attribute index.



16
17
18
# File 'lib/capsium/package/routes.rb', line 16

def index
  @index
end

#manifestObject (readonly)

Returns the value of attribute manifest.



16
17
18
# File 'lib/capsium/package/routes.rb', line 16

def manifest
  @manifest
end

#pathObject (readonly)

Returns the value of attribute path.



16
17
18
# File 'lib/capsium/package/routes.rb', line 16

def path
  @path
end

#storageObject (readonly)

Returns the value of attribute storage.



16
17
18
# File 'lib/capsium/package/routes.rb', line 16

def storage
  @storage
end

Instance Method Details

#add_route(route, target) ⇒ Object



40
41
42
43
# File 'lib/capsium/package/routes.rb', line 40

def add_route(route, target)
  validate_route_target(route, target)
  @config.add(route, target)
end

#remove_route(route) ⇒ Object



50
51
52
# File 'lib/capsium/package/routes.rb', line 50

def remove_route(route)
  @config.remove(route)
end

#resolve(url_path) ⇒ Object



36
37
38
# File 'lib/capsium/package/routes.rb', line 36

def resolve(url_path)
  @config.resolve(url_path)
end

#save_to_file(output_path = @path) ⇒ Object



64
65
66
# File 'lib/capsium/package/routes.rb', line 64

def save_to_file(output_path = @path)
  File.write(output_path, to_json)
end

#to_json(*_args) ⇒ Object



60
61
62
# File 'lib/capsium/package/routes.rb', line 60

def to_json(*_args)
  @config.sort!.to_json
end

#update_route(route, updated_route, updated_target) ⇒ Object



45
46
47
48
# File 'lib/capsium/package/routes.rb', line 45

def update_route(route, updated_route, updated_target)
  validate_route_target(updated_route, updated_target)
  @config.update(route, updated_route, updated_target)
end

#validateObject



54
55
56
57
58
# File 'lib/capsium/package/routes.rb', line 54

def validate
  @config.routes.each do |route|
    route.target.validate(@manifest, @storage)
  end
end