Class: Typingpool::Project::Local

Inherits:
Filer::Dir show all
Defined in:
lib/typingpool/project/local.rb

Overview

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.

Instance Attribute Summary collapse

Attributes inherited from Filer::Files

#files

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Filer::Dir

#file, #file_path, #files, #finder_open, #initialize, #subdir, #to_s

Methods inherited from Filer::Files

#as, #each, #initialize, #mv!, #to_streams

Methods included from Utility::Castable

#as, included

Constructor Details

This class inherits a constructor from Typingpool::Filer::Dir

Instance Attribute Details

#pathObject (readonly)

Returns the dir path.



17
18
19
# File 'lib/typingpool/project/local.rb', line 17

def path
  @path
end

Class Method Details

.create(name, base_dir, template_dir) ⇒ Object

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.



31
32
33
34
35
36
# 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

.data_file_accessor(*syms) ⇒ Object

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’



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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.exists? file('data',"#{sym.to_s}.txt")
        File.delete(file('data',"#{sym.to_s}.txt"))
      end
    end
  end
end

.named(string, path) ⇒ Object

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.



42
43
44
45
46
47
48
# File 'lib/typingpool/project/local.rb', line 42

def named(string, path)
  match = super
  if match && ours?(match)
    return match
  end
  return
end

.ours?(dir) ⇒ Boolean

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.

Returns:

  • (Boolean)


53
54
55
# File 'lib/typingpool/project/local.rb', line 53

def ours?(dir)
  File.exists?(dir.subdir('audio')) && File.exists?(dir.subdir('audio', 'originals'))
end

.valid_name?(name) ⇒ Boolean

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.

Returns:

  • (Boolean)


59
60
61
62
63
64
65
66
67
68
# 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.exists?(File.join(dir, name))
  end #Utility.in_temp_dir do...
end

Instance Method Details

#create_idObject

Creates a file storing the canonical ID of the project in ‘data/id.txt’. Raises an exception if the file already exists.



109
110
111
112
113
114
# 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

#idObject

Returns the ID of the project, as stored in ‘data/id.txt’.



103
104
105
# File 'lib/typingpool/project/local.rb', line 103

def id
  file('data','id.txt').read
end