Class: SimpleDocument::FileStore
- Defined in:
- lib/simple_document/file_store.rb
Defined Under Namespace
Classes: Document
Instance Attribute Summary collapse
-
#url ⇒ Object
(also: #root)
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#all(subset) ⇒ Object
Return a Hash of all documents in a specific subset in this store.
-
#fetch_with_locale(subset, name, locale = nil) ⇒ Object
Fetches a document by name from a specific subset with a given locale (or no locale, if the locale parameter is set to nil.).
-
#initialize(url) ⇒ FileStore
constructor
A new instance of FileStore.
- #store(subset, name, locale, data) ⇒ Object
Constructor Details
#initialize(url) ⇒ FileStore
Returns a new instance of FileStore.
11 12 13 |
# File 'lib/simple_document/file_store.rb', line 11 def initialize(url) @url = url end |
Instance Attribute Details
#url ⇒ Object (readonly) Also known as: root
Returns the value of attribute url.
8 9 10 |
# File 'lib/simple_document/file_store.rb', line 8 def url @url end |
Instance Method Details
#all(subset) ⇒ Object
Return a Hash of all documents in a specific subset in this store.
27 28 29 30 31 32 33 |
# File 'lib/simple_document/file_store.rb', line 27 def all(subset) Dir.glob("#{root}/#{subset}/*.{#{FORMAT_BY_EXTENSION.keys.join(",")}}"). map { |path| read_from_file(path) }. select(&:active?). group_by(&:name). tap { |hash| hash.default = [] } end |
#fetch_with_locale(subset, name, locale = nil) ⇒ Object
Fetches a document by name from a specific subset with a given locale (or no locale, if the locale parameter is set to nil.)
17 18 19 20 21 22 23 24 |
# File 'lib/simple_document/file_store.rb', line 17 def fetch_with_locale(subset, name, locale = nil) locale_ext = ".#{locale}" if locale pattern = "#{root}/#{subset}/#{name}#{locale_ext}.{#{FORMAT_BY_EXTENSION.keys.join(",")}}" Dir.glob(pattern).sort. map do |path| read_from_file(path) end. detect(&:active?) end |
#store(subset, name, locale, data) ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/simple_document/file_store.rb', line 45 def store(subset, name, locale, data) format = data.delete "format" body = data.delete "body" ext = FORMAT_BY_EXTENSION.key(format.to_sym) || raise(ArgumentError, "Unsupported format #{format.inspect}") locale_ext = ".#{locale}" if locale path = "#{dir(subset)}/#{name}#{locale_ext}.#{ext}" File.open(path, "w") do |file| file.write data.to_yaml unless data.empty? file.write "---\n" file.write body end fetch_with_locale(subset, name, locale) end |