class Typingpool::Project::Local
Representation of the Project
instance in the local filesystem. Subclass of Filer::Dir
; see Filer::Dir
docs for additional details.
This is basically a local dir with various subdirs and files containing the canonical representation of the project, including data on remote resources, the project ID and subtitle, the audio files themselves, and, when complete, an HTML transcript of that audio, along with supporting CSS and Javascript files.
Attributes
Returns the dir path.
Public Class Methods
Constructor. Creates a directory in the filesystem for the project.
==== Params [name] Name of the associated project. [base_dir] Path to the local directory into which the project dir should be placed. [template_dir] Path to the dir which will be used as a base template for new projects. ==== Returns Project::Local instance.
Typingpool::Filer::Dir::create
# File lib/typingpool/project/local.rb, line 31 def create(name, base_dir, template_dir) local = super(File.join(base_dir, name)) FileUtils.cp_r(File.join(template_dir, '.'), local) local.create_id local end
Takes one or more symbols. Adds corresponding getter/setter and delete method(s) to Project::Local
, which read (getter) and write (setter) and delete corresponding text files in the data directory.
So, for example, 'data_file_accessor :name' would allow you to later create the file 'data/foo.txt' in the project dir by calling 'project.local.name = “Foo”', read that same file via 'project.local.name', and delete the file via 'project.local.delete_name'
# File lib/typingpool/project/local.rb, line 80 def data_file_accessor(*syms) syms.each do |sym| define_method(sym) do file('data',"#{sym.to_s}.txt").read end define_method("#{sym.to_s}=".to_sym) do |value| file('data',"#{sym.to_s}.txt").write(value) end define_method("delete_#{sym.to_s}".to_sym) do if File.exist? file('data',"#{sym.to_s}.txt") File.delete(file('data',"#{sym.to_s}.txt")) end end end end
Takes the name of a project and a path. If there's a directory with a matching name in the given path whose file layout indicates it is a Project::Local
instance (see 'ours?' docs), returns a corresponding Project::Local
instance.
Typingpool::Filer::Dir::named
# File lib/typingpool/project/local.rb, line 42 def named(string, path) match = super if match && ours?(match) return match end return end
Takes a Filer::Dir
instance. Returns true or false depending on whether the file layout inside the dir indicates it is a Project::Local
instance.
# File lib/typingpool/project/local.rb, line 53 def ours?(dir) File.exist?(dir.subdir('audio')) && File.exist?(dir.subdir('audio', 'originals')) end
Takes the name of a project and returns true if it is a valid name for a directory in the local filesystem, false if not.
# File lib/typingpool/project/local.rb, line 59 def valid_name?(name) Utility.in_temp_dir do |dir| begin FileUtils.mkdir(File.join(dir, name)) rescue Errno::ENOENT return false end #begin return File.exist?(File.join(dir, name)) end #Utility.in_temp_dir do... end
Public Instance Methods
Creates a file storing the canonical ID of the project in 'data/id.txt'. Raises an exception if the file already exists.
# File lib/typingpool/project/local.rb, line 109 def create_id if id raise Error, "id already exists" end file('data','id.txt').write(SecureRandom.hex(16)) end
Returns the ID of the project, as stored in 'data/id.txt'.
# File lib/typingpool/project/local.rb, line 103 def id file('data','id.txt').read end