Sha256: e4c6b5726398de4509c9c773c7c18d29bf44adcb9c95d4b1205ce1ba83434f1e

Contents?: true

Size: 1.79 KB

Versions: 4

Compression:

Stored size: 1.79 KB

Contents

require 'noumenon/asset_repository'
require 'fileutils'

# A content repository which uses YAML files within the filesystem for storage.
# 
# @example The structure of a filesystem repository
#   index.yml
#   |- about
#      |- index.yml
#      |- team.yml
#      |- company.yml
#   contact.yml
#
# @example 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.
#
# @api public
class Noumenon::AssetRepository::FileSystem < Noumenon::AssetRepository
  # @param [ Hash ] options A hash of options.
  # @option options [ String ] :path The path to access the repository at.
  # @api public
  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
  
  # Save a static asset to the repository.
  #
  # @see Noumenon::Repository#save_asset
  # @api public
  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

  # Retreive a static asset to the repository.
  #
  # @see Noumenon::Repository#get_asset
  # @api public
  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
  
  def get_asset_path(path)
    return nil unless File.extname(path).size > 0
    filesystem_path_for path
  end

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

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
noumenon-0.2.3 lib/noumenon/asset_repository/file_system.rb
noumenon-0.2.2 lib/noumenon/asset_repository/file_system.rb
noumenon-0.2.1 lib/noumenon/asset_repository/file_system.rb
noumenon-0.2.0 lib/noumenon/asset_repository/file_system.rb