class SimpleDocument

Public Instance Methods

all(subset) click to toggle source

Return a Hash of all documents in a specific subset in this store. The Hash keys are the document names, the hash values are an Array of documents for this name, with potentially different locales.

# File lib/simple_document.rb, line 31
def all(subset)
  store.all(subset)
end
parse_name(name) click to toggle source
# File lib/simple_document.rb, line 74
def parse_name(name)
  if name =~ /^([A-Za-z][-a-zA-Z0-9_]*)\/([A-Za-z][-a-zA-Z0-9_]*)$/
    return [$1, $2]
  end
  
  raise ArgumentError, "Invalid name #{name.inspect}"
end
read(name, options = {}) click to toggle source

Fetches a document by name from a specific subset. If a localize is set it tries to load a localized variant of the document first. If there is no such document, it then tries to load a non-localized variant of the document.

The method returns nil if there is no such document.

# File lib/simple_document.rb, line 54
def read(name, options = {})
  subset, name = parse_name(name)
  
  # stringify keys
  options = options.inject({}) { |hash, (k,v)| hash.update(k.to_s => v) }
  locale = options.delete "locale"
  
  (locale && store.fetch_with_locale(subset, name, locale)) ||
  store.fetch_with_locale(subset, name, nil)
end
read!(name, options = {}) click to toggle source

Fetches a document by name from a specific subset, using fetch. In opposite to fetch this method raises an Errno::ENOENT exception if there is no such document.

# File lib/simple_document.rb, line 68
def read!(name, options = {})
  read(name, options) || raise(Errno::ENOENT, "SimpleDocument[#{url}]/#{name}")
end
store() click to toggle source
# File lib/simple_document.rb, line 20
def store
  @store || raise("Missing SimpleDocument.url setting")
end
uncache() click to toggle source
# File lib/simple_document.rb, line 24
def uncache
  self.url = store.url if store
end
url() click to toggle source
# File lib/simple_document.rb, line 12
def url
  @store.url if @store
end
url=(url) click to toggle source
# File lib/simple_document.rb, line 16
def url=(url)
  @store = FileStore.new(url)
end
write(name, data) click to toggle source

Stores a single document in a specific subset in this store.

# File lib/simple_document.rb, line 36
def write(name, data)
  subset, name = parse_name(name)

  # stringify keys
  data = data.inject({}) { |hash, (k,v)| hash.update(k.to_s => v) }
  locale = data.delete "locale"

  raise(ArgumentError, "Missing format entry") unless data.key?("format")
  
  store.store(subset, name, locale, data)
end