Class: Ant::Storage::Datasource::JSONRepository

Inherits:
Repository
  • Object
show all
Defined in:
lib/ant/storage/datasource/json_repository.rb

Overview

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

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Repository

#create, #create_initial_object, #exist?

Constructor Details

#initialize(folder, id, id_generator) ⇒ JSONRepository

Returns a new instance of JSONRepository.



26
27
28
29
30
# 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

Class Method Details

.from_config(conf) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# 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

Instance Method Details

#create_(data) ⇒ Object



40
41
42
43
# File 'lib/ant/storage/datasource/json_repository.rb', line 40

def create_(data)
  store(data)
  data
end

#get(id) ⇒ Object

Raises:



32
33
34
35
36
37
38
# 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) ⇒ Object



45
46
47
48
# 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