Class: Yoda::Server::FileStore
- Inherits:
-
Object
- Object
- Yoda::Server::FileStore
- Defined in:
- lib/yoda/server/file_store.rb
Class Method Summary collapse
Instance Method Summary collapse
- #get(uri_string) ⇒ String?
-
#initialize ⇒ FileStore
constructor
A new instance of FileStore.
- #load(uri_string) ⇒ Object
- #program_file_uri?(uri_string) ⇒ Boolean
- #read(uri_string) ⇒ Object
- #store(uri_string, text) ⇒ Object
Constructor Details
#initialize ⇒ FileStore
Returns a new instance of FileStore.
6 7 8 |
# File 'lib/yoda/server/file_store.rb', line 6 def initialize @cache = Concurrent::Map.new end |
Class Method Details
.path_of_uri(uri_string) ⇒ Object
41 42 43 44 45 46 47 |
# File 'lib/yoda/server/file_store.rb', line 41 def self.path_of_uri(uri_string) uri = URI.parse(uri_string) return nil unless uri.scheme == 'file' uri.path rescue URI::InvalidURIError nil end |
.uri_of_path(path) ⇒ Object
36 37 38 |
# File 'lib/yoda/server/file_store.rb', line 36 def self.uri_of_path(path) "file://#{File.(path)}" end |
Instance Method Details
#get(uri_string) ⇒ String?
12 13 14 |
# File 'lib/yoda/server/file_store.rb', line 12 def get(uri_string) @cache[uri_string] end |
#load(uri_string) ⇒ Object
24 25 26 |
# File 'lib/yoda/server/file_store.rb', line 24 def load(uri_string) store(uri_string, read(uri_string)) end |
#program_file_uri?(uri_string) ⇒ Boolean
50 51 52 53 54 |
# File 'lib/yoda/server/file_store.rb', line 50 def program_file_uri?(uri_string) %w(.c .rb).include?(File.extname(URI.parse(uri_string).path)) rescue URI::InvalidURIError => _e false end |
#read(uri_string) ⇒ Object
29 30 31 32 33 |
# File 'lib/yoda/server/file_store.rb', line 29 def read(uri_string) path = self.class.path_of_uri(uri_string) fail ArgumentError unless path File.read(path) end |
#store(uri_string, text) ⇒ Object
18 19 20 21 |
# File 'lib/yoda/server/file_store.rb', line 18 def store(uri_string, text) return unless program_file_uri?(uri_string) @cache[uri_string] = text end |