class Ant::Storage::Datasource::JSONRepository

Stores objects as a plain json file inside the specified folder. Uses this for testing purpouse.

Public Class Methods

from_config(conf) click to toggle source
# File lib/ant/storage/datasource/json_repository.rb, line 14
def self.from_config(conf)
  folder = conf['storage'].gsub('$name', conf['schema_name'])
  if conf['schema'].nil?
    # TODO: decouple use of classes
    new(folder, conf['primary_key'].to_sym,
        IDGenerators[:id])
  else
    # TODO: This line is very high coupled to ant-nanoservice
    new(folder, conf['schema']::PRIMARY_KEY, IDGenerators[:id])
  end
end
new(folder, id, id_generator) click to toggle source
Calls superclass method
# File lib/ant/storage/datasource/json_repository.rb, line 26
def initialize(folder, id, id_generator)
  @path = folder
  FileUtils.mkdir_p folder
  super(id, id_generator)
end

Public Instance Methods

create_(data) click to toggle source
# File lib/ant/storage/datasource/json_repository.rb, line 40
def create_(data)
  store(data)
  data
end
get(id) click to toggle source
# File lib/ant/storage/datasource/json_repository.rb, line 32
def get(id)
  path = full_path(id)
  raise(ObjectNotFound, id) unless File.file?(path)

  contents = File.read(path)
  JSON.parse(contents, symbolize_names: true)
end
store(data) click to toggle source
# File lib/ant/storage/datasource/json_repository.rb, line 45
def store(data)
  id = data[@id]
  File.write(full_path(id), data.to_json)
end

Private Instance Methods

full_path(id) click to toggle source
# File lib/ant/storage/datasource/json_repository.rb, line 52
def full_path(id)
  "#{@path}/#{id}.json"
end