Class: Kybus::Storage::Datasource::JSONRepository

Inherits:
Repository
  • Object
show all
Defined in:
lib/kybus/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.



27
28
29
30
31
# File 'lib/kybus/storage/datasource/json_repository.rb', line 27

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
25
# File 'lib/kybus/storage/datasource/json_repository.rb', line 14

def self.from_config(conf)
  folder = conf['storage']
  folder.gsub!('$name', conf['schema_name']) if 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 kybus-nanoservice
    new(folder, conf['schema']::PRIMARY_KEY, IDGenerators[:id])
  end
end

Instance Method Details

#create_(data) ⇒ Object



41
42
43
44
# File 'lib/kybus/storage/datasource/json_repository.rb', line 41

def create_(data)
  store(data)
  data
end

#get(id) ⇒ Object

Raises:



33
34
35
36
37
38
39
# File 'lib/kybus/storage/datasource/json_repository.rb', line 33

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



46
47
48
49
# File 'lib/kybus/storage/datasource/json_repository.rb', line 46

def store(data)
  id = data[@id]
  File.write(full_path(id), data.to_json)
end