Class: Noumenon::AssetRepository::FileSystem

Inherits:
Noumenon::AssetRepository show all
Defined in:
lib/noumenon/asset_repository/file_system.rb

Overview

A content repository which uses YAML files within the filesystem for storage.

Examples:

The structure of a filesystem repository

index.yml
|- about
   |- index.yml
   |- team.yml
   |- company.yml
contact.yml

A piece of content

template: team_member
name: Jon Wood
position: Head Honcho of Awesomeness
bio: Jon's just awesome, we'll leave it at that.

Instance Attribute Summary

Attributes inherited from Noumenon::AssetRepository

#options

Instance Method Summary collapse

Methods inherited from Noumenon::AssetRepository

#url_for

Constructor Details

#initialize(options = {}) ⇒ FileSystem

Returns a new instance of FileSystem.

Parameters:

  • options (Hash) (defaults to: {})

    A hash of options.

Options Hash (options):

  • :path (String)

    The path to access the repository at.



25
26
27
28
29
30
31
# File 'lib/noumenon/asset_repository/file_system.rb', line 25

def initialize(options = {})
  unless options.key? :path
    raise ArgumentError.new("You must provide a path to the asset repository: Noumenon::AssetRepository::FileSystem.new(path: '/tmp')")
  end

  super options
end

Instance Method Details

#filesystem_path_for(path) ⇒ Object



61
62
63
# File 'lib/noumenon/asset_repository/file_system.rb', line 61

def filesystem_path_for(path)
  File.join options[:path], path
end

#get(path) ⇒ Object

Retreive a static asset to the repository.

See Also:

  • Repository#get_asset


50
51
52
53
54
# File 'lib/noumenon/asset_repository/file_system.rb', line 50

def get(path)
  return nil unless File.exist?(filesystem_path_for(path))
  return nil unless File.extname(path).size > 0
  File.read filesystem_path_for(path)
end

#get_asset_path(path) ⇒ Object



56
57
58
59
# File 'lib/noumenon/asset_repository/file_system.rb', line 56

def get_asset_path(path)
  return nil unless File.extname(path).size > 0
  filesystem_path_for path
end

#put(path, content) ⇒ Object

Save a static asset to the repository.

Raises:

  • (ArgumentError)

See Also:

  • Repository#save_asset


37
38
39
40
41
42
43
44
# File 'lib/noumenon/asset_repository/file_system.rb', line 37

def put(path, content)
  raise ArgumentError.new("Assets must have a file extension.") unless File.extname(path).size > 0

  full_path = filesystem_path_for(path)
  directory = File.dirname(full_path)
  FileUtils.mkdir_p(directory) unless File.exist?(directory)
  File.open(full_path, "w") { |f| f.write content }
end