Class: QuestionCompiler::LocalContextReader

Inherits:
Object
  • Object
show all
Defined in:
lib/question_compiler/local.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(root) ⇒ LocalContextReader

Pass the path to the contexts folder. I.e. the folder whose children are usernames.



21
22
23
# File 'lib/question_compiler/local.rb', line 21

def initialize(root)
  @root = root
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



25
26
27
# File 'lib/question_compiler/local.rb', line 25

def log
  @log
end

Instance Method Details

#file?(sub_path) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/question_compiler/local.rb', line 16

def file?(sub_path)
  File.file? File.join(@root, @context_path, sub_path)
end

#glob(glob_str) ⇒ Object

Given a context path such as ‘jarrett/example` and a glob such as `assets/*.css`, returns an array of matching paths. The returned paths are relative to the context folder.



8
9
10
11
12
13
14
# File 'lib/question_compiler/local.rb', line 8

def glob(glob_str)
  Dir.glob(File.join(@root, @context_path, glob_str)).select do |sub_path|
    File.file? sub_path
  end.map do |sub_path|
    sub_path.sub(File.join('contexts', @context_path), '')
  end
end

#open(context_path) ⇒ Object

Pass a path such as ‘jarrett/example`.



28
29
30
31
32
33
# File 'lib/question_compiler/local.rb', line 28

def open(context_path)
  @context_path = context_path
  # This is basically a no-op. Some other context reader implementations may need to
  # open and close the question as a whole.
  yield
end

#read(sub_path) ⇒ Object

Given a path to a file within the context such as ‘assets/context.css`, returns the file data as a string. Returns nil if the file doesn’t exist or is unreadable.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/question_compiler/local.rb', line 37

def read(sub_path)
  path = File.expand_path(File.join(@root, @context_path, sub_path))
  begin
    data = File.read path
    @log.debug "R: #{path} (#{data.length} bytes)"
    data
  rescue Exception
    @log.warn "Could not read #{path}"
    nil
  end
end