Class: Typingpool::Config

Inherits:
Object
  • Object
show all
Defined in:
lib/typingpool/config.rb,
lib/typingpool/config/root.rb

Overview

Hierarchical config object. Can be read from a YAML file and is often modified at runtime, for example in response to script flags.

Fields

All listed defaults are populated when you run tp-install.

Required

[transcripts] Unexpanded path to working directory for
              transcripts. This is where tp-make creates new
              transcript projects, and where other scripts like
              tp-assign, tp-review and tp-finish look for
              them. Default: On systems with a ~/Desktop (like OS
              X), ~/Desktop/Transcripts. Elsewhere,
              ~/transcripts.

amazon

[key]    An Amazon Web Services "Access Key ID." Default: none.
[secret] An Amazon Web Services "Secret Access Key." Default: none.
[bucket] The name of the "bucket" on Amazon S3 where your uploads
         will be stored. Not required if you specify SFTP config
         instead (see below). Default: Generated for you when you
         run tp-install.

Optional

[cache]     Unexpanded path to the cache file (pstore). Default:
            ~/.typingpool.cache
[templates] Unexpanded path to directory for user-created
            templates. Will be searched before looking in the
            template dir within the app. Default: 'templates' or
            'Templates' (OS X) dir inside the transcripts dir.

amazon

[url] Base URL to use when linking to files uploaded to S3. You
      may want to use this if you do custom domain mapping on
      S3. Default is https://$bucket.s3.amazonaws.com.

sftp

If you provide SFTP config, the specified SFTP server will be used to host remote mp3 and html files rather than Amazon S3. At minimum, you must provide a user, host, and URL. SFTP will work fine with public-key authentication (passwordless login). In fact, I’ve not bothered to add password support yet.

[user] SFTP username
[host] SFTP server
[path] Files will be uploaded into this path. Optional.
[url]  Base URL to use when linking to files uploaded using the
       preceding config.

assign

Defaults for tp-assign.

[reward]   Pay per transcription chunk in U.S. dollars. Default: 0.75.
[deadline] Length of time a worker has to complete a
           transcription job after accepting it (HIT
           'AssignmentDurationInSeconds' in the Mechanical Turk
           API). For details on the format, see docs for
           Utility.timespec_to_seconds. Default: 3h.
[approval] Length of time before a submitted transcription job is
           automatically approved (HIT
           'AutoApprovalDelayInSeconds' in the Mechanical Turk
           API). For details on the format, see docs for
           Utility.timespec_to_seconds. Default: 1d.
[lifetime] Length of time before a transcription job is no longer
           available to be accepted (HIT 'LifetimeInSeconds' in
           the Mechanical Turk API). For details on the format,
           see docs for Utility.timespec_to_seconds. Default: 2d.
[qualify]  An array of qualifications with which to filter workers
           who may accept a transcript job. The first part of the
           qualification should be the string form of a key in
           RTurk::Qualifications.types (see
           https://github.com/mdp/rturk/blob/master/lib/rturk/builders/qualification_builder.rb
           ). The second part should be one of the following
           comparators: > >= < <= == != exists. The optional
           third part is a value. Default: ['approval_rate >=
           95'].
[keywords] An array of keywords with which to tag each
           transcription job. Default: ['transcription', 'audio',
           'mp3'].

API

Values are read via same-named methods and set via same-named equals methods, like so:

transcript_path = config.transcripts
config.transcripts = new_path

Nested sections are created simply by declaring a nested class (which should typically inherit from Config, even if nested several levels lower).

Fields can be assigned special behaviors:

class Config
  class Root < Config
    local_path_reader :transcripts
    class SFTP < Config
      never_ends_in_slash_reader :url
    end
  end
end

conf = Typingpool::Config.file
conf.transcripts = '~/Documents/Transcripts'
puts conf.transcripts #'/Volumes/Redsector/Users/chad/Documents/Transcripts'
conf.sftp.url = 'http://luvrecording.s3.amazonaws.com/'
puts conf.sftp.url #'http://luvrecording.s3.amazonaws.com'

Defined Under Namespace

Classes: Root

Constant Summary collapse

@@default_file =
"~/.typingpool"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Config

Returns a new instance of Config.



107
108
109
# File 'lib/typingpool/config.rb', line 107

def initialize(params)
  @param = params
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args) ⇒ Object



216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/typingpool/config.rb', line 216

def method_missing(meth, *args)
  equals_param = equals_method?(meth)
  if equals_param
    args.count == 1 or raise Error::Argument, "Wrong number of args (#{args.count} for 1)"
    return @param[equals_param] = args[0]
  end
  args.empty? or raise Error::Argument, "Too many args #{meth} #{args.join('|')}"
  value = @param[meth.to_s]
  if self.class.subklass?(meth.to_s) && value
    return self.class.subklass?(meth.to_s).new(value)
  end
  value
end

Class Method Details

.default_fileObject

Will always return ~/.typingpool unless you subclass. Will be handed to File.expand_path before use.



123
124
125
# File 'lib/typingpool/config.rb', line 123

def default_file
  @@default_file
end

.define_accessor(*syms, &block) ⇒ Object



180
181
182
183
184
185
186
187
188
# File 'lib/typingpool/config.rb', line 180

def define_accessor(*syms, &block)
  #explicit block passing to avoid sefault in 1.9.3-p362
  define_reader(*syms) do |value|
    block.call(value) if value
  end
  define_writer(*syms) do |value|
    block.call(value)
  end
end

.define_reader(*syms, &block) ⇒ Object



161
162
163
164
165
166
167
168
169
# File 'lib/typingpool/config.rb', line 161

def define_reader(*syms, &block)
  #explicit block passing to avoid sefault in 1.9.3-p362
  syms.each do |sym|
    define_method(sym) do
      value = @param[sym.to_s]
      block.call(value)
    end
  end
end

.define_writer(*syms, &block) ⇒ Object



171
172
173
174
175
176
177
178
# File 'lib/typingpool/config.rb', line 171

def define_writer(*syms, &block)
  #explicit block passing to avoid sefault in 1.9.3-p362
  syms.each do |sym|
    define_method("#{sym.to_s}=".to_sym) do |value|
      @param[sym.to_s] = block.call(value)
    end
  end
end

.file(path = File.expand_path(default_file)) ⇒ Object

Constructor.

Params

path

Fully expanded path to YAML file.

Returns

Config instance.



117
118
119
# File 'lib/typingpool/config.rb', line 117

def file(path=File.expand_path(default_file))
  Root.new(YAML.load(IO.read((path))))
end

.from_bundled_templateObject

Returns a new Config instance, empty except for the values included in lib/templates/config.yml



129
130
131
# File 'lib/typingpool/config.rb', line 129

def from_bundled_template
  file(File.join(Utility.lib_dir, 'templates', 'config.yml'))
end

.inherited(subklass) ⇒ Object



190
191
192
193
# File 'lib/typingpool/config.rb', line 190

def inherited(subklass)
  @@subklasses ||= {}
  @@subklasses[subklass.name.downcase] = subklass
end

.local_path_reader(*syms) ⇒ Object

Define a field in a Config subclass as a local path. Reads on that field will be filtered through File.expand_path.



137
138
139
140
141
# File 'lib/typingpool/config.rb', line 137

def local_path_reader(*syms)
  define_reader(*syms) do |value|
    File.expand_path(value) if value
  end
end

.never_ends_in_slash_reader(*syms) ⇒ Object

Define a field in a Config subclass as never ending in ‘/’. Useful for URLs and SFTP path specs. When a field is set to a value ending in ‘/’, the last character is stripped.



146
147
148
149
150
# File 'lib/typingpool/config.rb', line 146

def never_ends_in_slash_reader(*syms)
  define_reader(*syms) do |value|
    value.sub(/\/$/, '') if value
  end
end

.subklass?(param) ⇒ Boolean

Returns:

  • (Boolean)


195
196
197
# File 'lib/typingpool/config.rb', line 195

def subklass?(param)
  @@subklasses["#{self.name.downcase}::#{param.downcase}"] 
end

.time_accessor(*syms) ⇒ Object

Define a field in a Config subclass as a time-length specification. For format details, see docs for Utility.timespec_to_seconds.



155
156
157
158
159
# File 'lib/typingpool/config.rb', line 155

def time_accessor(*syms)
  define_accessor(*syms) do |value|
    Utility.timespec_to_seconds(value) or raise Error::Argument::Format, "Can't convert '#{value}' to time"
  end
end

Instance Method Details

#[](key) ⇒ Object

Read the raw data for a field



207
208
209
# File 'lib/typingpool/config.rb', line 207

def [](key)
  @param[key]
end

#[]=(key, value) ⇒ Object

Set the raw data for a field



212
213
214
# File 'lib/typingpool/config.rb', line 212

def []=(key, value)
  @param[key] = value
end

#to_hashObject

All fields as raw key-value pairs. For nested subclasses, the value is another hash.



202
203
204
# File 'lib/typingpool/config.rb', line 202

def to_hash
  @param
end